-
로봇 청소기 삼성 기출문제, 시뮬레이션문젠데 지난번에는 BFS써서 풀었었던 기억이있다.
BFS말고, 그냥 문제에 주어진로직대로 풀어보는 방식을 사용해서 문제를 재구성했다.
#include<cstdio>// using namespace std; struct robot {//0북1동 2남, 3서 int x = 0, y = 0, dir = 0, cnt = 1; robot(int x, int y, int dir) { this->x = x; this->y = y; this->dir = dir; } }; int dx[] = { -1,0,1,0 }; int dy[] = { 0,1,0,-1 }; robot rb = robot(0,0,0); int r, c, X, Y, D; int map[51][51]; bool dist[51][51]; bool first() { int x = rb.x; int y = rb.y; int d = rb.dir; d -= 1; if (d < 0) d = 3; // printf("%d\n", d); int nx = rb.x + dx[d]; int ny = rb.y + dy[d]; if (dist[nx][ny] == false && map[nx][ny] == 0) { dist[nx][ny] = true; rb.x = nx; rb.y = ny; rb.dir = d; rb.cnt++; // printf("%d %d %d true\n", nx, ny, d); return true; } else { rb.dir = d; return false; } } void cleaning() { while (true) { bool no1 = false; if (first()) { no1 = true; continue; } else { for (int i = 0; i < 3; i++) { no1 = first(); if (no1) break; } } if (no1) { continue; } bool no2 = false; if (!no1) { int d = 0; int nx; int ny; switch (rb.dir) { case 0: nx = rb.x + dx[2]; ny = rb.y + dy[2]; if (map[nx][ny] != 1) { rb.x = nx; rb.y = ny; no2 = true; } break; case 1: nx = rb.x + dx[3]; ny = rb.y + dy[3]; if (map[nx][ny] != 1) { rb.x = nx; rb.y = ny; no2 = true; } break; case 2: nx = rb.x + dx[0]; ny = rb.y + dy[0]; if (map[nx][ny] != 1) { rb.x = nx; rb.y = ny; no2 = true; } break; case 3: nx = rb.x + dx[1]; ny = rb.y + dy[1]; if (map[nx][ny] != 1) { rb.x = nx; rb.y = ny; no2 = true; } break; } } if (!no2 && !no1) break; } } int main() { scanf("%d %d %d %d %d", &r, &c, &X, &Y, &D); rb = robot(X, Y, D); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { scanf("%d", &map[i][j]); } } dist[rb.x][rb.y] = true; cleaning(); /*for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { if (dist[i][j] == true) printf("1 "); else printf("0 "); } printf("\n"); }*/ printf("%d", rb.cnt); }
디버깅하는 과정에서 귀찮은 부분이 좀많았지만, 그래도 로직자체가 굉장히 간단한 문제인지라 어렵지않았음