fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static boolean graph[][];
  11.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. Scanner sc = new Scanner(System.in);
  15. int te=sc.nextInt();
  16. for(int c=1;c<=te;c++){
  17. int r=sc.nextInt();
  18. int co=sc.nextInt();
  19. char d = 'N';
  20. graph = new boolean[r+2][co+2];
  21.  
  22. sc.nextLine();
  23. for(int i=1;i<=r;i++){
  24. String s=sc.nextLine();
  25. //System.out.println(s);
  26. for(int j=0;j<s.length();j++){
  27. if(s.charAt(j)==' ')
  28. graph[i][j+1]=true;
  29. }
  30. }
  31.  
  32. int y=sc.nextInt();
  33. int x=sc.nextInt();
  34.  
  35. boolean t=false;
  36. while(r>0){
  37. String s = sc.nextLine();
  38. for(int i=0;i<s.length();i++){
  39. if(s.charAt(i)=='Q'){
  40. t = true;
  41. break;
  42. }else if(s.charAt(i)=='R'){
  43. if(d=='N')
  44. d='E';
  45. else if(d=='E')
  46. d='S';
  47. else if(d=='S')
  48. d='W';
  49. else if(d=='W')
  50. d='N';
  51. }else if(s.charAt(i)=='L'){
  52. if(d=='N')
  53. d='W';
  54. else if(d=='E')
  55. d='N';
  56. else if(d=='S')
  57. d='E';
  58. else if(d=='W')
  59. d='S';
  60. }else if(s.charAt(i)=='F'){
  61. if(d=='N'){
  62. if(graph[y-1][x]==true)
  63. y--;
  64. }else if(d=='E'){
  65. if(graph[y][x+1]==true)
  66. x++;
  67. }else if(d=='S'){
  68. if(graph[y+1][x]==true)
  69. y++;
  70. }else if(d=='W'){
  71. if(graph[y][x-1]==true)
  72. x--;
  73. }
  74. }
  75. }
  76. if(t==true)
  77. break;
  78. }
  79.  
  80. System.out.println(y+" "+x+" "+d);
  81. System.out.println();
  82. }
  83. }
  84. }
Success #stdin #stdout 0.09s 380672KB
stdin
2

7 8
********
* * * **
* *    *
* * ** *
* * *  *
*   * **
********
2 4
RRFLF
FFFR
F
F
Q

7 8
********
* * * **
* *    *
* * ** *
* * *  *
*   * **
********
2 4
RRFLFF FFR
FF
RFFQ
stdout
5 7 S

5 6 W