Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int dr[] = {-1,-2,-2,-1,+1,+2,+2,+1};
  5. int dc[] = {-2,-1,+1,+2,+2,+1,-1,-2};
  6.  
  7.  
  8. int sr,sc,cr,cc,tc,tr;
  9.  
  10. int main(){
  11. int t,r,c;
  12. char temp;
  13. scanf("%d",&t);
  14. //freopen("output.txt", "w", stdout);
  15. for(int i = 0; i < t; i++){
  16. queue<int> rqueue;
  17. queue<int> cqueue;
  18. //queue<char> moves;
  19. int moves = 0;
  20. int nodes_left_in_layer = 1;
  21. int nodes_in_next_layer = 0;
  22. char move[] = {'A','B','C','D','E','F','G','H'};
  23. bool reached_end = false;
  24.  
  25. scanf("%d %d",&r,&c);
  26. char grid[r][c];
  27. int visited[r][c] = {0};
  28. int path[r][c];
  29.  
  30. for(int j = 0; j < r; j++){
  31. for(int k = 0; k < c; k++){
  32. cin >> temp;
  33. if(temp == 'K'){
  34. sr = j;
  35. sc = k;
  36. }
  37. grid[j][k] = temp;
  38. }
  39. }
  40.  
  41. rqueue.push(sr);
  42. cqueue.push(sc);
  43. visited[sr][sc] = 1;
  44.  
  45. while(!rqueue.empty()){
  46. cr = rqueue.front();
  47. cc = cqueue.front();
  48. rqueue.pop();
  49. cqueue.pop();
  50. //printf("%c\n",grid[cr][cc]);
  51. if(grid[cr][cc] == 'F'){
  52. reached_end = true;
  53. break;
  54. }
  55.  
  56. for(int l = 0; l < 8; l++){
  57.  
  58. tr = cr + dr[l];
  59. tc = cc + dc[l];
  60. //
  61. if(tr < 0 || tc < 0){
  62. //printf("Pasok sa 1st\n");
  63. continue;
  64. }
  65. else if(tr >= r || tc >= c){
  66. //printf("Pasok sa 2nd\n");
  67. continue;
  68. }
  69. else if(visited[tr][tc] == 1){
  70. //printf("Pasok sa 3rd\n");
  71. continue;
  72. }
  73. else if(grid[tr][tc] == 'X'){
  74. //printf("Pasok sa 4th\n");
  75. continue;
  76. }
  77. else{
  78. //printf("Pasok sa 5th\n");
  79. //printf("%d Row : %d Column : %d\n",l,tr,tc);
  80. rqueue.push(tr);
  81. cqueue.push(tc);
  82. visited[tr][tc] = 1;
  83. path[tr][tc] = l;
  84. nodes_in_next_layer ++;
  85. }
  86. }
  87.  
  88. nodes_left_in_layer--;
  89. if(nodes_left_in_layer == 0){
  90. nodes_left_in_layer = nodes_in_next_layer;
  91. nodes_in_next_layer = 0;
  92. moves++;
  93. }
  94. }
  95.  
  96. if(reached_end){
  97. vector<char> tiles;
  98. printf("Whinny\n");
  99. //printf("%d %d",cr,cc);
  100. for(int i = 0; i < moves; i++){
  101. int m = path[cr][cc];
  102. tiles.push_back(move[m]);
  103.  
  104. cr -= dr[m];
  105. cc -= dc[m];
  106. }
  107. reverse(tiles.begin(),tiles.end());
  108. for(int i = 0; i < tiles.size();i++){
  109. printf("%c",tiles[i]);
  110. }
  111. printf("\n");
  112. }
  113. else{
  114. printf("Neigh\n");
  115. }
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement