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;
}
- .










