#include <iostream>
#define MAP_SIZE 60
using namespace std;
int n, m, startRow, startCol, startDir;
int MAP[MAP_SIZE][MAP_SIZE];
int visited[MAP_SIZE][MAP_SIZE];
// 북(상), 동(우), 남(하), 서(좌)
int dr[] = {-1, 0, 1, 0};
int dc[] = {0, 1, 0, -1};
int changeDir(int now_dir)
{
if (now_dir != 0)
now_dir--;
else
now_dir = 3;
return now_dir;
}
void INPUT()
{
cin >> n >> m >> startRow >> startCol >> startDir;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> MAP[i][j];
}
void Simulation()
{
int area = 1;
int nowRow = startRow;
int nowCol = startCol;
int nowDir = startDir;
// 후진을 위한 방향 저장
int prepDir = 0;
visited[startRow][startCol] = 1;
int flag4Dir = 0;
int flagBack = 0;
while (area < 2500)
{
// 현재방향 기준 왼쪽방향으로 간 적이 없다면 전진
int tmpDir = changeDir(nowDir);
int tmpRow = nowRow + dr[tmpDir];
int tmpCol = nowCol + dc[tmpDir];
if (visited[tmpRow][tmpCol] == 0 && MAP[tmpRow][tmpCol] != 1)
{
prepDir = tmpDir;
nowRow = tmpRow;
nowCol = tmpCol;
nowDir = tmpDir;
visited[tmpRow][tmpCol] = 1;
flag4Dir = 0;
area++;
continue;
}
// 4방향 모두 확인
else
{
nowDir = tmpDir;
flag4Dir++;
}
// 모두 확인하였으나 전진하지 못하는 경우
if (flag4Dir >= 4) {
flag4Dir = 0;
tmpRow = nowRow - dr[prepDir];
tmpCol = nowCol - dc[prepDir];
// 후진이 불가능한 경우
if (MAP[tmpRow][tmpCol] == 1)
{
cout << area << endl;
return;
}
// 후진이 가능한 경우
else {
nowRow = tmpRow;
nowCol = tmpCol;
}
}
}
cout << area << endl;
}
int main()
{
INPUT();
Simulation();
return 0;
}
📌 memo 😊
덕분에 좋은 정보 얻어갑니다, 감사합니다.