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.2046 sec