Contents
2. ¶
StructuredProgramming StepwiseRefinement 하. TopDown 하 , Depth First (트 ) 하하 해 . UnitTest 하 , UnitTest Code 하.
3. Version 0.1 - ¶
. 형 '' . ' , 해 한.' ;
~cpp int main () { Input (); Process (); Output (); return 0; } void Input () { } void Process () { } void Output () { }
Process () 확하 하 , .
~cpp int main () { Input (); ScheduledWalk (); Output (); return 0; } void ScheduledWalk () { }
4. Version 0.2 - Refinement ¶
트 1 , 해. 파 ..~ . 파 한 하 void 형 return false; . 하.
~cpp void Input() { InputBoardSize(); InputStartRoachPosition(); InputRoachJourney(); } void InputBoardSize() { } void InputStartRoachPosition() { } void InputRoachJourney() { } void ScheduledWalk() { while (!IsFinished()) { Move(); } } BOOL IsFinished() { return IsJourneyEnd() || IsAllBoardChecked(); } void Move() { } void Output() { OutputMoveCount(); OutputBoardStatus(); } void OutputMoveCount() { } void OutputBoardStatus() { }
5. Version 0.3 - Refinement More ¶
행 한 15 . 하 10 할.~ 하 .~
~cpp typedef int BOOL; void Input(); void InputBoardSize(); void InputStartRoachPosition(); void InputRoachJourney(); void ScheduledWalk(); BOOL IsFinished(); BOOL IsJourneyEnd(); BOOL IsAllBoardChecked(); void MoveNext(); void Output(); void OutputMoveCount(); void OutputBoardStatus(); int main() { Input(); ScheduledWalk(); Output(); return 0; } void Input() { InputBoardSize(); InputStartRoachPosition(); InputRoachJourney(); } void InputBoardSize() { } void InputStartRoachPosition() { } void InputRoachJourney() { } void ScheduledWalk() { while (!IsFinished()) { MoveNext(); } } BOOL IsFinished() { return IsJourneyEnd() || IsAllBoardChecked(); } BOOL IsJourneyEnd() { return false; } BOOL IsAllBoardChecked() { return false; } void MoveNext() { } void Output() { OutputMoveCount(); OutputBoardStatus(); } void OutputMoveCount() { } void OutputBoardStatus() { }
(Hierarchy Input-Process-Output) . ( )
6. Version 0.4 - Implementation : Input ¶
10 휴 - 1 55. ;
해 . (, 화 하, )
해 . (, 화 하, )
. 하 해 .
함 해 할 했. 키 . DFD 흐 해 표할 , . 하 하 하, 하 했. HIPO Argument 표해 . ( . 하, .~)
해 판하 .
- - -> . 행 한. (행 하 할 . UnitTest )
- Depth-Module First. -> 판, 하 했. (, 하 Bottom-Up .. 해 함 하 확하 해 . 할 .)
~cpp void InputBoardSize() { }
함 하 Board Size Input. 판 크 . scanf cin 하 할 .
~cpp void InputBoardSize() { int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); printf ("%d, %d \n", boardRow, boardCol); }
해 Visual C++ , 해 하 . . 해 해.
하 . 하 , 하 해 포 , 화 . (scanf 함 한 .) 하 했. int 형 하 하 하 판.
~cpp typedef struct __IntegerPair { // return 하 한 int n1; int n2; } IntPair; IntPair InputBoardSize(); // void InputBoardSize() IntPair InputBoardSize() . 토 '' . IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; 0 return size; }
테트해?
~cpp #include <stdio.h> typedef int BOOL; typedef struct __IntegerPair { int n1; int n2; } IntPair; IntPair InputBoardSize(); void Input(); IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } void Input() { IntPair testReceiver; testReceiver = InputBoardSize(); printf ("Board Size value : %d, %d \n", testReceiver.n1, testReceiver.n2); }
~cpp IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; }
InputRoachJourney() String 했. 한, 포 하 합. 포 , .
~cpp void InputRoachJourney(PSTR journey) { scanf ("%s", journey); }
험합. char [] journey . journey 크 클 킬 . 하, ' 하 ' 하 해 . 큰 해 합. , 해 InputRoachJourney () 함 화 합. Structured Programming 하 , 해 .
Input Implementation 1 했.
~cpp #include <stdio.h> typedef char* PSTR; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; int n2; } IntPair; void Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void Input() { IntPair testReceiver; testReceiver = InputBoardSize(); printf ("Board Size value : %d, %d \n", testReceiver.n1, testReceiver.n2); testReceiver = InputStartRoachPosition(); printf ("Start Position value : %d, %d \n", testReceiver.n1, testReceiver.n2); char testJourney[MAX_JOURNEY_LENGTH]=""; InputRoachJourney(testJourney); printf ("Journey : %s \n", testJourney); } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); }
. 화(?) .
1. 하 함 택
2. . 한 하 .
3. return 해 . .
4. 함 호 return 해 printf cout 확해.
2. . 한 하 .
3. return 해 . .
4. 함 호 return 해 printf cout 확해.
Input 함 main 함 . ScheduedWalk .
~cpp #include <stdio.h> typedef int BOOL; typedef char* PSTR; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; int n2; } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); int main() { InputData inputData; inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); ScheduledWalk(); Output(); return 0; } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); return inputData; } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); }
Input 했.
7. Version 0.5 - Implementation : ScheduledWalk ¶
~cpp BOOL IsFinished() { // Test Code. 했 하 황. char journey = "111122222"; // '111122222' 하 int currentPosition = 1; // 행 (0,1,..) , '1' . 9. assert (IsJourneyEnd(journey, currentPosition) == false); // , '' 합. // return IsJourneyEnd() || IsAllBoardChecked(); return true; } BOOL IsJourneyEnd() { // 했. return true; } BOOL IsAllBoardChecked() { // . return true; }
행해, assert . assert 한 표해. , 하 황 하 Test , Test 통할 하 . 하 한,
- 해 크한.
- Test Code 확해.
- 함 확해.
합.
~cpp BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; }
테트 .
~cpp BOOL IsFinished() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); // return IsJourneyEnd() || IsAllBoardChecked(); return true; }
IsAllBoardChecked 해 테트 , 합.
~cpp BOOL IsFinished() { /* ---------------- IsJourney Test Case ---------------- char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); ---------------- IsJourney Test Case ---------------- */ /* ---------------- IsAllBoardChecked Test Case ---------------- int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); ---------------- IsAllBoardChecked Test Case ---------------- */ char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 10; // for true condition int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } int maxRow = 10; int maxCol = 10; return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) < (UINT)currentPosition; } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[i*maxRow+j] <= 0) return false; } } return true; }
2 34; .
3. .
MoveNext 해 해.
~cpp void MoveNext() { GetMoveVector(); MoveRoach(); IncrementBoardBlockCount(); } void GetMoveVector() { } void MoveRoach() { } void IncrementBoardBlockCount() { }
Prototype 표 텝 . 해 해하 표.~
~cpp #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; int n2; } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void ScheduledWalk(); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); void MoveNext(); void GetMoveVector(); void MoveRoach(); void IncrementBoardBlockCount(); void Output(); void OutputMoveCount(); void OutputBoardStatus(); . . .
해 함 한 .
1. 크 호한 함/ 해 하.
2. 해 하 Test Case 해.
3. Test Case .
4. . Test Case 통 확하.
5. Test Case . ( . 테트 테트 . 한 .)
2. 해 하 Test Case 해.
3. Test Case .
4. . Test Case 통 확하.
5. Test Case . ( . 테트 테트 . 한 .)
GetMoveVector 해 합.
~cpp void MoveNext() { testGetMoveVector(); /* GetMoveVector(journey, currnetPosition); MoveRoach(); IncrementBoardBlockCount(); */ } IntPair GetMoveVector(char* journey, int currentPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextMoveVector.n1 == 0); assert (nextMoveVector.n2 == 1); nextMoveVector = GetMoveVector(journey, 1); assert (nextMoveVector.n1 == 1); assert (nextMoveVector.n2 == 0); nextMoveVector = GetMoveVector(journey, 2); assert (nextMoveVector.n1 == -1); assert (nextMoveVector.n2 == -1); }
MoveRoach 한 Test Case .
~cpp void MoveRoach() { } void testMoveRoach() { IntPair currentRoachPosition; IntPair moveVector; currentRoachPosition.n1 = 0; currentRoachPosition.n2 = 0; // case move type '2': moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 1); // One More Time..~ moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 2); }
Test Case 해 하 한 MoveRoach 해. Requirement 해 판
편 해 하, 해 하 . 한 Test Case 하 하 .
편 해 하, 해 하 . 한 Test Case 하 하 .
~cpp IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = currentRoachPosition.n1 + moveVector.n1; updatedRoachPosition.n2 = currentRoachPosition.n2 + moveVector.n2; return updatedRoachPosition; }해 테트 하 행해.
하 항 판 한 해 Test Case 해.
~cpp void testMoveRoach() { . . . // Checking Boundary Warp. int maxCol = 10; int maxRow = 10; currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; moveVector.n1 = 1; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxCol, maxRow); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); }한 IncrementBoardBlockCount 합.
~cpp void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxRow + roachPosition.n2]++; } void testIncrementBoardBlockCount() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } IntPair currentRoachPosition; currentRoachPosition.n1 = 1; currentRoachPosition.n2 = 1; IncrementBoardBlockCount(board, currentRoachPosition, 10, 10); assert (board[1*10+1] == 1); }
3 52 4 휴;
4 5. ~
4 5. ~
~cpp IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; /* . */ return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "222"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[0*maxRow+0] == 1); }
MoveNext test case pass 하 해 합. 해 GetMoveVector, MoveRoach, IncrementBoardBlockCount 합하 하 .
~cpp IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[0*maxRow+0] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[1*maxRow+1] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[2*maxRow+2] == 1); }
ScheduledWalk (). 테트 했 . ?
{{{~cpp int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { testIsFinished(); testMoveNext(); int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; // '22222' 했. int maxCol = 10; // 판 10, 10 int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; // Roach 포트 0,0 startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); // printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); // 5 합. 한 6 . } ? 파해 하. 1. ScheduledWalk totalMoveCount . 2. totalMoveCount while . 3. while IsFinished() . 4. IsFinished() IsJourneyEnd, IsAllBoardChecked 하 . 하, Test Case . Test Case 해? {{{~cpp void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; // <---- 9. 0 하 8 함. assert (IsJourneyEnd(journey, currentPosition) == false); // <--- true! currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } }}} , 하 IsJourneyEnd 한 함 . 하, 해 합. . Test Case 합. {{{~cpp void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); assert (board[0*maxRow+0] == 1); assert (board[0*maxRow+1] == 1); assert (board[0*maxRow+2] == 1); assert (board[0*maxRow+3] == 1); assert (board[0*maxRow+4] == 1); assert (board[0*maxRow+5] == 1); } }}} === Version 0.6 - Implementation : Output & === ScheduledWalk . Output 하 합. Acceptance TestCase 1 . {{{~cpp #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; // Row . int n2; // Col . } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void testInput(); void InitializeArray(int* board, int maxRow, int maxCol); int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); void testIsJourney(); void testIsAllBoardCheck(); IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol); IntPair GetMoveVector(char* journey, int currentPosition); IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol); void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol); void testGetMoveVector(); void testMoveRoach(); void testIncrementBoardBlockCount(); void testIsFinished(); void testMoveNext(); void testScheduledWalk(); void Output(int totalMoveCount, int* board, int maxRow, int maxCol); void OutputMoveCount(int totalMaxCount); void OutputBoardStatus(int* board, int maxRow, int maxCol); int main() { /* ------------- excute test code ------------- testInput(); testScheduledWalk(); ------------- excute test code ------------- */ InputData inputData = Input(); int maxRow = inputData.boardSize.n1; int maxCol = inputData.boardSize.n2; int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); IntPair startRoachPosition = inputData.roachPosition; char journey[MAX_JOURNEY_LENGTH]=""; strcpy(journey, inputData.journey); int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); delete board; return 0; } void InitializeArray(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); InputEndCode(); return inputData; } void testInput() { InputData inputData; inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardRow, &boardCol); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); } void InputEndCode() { int endCode; scanf("%d", &endCode); } int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { testIsFinished(); testMoveNext(); int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); assert (board[0*maxRow+0] == 1); assert (board[0*maxRow+1] == 1); assert (board[0*maxRow+2] == 1); assert (board[0*maxRow+3] == 1); assert (board[0*maxRow+4] == 1); assert (board[0*maxRow+5] == 1); } void testIsFinished() { // Test Data. char journey[MAX_JOURNEY_LENGTH] = "22"; int currentPosition = 0; int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } int maxRow = 10; int maxCol = 10; int roachPositionRow = 0; int roachPositionCol = 0; assert(IsFinished(journey, currentPosition, board, maxRow, maxCol) == false); } BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol) { testIsJourney(); testIsAllBoardCheck(); return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; } void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == true); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[i*maxRow+j] <= 0) return false; } } return true; } void testIsAllBoardCheck() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); } IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[i*maxRow+j] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[0*maxRow+0] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[1*maxRow+1] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[2*maxRow+2] == 1); } IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextMoveVector.n1 == 0); assert (nextMoveVector.n2 == 1); nextMoveVector = GetMoveVector(journey, 1); assert (nextMoveVector.n1 == 1); assert (nextMoveVector.n2 == 0); nextMoveVector = GetMoveVector(journey, 2); assert (nextMoveVector.n1 == -1); assert (nextMoveVector.n2 == -1); } IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1) % maxRow; updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2) % maxCol; return updatedRoachPosition; } void testMoveRoach() { IntPair currentRoachPosition; IntPair moveVector; currentRoachPosition.n1 = 0; currentRoachPosition.n2 = 0; // case move type '2': moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 1); // One More Time..~ moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 2); // Checking Boundary Warp. int maxCol = 10; int maxRow = 10; currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; moveVector.n1 = 1; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); } void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxRow + roachPosition.n2]++; } void testIncrementBoardBlockCount() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } IntPair currentRoachPosition; currentRoachPosition.n1 = 1; currentRoachPosition.n2 = 1; IncrementBoardBlockCount(board, currentRoachPosition, 10, 10); assert (board[1*10+1] == 1); } void Output(int totalMoveCount, int* board, int maxRow, int maxCol) { OutputMoveCount(totalMoveCount); OutputBoardStatus(board, maxRow, maxCol); } void OutputMoveCount(int totalMaxCount) { printf ("%d\n", totalMaxCount); printf ("\n"); } void OutputBoardStatus(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { printf ("%d ", board[maxRow*i+j]); } printf ("\n"); } } }}} === Test & === . ["RandomWalk2/TestCase"] ["AcceptanceTest"] 하하 행해. {{{~cpp F:\WorkingTemp\ScheduledWalk\Debug>ScheduledWalk 5 5 0 0 22224444346 999 11 2 1 1 1 2 1 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 : 2 1 1 1 1 1 0 0 0 2 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 }}} 호 . . 6 한 1 한 . , 6 한 . 향 해 틴 GetMoveVector . {{{~cpp IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } }}} .. Vector . Roach 키 Position MoveRoach 펴. ( 향 하 함 키 함 합. board update 해 한 향 .) Debugger Trace, break point 할 . 하, 화 해 . {{{~cpp IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1) % maxRow; updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2) % maxCol; return updatedRoachPosition; } }}} 해 퍽 하 ; ( Debugger .) index -1 . Modular 할 . row 1, col 0 row 0, col -1 했 한 row 1, col -1 . -1 해 modular 5 한? 4 . , 1, 4 . main () test code 행 . {{{~cpp int main() { /* ------------- excute test code ------------- testInput(); testScheduledWalk(); ------------- excute test code ------------- */ /* InputData inputData = Input(); int maxRow = inputData.boardSize.n1; int maxCol = inputData.boardSize.n2; int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); IntPair startRoachPosition = inputData.roachPosition; char journey[MAX_JOURNEY_LENGTH]=""; strcpy(journey, inputData.journey); int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); delete board; */ testScheduledWalk(); return 0; } }}} MoveRoach . {{{~cpp IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1); updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2); if (updatedRoachPosition.n1 >= maxRow) updatedRoachPosition.n1 = 0; else if (updatedRoachPosition.n1 < 0) updatedRoachPosition.n1 = maxRow-1; if (updatedRoachPosition.n2 >= maxCol) updatedRoachPosition.n2 = 0; else if (updatedRoachPosition.n2 < 0) updatedRoachPosition.n2 = maxCol-1; return updatedRoachPosition; } }}} assert 해 ok. {{{~cpp 5 5 0 0 22224444346 999 11 2 1 1 1 1 1 0 0 0 2 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 Press any key to continue }}} Test Case 하 해 ok. 테트 해 . ["RandomWalk2/TestCase"] 해 ok. ["RandomWalk2/TestCase2"] Test1,2,3 해 ok. . 테트 ? .. Test4 해 {{{~cpp 5 4 0 0 2224444666 999 10 1 1 1 1 0 0 0 1 0 0 0 1 0 0 0 1 1 1 1 1 }}} . 하 {{{~cpp 10 1 1 1 1 0 0 0 0 1 0 0 1 0 1 0 1 1 1 1 0 }}} ["RandomWalk2/TestCase"] 큰 했. 테트 해 . Quality Assurance 해. 풋 , 행 크 혔. . 한, 키 IncrementBoardBlockCount() . {{{~cpp void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxRow + roachPosition.n2]++; // <--- maxRow maxCol 한. } }}} ? 히 . 2 1 해 . 호.. ( 1 했 해 Search '*maxRow+' 해. 13 ;;) 훈 - 환 함 . -_-; OO Language 1 한 2 클 편합. 해 클 화 킬 . C 하 합. {{{~cpp int _2to1(int row, int col, int maxCol) { return maxCol*row + col; } }}} ["NewTestsForOldBugs"] . === 테트 키 === 테트 (["RandomWalk2/TestCase"], ["RandomWalk2/TestCase2"]) 키 . {{{~cpp #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; // Row . int n2; // Col . } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void InputEndCode(); void testInput(); void InitializeArray(int* board, int maxRow, int maxCol); int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); void testIsJourney(); void testIsAllBoardCheck(); IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol); IntPair GetMoveVector(char* journey, int currentPosition); IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol); void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol); void testGetMoveVector(); void testMoveRoach(); void testIncrementBoardBlockCount(); void testIsFinished(); void testMoveNext(); void testScheduledWalk(); void Output(int totalMoveCount, int* board, int maxRow, int maxCol); void OutputMoveCount(int totalMaxCount); void OutputBoardStatus(int* board, int maxRow, int maxCol); int _2to1(int row, int col, int maxCol) { return maxCol*row+col; } int main() { /* ------------- excute test code ------------- testInput(); testScheduledWalk(); ------------- excute test code ------------- */ InputData inputData = Input(); int maxRow = inputData.boardSize.n1; int maxCol = inputData.boardSize.n2; int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); IntPair startRoachPosition = inputData.roachPosition; char journey[MAX_JOURNEY_LENGTH]=""; strcpy(journey, inputData.journey); int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); delete board; return 0; } void InitializeArray(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); InputEndCode(); return inputData; } void testInput() { InputData inputData; inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardCol, &boardRow); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); } void InputEndCode() { int endCode; scanf("%d", &endCode); } int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { testIsFinished(); testMoveNext(); int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); printf ("total move count : %d \n", totalMoveCount); assert (totalMoveCount == 5); assert (board[_2to1(0,0,maxCol)] == 1); assert (board[_2to1(0,1,maxCol)] == 1); assert (board[_2to1(0,2,maxCol)] == 1); assert (board[_2to1(0,3,maxCol)] == 1); assert (board[_2to1(0,4,maxCol)] == 1); assert (board[_2to1(0,5,maxCol)] == 1); } void testIsFinished() { // Test Data. char journey[MAX_JOURNEY_LENGTH] = "22"; int currentPosition = 0; int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } int maxRow = 10; int maxCol = 10; int roachPositionRow = 0; int roachPositionCol = 0; assert(IsFinished(journey, currentPosition, board, maxRow, maxCol) == false); } BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol) { testIsJourney(); testIsAllBoardCheck(); return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; } void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == true); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[_2to1(i,j,maxCol)] <= 0) return false; } } return true; } void testIsAllBoardCheck() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); } IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { testGetMoveVector(); testMoveRoach(); testIncrementBoardBlockCount(); IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[_2to1(0,0,maxCol)] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[_2to1(1,1,maxCol)] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[_2to1(2,2,maxCol)] == 1); } IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextMoveVector.n1 == 0); assert (nextMoveVector.n2 == 1); nextMoveVector = GetMoveVector(journey, 1); assert (nextMoveVector.n1 == 1); assert (nextMoveVector.n2 == 0); nextMoveVector = GetMoveVector(journey, 2); assert (nextMoveVector.n1 == -1); assert (nextMoveVector.n2 == -1); } IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1); updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2); if (updatedRoachPosition.n1 >= maxRow) updatedRoachPosition.n1 = 0; else if (updatedRoachPosition.n1 < 0) updatedRoachPosition.n1 = maxRow-1; if (updatedRoachPosition.n2 >= maxCol) updatedRoachPosition.n2 = 0; else if (updatedRoachPosition.n2 < 0) updatedRoachPosition.n2 = maxCol-1; return updatedRoachPosition; } void testMoveRoach() { IntPair currentRoachPosition; IntPair moveVector; currentRoachPosition.n1 = 0; currentRoachPosition.n2 = 0; // case move type '2': moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 1); // One More Time..~ moveVector.n1 = 0; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, 10, 10); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 2); // Checking Boundary Warp. int maxCol = 10; int maxRow = 10; currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; moveVector.n1 = 1; moveVector.n2 = 1; currentRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); } void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxCol + roachPosition.n2]++; } void testIncrementBoardBlockCount() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } IntPair currentRoachPosition; currentRoachPosition.n1 = 1; currentRoachPosition.n2 = 1; IncrementBoardBlockCount(board, currentRoachPosition, 10, 10); assert (board[1*10+1] == 1); } void Output(int totalMoveCount, int* board, int maxRow, int maxCol) { OutputMoveCount(totalMoveCount); OutputBoardStatus(board, maxRow, maxCol); } void OutputMoveCount(int totalMaxCount) { printf ("%d\n", totalMaxCount); printf ("\n"); } void OutputBoardStatus(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { printf ("%d ", board[maxCol*i+j]); } printf ("\n"); } } }}} . 테트 히 해. AcceptanceTest 해 화 . === === ==== main.cpp ==== {{{~cpp #include "ScheduledWalkTestCase.h" #include "ScheduledWalk.h" int main() { /* ------------- Acceptance Test ------------- AcceptanceTestAll (); ------------- Acceptance Test ------------- */ AcceptanceTestAll (); // <--- For Acceptance Test UnitTestAll(); // ScheduledWalkMain(); // <--- Main Routine return 0; } void ScheduledWalkMain() { InputData inputData; IntPair startRoachPosition; int maxRow; int maxCol; int* board; char journey[MAX_JOURNEY_LENGTH]=""; int totalMoveCount = 0; inputData = Input(); maxRow = inputData.boardSize.n1; maxCol = inputData.boardSize.n2; board = CreateBoard(maxRow, maxCol); startRoachPosition = inputData.roachPosition; strcpy(journey, inputData.journey); totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); Output(totalMoveCount, board, maxRow, maxCol); DestroyBoard(board); } }}} ==== ScheduledWalk.h ==== {{{~cpp #ifndef _SCHEDULEDWALK_H_ #define _SCHEDULEDWALK_H_ #include <stdio.h> #include <string.h> #include <assert.h> typedef int BOOL; typedef char* PSTR; typedef unsigned int UINT; const int MAX_JOURNEY_LENGTH = 1000; typedef struct __IntegerPair { int n1; // Row . int n2; // Col . } IntPair; typedef struct __InputDataStructure { IntPair boardSize; IntPair roachPosition; char journey[MAX_JOURNEY_LENGTH]; } InputData; InputData Input(); IntPair InputBoardSize(); IntPair InputStartRoachPosition(); void InputRoachJourney(PSTR journey); void InputEndCode(); void InitializeArray(int* board, int maxRow, int maxCol); int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey); BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol); BOOL IsJourneyEnd(PSTR journey, int currentPosition); BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol); IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol); IntPair GetMoveVector(char* journey, int currentPosition); IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol); void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol); void Output(int totalMoveCount, int* board, int maxRow, int maxCol); void OutputMoveCount(int totalMaxCount); void OutputBoardStatus(int* board, int maxRow, int maxCol); int _2to1(int row, int col, int maxCol); int* CreateBoard(int maxRow, int maxCol); void DestroyBoard(int* board); #endif }}} ==== ScheduledWalk.cpp ==== {{{~cpp #include "ScheduledWalk.h" int _2to1(int row, int col, int maxCol) { return maxCol*row+col; } int* CreateBoard(int maxRow, int maxCol) { int* board = new int[maxRow * maxCol]; InitializeArray(board, maxRow, maxCol); return board; } void DestroyBoard(int* board) { delete board; } void InitializeArray(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } } InputData Input() { InputData inputData; inputData.boardSize = InputBoardSize(); inputData.roachPosition = InputStartRoachPosition(); InputRoachJourney(inputData.journey); InputEndCode(); return inputData; } IntPair InputBoardSize() { IntPair size; int boardRow; int boardCol; scanf("%d%d", &boardCol, &boardRow); size.n1 = boardRow; size.n2 = boardCol; return size; } IntPair InputStartRoachPosition() { IntPair position; int startRow; int startCol; scanf("%d%d", &startRow, &startCol); position.n1 = startRow; position.n2 = startCol; return position; } void InputRoachJourney(PSTR journey) { scanf ("%s", journey); } void InputEndCode() { int endCode; scanf("%d", &endCode); } int ScheduledWalk(int* board, int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey) { int currentJourneyPosition = 0; IntPair currentRoachPosition = startRoachPosition; int totalMoveCount = 0; IncrementBoardBlockCount(board, currentRoachPosition, maxRow, maxCol); while (!IsFinished(journey, currentJourneyPosition, board, maxRow, maxCol)) { currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); totalMoveCount++; currentJourneyPosition++; } return totalMoveCount; } BOOL IsFinished(PSTR journey, int currentPosition, int* board, int maxRow, int maxCol) { return IsJourneyEnd(journey, currentPosition) || IsAllBoardChecked(board, maxRow, maxCol); } BOOL IsJourneyEnd(PSTR journey, int currentPosition) { return strlen(journey) <= (UINT)currentPosition; } BOOL IsAllBoardChecked(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { if (board[_2to1(i,j,maxCol)] <= 0) return false; } } return true; } IntPair MoveNext(IntPair currentRoachPosition, PSTR journey, int currentJourneyPosition, int* board, int maxRow, int maxCol) { IntPair updatedRoachPosition; IntPair moveVector = GetMoveVector(journey, currentJourneyPosition); updatedRoachPosition = MoveRoach(currentRoachPosition, moveVector, maxRow, maxCol); IncrementBoardBlockCount(board, updatedRoachPosition, maxRow, maxCol); return updatedRoachPosition; } IntPair GetMoveVector(char* journey, int currentJourneyPosition) { IntPair moveVector; // vector - row move vector, col move vector. int MOVE_VECTOR_PAIR_ROW[8] = {-1, -1, 0, 1, 1, 1, 0, -1}; int MOVE_VECTOR_PAIR_COL[8] = { 0, 1, 1, 1, 0, -1, -1, -1}; int moveVectorPairIndex = journey[currentJourneyPosition] - '0'; moveVector.n1 = MOVE_VECTOR_PAIR_ROW[moveVectorPairIndex]; moveVector.n2 = MOVE_VECTOR_PAIR_COL[moveVectorPairIndex]; return moveVector; } IntPair MoveRoach(IntPair currentRoachPosition, IntPair moveVector, int maxRow, int maxCol) { IntPair updatedRoachPosition; updatedRoachPosition.n1 = (currentRoachPosition.n1 + moveVector.n1); updatedRoachPosition.n2 = (currentRoachPosition.n2 + moveVector.n2); if (updatedRoachPosition.n1 >= maxRow) updatedRoachPosition.n1 = 0; else if (updatedRoachPosition.n1 < 0) updatedRoachPosition.n1 = maxRow-1; if (updatedRoachPosition.n2 >= maxCol) updatedRoachPosition.n2 = 0; else if (updatedRoachPosition.n2 < 0) updatedRoachPosition.n2 = maxCol-1; return updatedRoachPosition; } void IncrementBoardBlockCount(int* board, IntPair roachPosition, int maxRow, int maxCol) { assert (roachPosition.n1 < maxRow); assert (roachPosition.n2 < maxCol); board[roachPosition.n1*maxCol + roachPosition.n2]++; } void Output(int totalMoveCount, int* board, int maxRow, int maxCol) { OutputMoveCount(totalMoveCount); OutputBoardStatus(board, maxRow, maxCol); } void OutputMoveCount(int totalMaxCount) { printf ("%d\n", totalMaxCount); printf ("\n"); } void OutputBoardStatus(int* board, int maxRow, int maxCol) { for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { printf ("%d ", board[maxCol*i+j]); } printf ("\n"); } } }}} ==== ScheduledWalkTestCase.h ==== {{{~cpp #include <assert.h> #include <stdio.h> #include "ScheduledWalk.h" void testScheduledWalk1_1(); void testScheduledWalk1_2(); void testScheduledWalk1_3(); void testScheduledWalk1_4(); void testScheduledWalk2_1(); void testScheduledWalk2_2(); void testScheduledWalk2_3(); void testScheduledWalk2_4(); void testInput(); void testIsFinished(); void testIsJourney(); void testIsAllBoardCheck(); void testMoveNext(); void testGetMoveVector(); void testMoveRoach(); void testIncrementBoardBlockCount(); void testScheduledWalk(); void AcceptanceTestAll(); void UnitTestAll(); void testScheduledWalking(int maxRow, int maxCol, IntPair startRoachPosition, PSTR journey, int expectedTotalMoveCount, int* expectedBoardArray); void excuteTest (void (*func)(void), PSTR message); }}} ==== ScheduledWalkTestCase.cpp ==== {{{~cpp #include "ScheduledWalkTestCase.h" void testInput() { InputData inputData; printf ("Input data ... :"); inputData = Input(); // For Input() Testing.... printf ("Board Size value : %d, %d \n", inputData.boardSize.n1, inputData.boardSize.n2); printf ("Start Position value : %d, %d \n", inputData.roachPosition.n1, inputData.roachPosition.n2); printf ("Journey : %s \n", inputData.journey); } void testScheduledWalk() { char journey[MAX_JOURNEY_LENGTH] = "22222"; int maxCol = 10; int maxRow = 10; IntPair startRoachPosition; startRoachPosition.n1 = 0; startRoachPosition.n2 = 0; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } int totalMoveCount = ScheduledWalk(board, maxRow, maxCol, startRoachPosition, journey); assert (totalMoveCount == 5); assert (board[_2to1(0,0,maxCol)] == 1); assert (board[_2to1(0,1,maxCol)] == 1); assert (board[_2to1(0,2,maxCol)] == 1); assert (board[_2to1(0,3,maxCol)] == 1); assert (board[_2to1(0,4,maxCol)] == 1); assert (board[_2to1(0,5,maxCol)] == 1); } void testIsFinished() { // Test Data. char journey[MAX_JOURNEY_LENGTH] = "22"; int currentPosition = 0; int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 0; } } int maxRow = 10; int maxCol = 10; int roachPositionRow = 0; int roachPositionCol = 0; assert(IsFinished(journey, currentPosition, board, maxRow, maxCol) == false); } void testIsJourney() { char journey[MAX_JOURNEY_LENGTH] = "111122222"; int currentPosition = 1; assert (IsJourneyEnd(journey, currentPosition) == false); currentPosition = 9; assert (IsJourneyEnd(journey, currentPosition) == true); currentPosition = 10; assert (IsJourneyEnd(journey, currentPosition) == true); } void testIsAllBoardCheck() { int board[10*10]; for (int i=0;i<10;i++) { for (int j=0;j<10;j++) { board[i*10+j] = 1; } } assert (IsAllBoardChecked(board, 10, 10) == true); board[0] = 0; assert (IsAllBoardChecked(board, 10, 10) == false); } void testMoveNext() { IntPair currentRoachPosition; char journey[] = "333"; int maxRow = 10; int maxCol = 10; int board[10*10]; for (int i=0;i<maxRow;i++) { for (int j=0;j<maxCol;j++) { board[_2to1(i,j,maxCol)] = 0; } } currentRoachPosition.n1 = 9; currentRoachPosition.n2 = 9; int currentJourneyPosition = 0; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 0); assert (currentRoachPosition.n2 == 0); assert (board[_2to1(0,0,maxCol)] == 1); currentJourneyPosition = 1; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 1); assert (currentRoachPosition.n2 == 1); assert (board[_2to1(1,1,maxCol)] == 1); currentJourneyPosition = 2; currentRoachPosition = MoveNext (currentRoachPosition, journey, currentJourneyPosition, board, maxRow, maxCol); assert (currentRoachPosition.n1 == 2); assert (currentRoachPosition.n2 == 2); assert (board[_2to1(2,2,maxCol)] == 1); } void testGetMoveVector() { char journey[MAX_JOURNEY_LENGTH] = "247"; IntPair nextMoveVector; nextMoveVector = GetMoveVector(journey, 0); assert (nextM