2004 ìž�료구조 ¶
~cpp
#include <iostream>
#include <ctime>
#include <iomanip>
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 <iostream>
#include <ctime>
#include <iomanip>
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;
}
- ìž…ë ¥ 받는 부분ì�˜ 소스가 너무 ìœ ì‚¬í•˜ë‹¤.










