Guest User

Untitled

a guest
Oct 17th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. //로봇 청소기
  2. #include <cstdio>
  3. #include <vector>
  4. using namespace std;
  5. int xx[4] = { 0,1,0,-1 };
  6. int yy[4] = { -1,0,1,0 };
  7. int map[50][50];
  8. struct info {
  9. int y;
  10. int x;
  11. int d; // 0인 경우에는 북쪽을, 1인 경우에는 동쪽을, 2인 경우에는 남쪽을, 3인 경우에는 서쪽
  12. bool stop = false;
  13. void set(int yy, int xx, int dir) {
  14. y = yy;
  15. x = xx;
  16. d = dir;
  17. }
  18. void turn() { //회전
  19. if (d == 0)
  20. d = 3;
  21. else
  22. d -= 1;
  23. }
  24. void backturn() { //회전 복구
  25. if (d == 3)
  26. d = 0;
  27. else
  28. d += 1;
  29. }
  30. void move() {
  31. y += yy[d];
  32. x += xx[d];
  33. }
  34. void backmove() { //움직임 복구
  35. y -= yy[d];
  36. x -= xx[d];
  37. }
  38. void cleanup() {
  39. map[y][x] = 2;
  40. }
  41. };
  42. int X, Y, a, b, c, ans = 0;
  43. info robot;
  44. int main() {
  45. scanf("%d %d", &Y, &X);
  46. scanf("%d %d %d", &a, &b, &c);
  47. robot.set(a, b, c);
  48. for (int i = 0; i < Y; i++) {
  49. for (int j = 0; j < X; j++) {
  50. scanf("%d", &map[i][j]);
  51. }
  52. }
  53. /*printf("-----------\n");
  54. for (int i = 0; i < Y; i++) {
  55. for (int j = 0; j < X; j++) {
  56. printf("%d ", map[i][j]);
  57. }printf("\n");
  58. }*/
  59. int cnt = 0;
  60. while (!robot.stop) {
  61. if (cnt == 0)
  62. robot.cleanup(); //현재 위치를 청소한다
  63. robot.turn(); //현재 위치에서 현재 방향을 기준으로 왼쪽방향부터 차례대로 탐색을 진행한다.
  64. robot.move();
  65. if (map[robot.y][robot.x] == 0) { //왼쪽 방향에 아직 청소하지 않은 공간이 존재한다면
  66. cnt = 0; //그 방향으로 회전한 다음 한 칸을 전진하고 1번부터 진행한다.
  67. }
  68. else {
  69. if (cnt == 4) { //네 방향 모두 청소가 이미 되어있거나 벽인 경우에는,
  70. robot.backmove();
  71. robot.backturn();
  72. robot.backmove(); //바라보는 방향을 유지한 채로 한 칸 후진을 하고 2번으로 돌아간다
  73. if (map[robot.y][robot.x] == 1) //, 뒤쪽 방향이 벽이라 후진도 할 수 없는 경우에는 작동을 멈춘다.
  74. robot.stop = true;
  75. else
  76. cnt = 0;
  77. }
  78. else { //왼쪽 방향에 청소할 공간이 없다면, 그 방향으로 회전하고 2번으로 돌아간다
  79. cnt++;
  80. robot.backmove();
  81. }
  82. }
  83. }
  84. for (int i = 0; i < Y; i++) {
  85. for (int j = 0; j < X; j++) {
  86. if (map[i][j] == 2)
  87. ans++;
  88. }
  89. }
  90. printf("%d\n", ans);
  91. return 0;
  92. }
Add Comment
Please, Sign In to add comment