U E D R , A S I H C RSS

Scheduled Walk/석천


1.

데블2002 StructuredProgramming 는 모. , . 듯. , 면. ^^;

Spec Test Case 는 RandomWalk2 .

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() {

}

데블2002 HIPO 뼈대를 .
며, HIPO . (냐. Top-Down Design . 만, 문 램 디, 는데 . )

(Hierarchy Input-Process-Output) . ()



6. Version 0.4 - Implementation : Input

10 - 1 55. 등등;
뼈대 . (, 만, 는데 )

럼. .

는 모듈 부 . 모듈부 . DFD를 만, . , . HIPO Argument 를 는 부 . ( . 만, .~)


.

  1. - -> . . ( 독립 . UnitTest )
  2. Depth-Module First. -> , 모듈부 . (데, 면 Bottom-Up .. . .)
. Input 부 . ( ) InputBoardSize, InputStartRoachPosition, InputRoachJourney InputBoardSize.

~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);
}

Input . 럼, Input InputStartRoachPosition(), InputRoachJourney() .

~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 등 .

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

모듈 - ScheduledWalk() . Depth First . TestDrivenDevelopment .
( , TestFirstProgramming, UnitTest)

모듈 . IsFinished() 목. ( IsJourneyEnd IsAllBoardChecked는 부)

~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 를 . 면,

  1. 모듈 .
  2. Test Code 를 .
  3. 모듈 .

만들 .

.
~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 를 . ( . . .)

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를 면 됩.
~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. ~

MoveNext Test Case를 . 러면 , . MoveNext GetMoveVector MoveRoach, IncrementBoardBlockCount .

~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
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:27:59
Processing time 0.2012 sec