118


SUBMITTED BY: Guest

DATE: Dec. 25, 2013, 3:03 p.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 2068

  1. #include <cstdio>
  2. int maxx, maxy, x, y;
  3. char dir;
  4. bool lost;
  5. char directions[4] = {'N','E','S','W'};
  6. char path[101];
  7. bool grid[51][51];
  8. void walk()
  9. {
  10. for (int i = 0; i < 4; ++i)
  11. if (directions[i] == dir)
  12. {
  13. dir = i;
  14. break;
  15. }
  16. char * str = path;
  17. while (str[0])
  18. {
  19. /*printf("%d %d %c\n", x, y, directions[dir]);*/
  20. switch (str[0])
  21. {
  22. case 'R':
  23. dir = (dir + 1) % 4;
  24. break;
  25. case 'L':
  26. dir = (dir + 3) % 4;
  27. break;
  28. default:
  29. switch (dir)
  30. {
  31. case 0:
  32. if (y == maxy && grid[x][y])
  33. break;
  34. else if (y == maxy)
  35. {
  36. lost = true;
  37. grid[x][y] = true;
  38. return;
  39. }
  40. ++y;
  41. break;
  42. case 1:
  43. if (x == maxx && grid[x][y])
  44. break;
  45. else if (x == maxx)
  46. {
  47. lost = true;
  48. grid[x][y] = true;
  49. return;
  50. }
  51. ++x;
  52. break;
  53. case 2:
  54. if (y == 0 && grid[x][y])
  55. break;
  56. else if (y == 0)
  57. {
  58. lost = true;
  59. grid[x][y] = true;
  60. return;
  61. }
  62. --y;
  63. break;
  64. default:
  65. if (x == 0 && grid[x][y])
  66. break;
  67. else if (x == 0)
  68. {
  69. lost = true;
  70. grid[x][y] = true;
  71. return;
  72. }
  73. --x;
  74. }
  75. }
  76. ++str;
  77. }
  78. }
  79. int main()
  80. {
  81. // create grid
  82. scanf("%d %d", &maxx, &maxy);
  83. for (int i = 0; i < 51; ++i)
  84. for (int j = 0; j < 51; ++j)
  85. grid[i][j] = false;
  86. while (scanf("%d %d %c", &x, &y, &dir) == 3)
  87. {
  88. scanf("%s", path);
  89. lost = false;
  90. walk();
  91. printf("%d %d %c", x, y, directions[dir]);
  92. if (lost)
  93. printf(" LOST");
  94. printf("\n");
  95. }
  96. return 0;
  97. }

comments powered by Disqus