fork download
  1. /**********************************************************************************/
  2. /* Problem: a314 "C 導覽型機器人" from 2011 NPSC 國中組初賽 */
  3. /* Language: CPP (1296 Bytes) */
  4. /* Result: AC(4ms, 376KB) judge by this@ZeroJudge */
  5. /* Author: lfs92002 at 2011-12-06 12:34:06 */
  6. /**********************************************************************************/
  7.  
  8.  
  9. #include<iostream>
  10. using namespace std;
  11. int main()
  12. {
  13. int T;
  14. short map[12][12],h,w,x,y;
  15. short start_x,start_y,sum,tx,ty,n,tn;
  16. bool _b;
  17. cin>>T;
  18. while(T--)
  19. {
  20. for(y=0;y<=11;y++)
  21. for(x=0;x<=11;x++)
  22. map[y][x]=-1;
  23. cin>>h>>w;
  24. for(y=1;y<=h;y++)
  25. for(x=1;x<=w;x++)
  26. cin>>map[y][x];
  27. for(y=1;y<=h;y++)//Find 0
  28. {
  29. for(x=1;x<=w;x++)
  30. if(map[y][x]==0)
  31. {
  32. start_x=x;
  33. start_y=y;
  34. }
  35. }
  36. _b=true;
  37. n=1;tn=0;//tn 步
  38. while(_b)
  39. {
  40. if(map[start_y-1][start_x]==n)//UP
  41. {
  42. tn=0;
  43. while(map[start_y-1][start_x]==n)
  44. {
  45. start_y--;
  46. tn++;
  47. n++;
  48. }
  49. cout<<'N'<<tn;
  50.  
  51. }
  52. else if(map[start_y+1][start_x]==n)//DOWN
  53. {
  54. tn=0;
  55. while(map[start_y+1][start_x]==n)
  56. {
  57. start_y++;
  58. tn++;
  59. n++;
  60. }
  61. cout<<'S'<<tn;
  62. }
  63. else if(map[start_y][start_x+1]==n)//RIGHT
  64. {
  65. tn=0;
  66. while(map[start_y][start_x+1]==n)
  67. {
  68. start_x++;
  69. tn++;
  70. n++;
  71. }
  72. cout<<'E'<<tn;
  73. }
  74. else if(map[start_y][start_x-1]==n)//RIGHT
  75. {
  76. tn=0;
  77. while(map[start_y][start_x-1]==n)
  78. {
  79. start_x--;
  80. tn++;
  81. n++;
  82. }
  83. cout<<'W'<<tn;
  84. }
  85. else
  86. {
  87. _b=false;
  88. }
  89. }
  90. cout<<endl;
  91. }
  92. return 0;
  93. }
  94.  
Success #stdin #stdout 0s 3300KB
stdin
2
3 6
−1 −1 −1 −1 −1 −1
−1 3 2 1 0 −1
−1 −1 −1 −1 −1 −1
4 5
−1 3 4 5 6
−1 2 −1 −1 −1
−1 1 −1 −1 −1
−1 0 −1 −1 −1
stdout