No older revisions available
No older revisions available
소스 ¶
~cpp
#include <iostream>
using namespace std;
class Inputer{
public:
int col, row; // 판 크기
int startx, starty; // 바퀴 시작 좌표
char journey[30]; // 여정
Inputer(){
cout<<"판의 크기 입력하시오: (x y)";
cin>> col >> row;
cout<<"바퀴벌레의 초기 위치를 입력하시오: (x y)";
cin >> startx >> starty;
cout << "여정을 입력하십시오. : ";
cin >> journey;
}
};
class Board{
public:
int* BoardArray;
int maxCol, maxRow;
Board (int col, int row)
{
maxCol = col;
maxRow = row;
BoardArray = new int[row * col + row];
for (int i = 0 ; i < col ; i++)
for (int j = 0 ; j < row ; j++)
BoardArray[i * row + j] = 0;
}
void start(int x, int y, char* journey)
{
int move[8][2]={{0, -1}, {1, -1}, {1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}};
char CurrentMove;
for (int check, direction, i=0 ; check != maxRow * maxCol && CurrentMove != '' ; i++)
{
if (i != 0)
{
x+=move[direction][0];
y+=move[direction][1];
}
if (x == -1)
x = maxCol - 1;
if (y == -1)
y = maxRow - 1;
if (x == maxCol)
x = 0;
if (y == maxRow)
y = 0;
BoardArray[y * maxRow + x]++;
check=0;
for (int j = 0 ; j < maxRow * maxCol ; j++) // 빈 셀이 있는지 확인
if (BoardArray[j] != 0)
check++;
CurrentMove = journey[i];
direction = (CurrentMove - '0') % 8; // 다음 이동 방향 받기
}
}
void print()
{
int count = 0;
for (int i = 0 ; i < maxCol ; i++)
{
for (int j = 0 ; j < maxRow ; j++)
{
cout.width(4);
cout << BoardArray[i * maxRow + j];
count += BoardArray[i * maxRow + j];
}
cout << endl;
}
cout << "총 이동횟수 : " << count - 1 << endl;
}
};
int main()
{
Inputer inputer;
Board* board = new Board(inputer.row, inputer.col);
board->start(inputer.startx, inputer.starty, inputer.journey);
board->print();
return 0;
}