fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <stdio.h>
  4. using namespace std;
  5.  
  6. int** array_init(const int&, const int&);
  7. void change_orientation(char&, const char&);
  8. bool go_forward(int **, const int&, const int&, int&, int&, char&, bool&);
  9. void show_coordinate(int&, int&, char&, bool&);
  10.  
  11. int main()
  12. {
  13. int range_x, range_y; // upper-right coordinates of the rectangular world
  14. int x, y; // initial coordinate of robot
  15. char o; // initail orientation of robot
  16. string instr; // instruction
  17. int **scent = 0; // robot drop coordinates
  18.  
  19. cin >> range_x >> range_y;
  20. scent = array_init(range_x, range_y);
  21.  
  22. while( cin >> x >> y >> o >> instr )
  23. {
  24. bool isdrop = false;
  25. for(string::size_type i = 0; i != instr.size(); ++i)
  26. {
  27. if( instr[i] == 'F' )
  28. {
  29. if( go_forward(scent, range_x, range_y, x, y, o, isdrop) == false )
  30. break;
  31. }
  32. else
  33. change_orientation(o, instr[i]);
  34. }
  35. show_coordinate(x, y, o, isdrop);
  36. }
  37. }
  38.  
  39. int** array_init(const int &x, const int &y)
  40. {
  41. int **a = new int*[x];
  42. for(int i = 0; i <= x; ++i)
  43. a[i] = new int[y];
  44.  
  45. for(int i = 0; i <= x; ++i)
  46. for(int j = 0; j <= y; ++j)
  47. a[i][j] = 0;
  48. return a;
  49. }
  50. void change_orientation(char &o, const char &instr)
  51. {
  52. char orientation[4] = {'N', 'E', 'S', 'W'};
  53.  
  54. for(int i = 0; i != 4; ++i)
  55. {
  56. if( orientation[i] == o)
  57. {
  58. if( instr == 'R' )
  59. o = orientation[ (i+1)%4 ];
  60. else
  61. o = orientation[ (i+3)%4 ];
  62. break;
  63. }
  64. }
  65. }
  66. bool go_forward(int **a, const int &range_x, const int &range_y, int &x, int &y, char &o, bool &isdrop)
  67. {
  68. switch(o)
  69. {
  70. case 'N':
  71. if( y+1 > range_y ) // 超出範圍?
  72. {
  73. if( a[x][y] == 0 ) // 標記?
  74. {
  75. a[x][y] = 1;
  76. isdrop = true;
  77. return false;
  78. }
  79. else // 有標記 -> nop
  80. return true;
  81. }
  82. else
  83. {
  84. y++;
  85. return true;
  86. }
  87. case 'E':
  88. if( x+1 > range_x )
  89. {
  90. if( a[x][y] == 0 )
  91. {
  92. a[x][y] = 1;
  93. isdrop = true;
  94. return false;
  95. }
  96. else
  97. return true;
  98. }
  99. else
  100. {
  101. x++;
  102. return true;
  103. }
  104. case 'S':
  105. if( y-1 < 0 )
  106. {
  107. if( a[x][y] == 0 )
  108. {
  109. a[x][y] = 1;
  110. isdrop = true;
  111. return false;
  112. }
  113. else
  114. return true;
  115. }
  116. else
  117. {
  118. y--;
  119. return true;
  120. }
  121. case 'W':
  122. if( x-1 < 0 )
  123. {
  124. if( a[x][y] == 0 )
  125. {
  126. a[x][y] = 1;
  127. isdrop = true;
  128. return false;
  129. }
  130. else
  131. return true;
  132. }
  133. else
  134. {
  135. x--;
  136. return true;
  137. }
  138. default:
  139. return false;
  140. }
  141. }
  142. void show_coordinate(int &x, int &y, char &o, bool &isdrop)
  143. {
  144. cout << x << ' ' << y << ' ' << o;
  145. if( isdrop )
  146. cout << " LOST";
  147. cout << endl;
  148. }
Success #stdin #stdout 0.01s 2996KB
stdin
5 3
1 1 E
RFRFRFRF
3 2 N
FRRFLLFFRRFLL
0 3 W
LLFFFLFLFL
stdout
1 1 E
3 3 N LOST
2 3 S