=== 2004 자료구조 === {{{~cpp #include #include #include using namespace std; const int rowMax = 40; const int colMax = 20; int board[rowMax][colMax]; int row, col, ibug, jbug, count; void init(); void input(); void move(); bool existZero(int aBoard[rowMax][colMax]); void printResult(); int main() { init(); input(); move(); printResult(); return 0; } void init() { for (int i = 0; i < rowMax; i++) for (int j =0; j < colMax; j++) board[i][j] = 0; count = 0; } void input() { do { cout << "세로의 타일 개수는?(3개부터 40개)"; cin >> row; cout << "가로의 타일 개수는?(2개부터 20개)"; cin >> col; if (row > 2 && row < 41 && col > 1 && col < 21) break; cout << "세로, 가로의 개수의 범위를 벗어났네요. 다시 입력하세요." << endl; } while(true); do { cout << "벌레의 가로 위치를 정하세요."; cin >> ibug; cout << "벌레의 세로 위치를 정하세요."; cin >> jbug; if (ibug >= 0 && ibug < col && jbug >= 0 && jbug < row) break; cout << "벌레가 타일의 범위를 벗어났네요. 다시 입력하세요." << endl; } while(true); board[ibug][jbug] = 1; } void move() { srand(time(0)); int imove[] = {-1, 0, 1, 1, 1, 0, -1, -1}; int jmove[] = {1, 1, 1, 0, -1, -1, -1, 0}; while(true) { int index = rand() % 8; if (ibug + imove[index] < 0 || ibug + imove[index] > row - 1 || jbug + jmove[index] < 0 || jbug + jmove[index] > col - 1 ) continue; else { board[ibug + imove[index]][jbug + jmove[index]]++; ibug = ibug + imove[index]; jbug = jbug + jmove[index]; count++; } if (!existZero(board)) break; } } bool existZero(int aBoard[rowMax][colMax]) { for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) if (aBoard[i][j] == 0) return true; return false; } void printResult() { cout << "\n각 타일에 이동한 횟수 : \n\n"; for (int i = 0; i < row; i++) { for (int j = 0; j < col; j++) cout << setw(3) << board[i][j]; cout << endl; } cout << "\n총 이동 횟수 : " << count << endl; } }}} === 2005 자료구조 === 2005.3.19 {{{~cpp #include #include #include using namespace std; // 경계 값 기호상수로 정의 #define X_MIN 2 #define X_MAX 20 #define Y_MIN 2 #define Y_MAX 40 #define ITER_LIMIT 50000 // floor의 행(row) 입력 int inputY() { cout << "n값(세로)(3개-40개): "; int n; while (true) { cin >> n; if (n > Y_MIN && n <= Y_MAX) break; cout << "범위 벗어남. n값(세로)(3개-40개): "; } return n; } // floor의 열(col) 입력 int inputX() { cout << "m값(가로)(2개-20개): "; int m; while (true) { cin >> m; if (m >= X_MIN && m <= X_MAX) break; cout << "범위 벗어남. m값(가로)(2개-20개): "; } return m; } // 벌레의 x위치 입력 int inputPosX(int m) { cout << "X 위치: "; int posX; while(true) { cin >> posX; if (posX >= 0 && posX < m) break; cout << "X 위치: "; } return posX; } // 벌레의 y위치 입력 int inputPosY(int n) { cout << "Y 위치: "; int posY; while(true) { cin >> posY; if (posY >= 0 && posY < n) break; cout << "Y 위치: "; } return posY; } // 입력받은 row, col을 바탕으로 floor 생성 int** makeRoom(int n, int m) { int **room = new int*[n]; for(int i = 0; i < n; i++) room[i] = new int[m]; return room; } // floor의 각 tile을 0으로 초기화 void initToZero(int **aRoom, int n, int m) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { aRoom[i][j] = 0; } } } // 벌레가 모든 cell을 움직였는지 검사 bool isAllMoved(int **aRoom, int n, int m) { for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if (aRoom[i][j] == 0) return false; } } return true; } // 벌레가 cell을 벗어나는지 검사 bool isBlocked(int n, int m, int ibug, int jbug) { if (ibug < 0 || ibug >= n || jbug < 0 || jbug >= m) return true; return false; } // 벌레가 random으로 결정된 방향으로 이동 int moveRoach(int **aRoom, int n, int m, int ibug, int jbug) { int imove[8] = {-1, 0, 1, 1, 1, 0, -1, -1}; int jmove[8] = {1, 1, 1, 0, -1, -1, -1, 0}; aRoom[ibug][jbug] = 1; int count = 0; while(true) { int dir = rand() % 8; if(!isBlocked(n, m, ibug+imove[dir], jbug+jmove[dir])) { ibug += imove[dir]; jbug += jmove[dir]; aRoom[ibug][jbug]++; count++; if (isAllMoved(aRoom, n, m)) break; } if (count >= ITER_LIMIT) return -1; } return count; } // 각 cell로 이동한 횟수 출력 void printRoomCount(int **aRoom, int n, int m) { cout << "\n(2)The final count array:" << endl; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { cout << setw(3) << aRoom[i][j]; } cout << endl; } cout << endl; } // 전체 이동 횟수 출력 void printNumOfMove(int count) { if (count == -1) cout << "\n(1)최대 이동 횟수 초과되어 이동 종료" << endl; else cout << "\n(1)The total number of legal moves: " << count << endl; } // floor 제거(메모리 해제) void destroyRoom(int **aRoom, int n) { for(int i = 0; i < n; i++) delete []aRoom[i]; } int main() { // 시드 설정 for random function srand(time(0)); // floor의 크기 입력 int n = inputY(); int m = inputX(); // 벌레의 위치 입력 int ibug = inputPosX(m); int jbug = inputPosY(n); // floor 생성하고 각 cell을 0으로 설정. int **room = makeRoom(n, m); initToZero(room, n, m); // 벌레 이동 int count = moveRoach(room, n, m, ibug, jbug); // 움직인 횟수와 각 cell로 이동 횟수 출력 printNumOfMove(count); printRoomCount(room, n, m); // floor 제거(메모리 해제) destroyRoom(room, n); return 0; } }}} - 입력 받는 부분의 소스가 너무 유사하다. ---- RandomWalk