Advertisement
Guest User

Untitled

a guest
Jan 26th, 2020
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 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. int sr,sc,cr,cc,tc,tr;
  8.  
  9. int main(){
  10. // freopen("in.txt","rt",stdin);
  11. // freopen("outDarr.txt","wt",stdout);
  12. int t,r,c;
  13. char temp;
  14. scanf("%d",&t);
  15. //freopen("output.txt", "w", stdout);
  16. for(int i = 0; i < t; i++){
  17. queue<int> rqueue;
  18. queue<int> cqueue;
  19. //queue<char> moves;
  20. int moves = 0;
  21. char move[] = {'A','B','C','D','E','F','G','H'};
  22. bool reached_end = false;
  23.  
  24. scanf("%d %d",&r,&c);
  25. char grid[r][c];
  26. int visited[r][c];
  27. int path[r][c];
  28. fill(&path[0][0], &path[0][0] + sizeof(path) / sizeof(path[0][0]), -1);
  29.  
  30. for(int j = 0; j < r; j++){
  31. string line;
  32. cin >> line;
  33. for(int k = 0; k < line.length(); k++){
  34. if(line[k] == 'K'){
  35. sr = j;
  36. sc = k;
  37. }
  38. grid[j][k] = line[k];
  39. }
  40. }
  41.  
  42. rqueue.push(sr);
  43. cqueue.push(sc);
  44. memset(visited,0,sizeof(visited));
  45. visited[sr][sc] = 1;
  46. while(!rqueue.empty()){
  47. cr = rqueue.front();
  48. cc = cqueue.front();
  49. rqueue.pop();
  50. cqueue.pop();
  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. continue;
  63. }
  64. else if(tr >= r || tc >= c){
  65. continue;
  66. }
  67. else if(visited[tr][tc] == 1){
  68. continue;
  69. }
  70. else if(grid[tr][tc] == 'X'){
  71. continue;
  72. }
  73. else{
  74. rqueue.push(tr);
  75. cqueue.push(tc);
  76. visited[tr][tc] = 1;
  77. path[tr][tc] = l;
  78. }
  79. }
  80. }
  81.  
  82. if(reached_end){
  83. vector<char> tiles;
  84. printf("Whinny\n");
  85. while(true){
  86. int m = path[cr][cc];
  87.  
  88. if(m == -1)
  89. break;
  90. tiles.push_back(move[m]);
  91.  
  92. cr -= dr[m];
  93. cc -= dc[m];
  94.  
  95. }
  96. reverse(tiles.begin(),tiles.end());
  97. for(int i = 0; i < tiles.size();i++){
  98. printf("%c",tiles[i]);
  99. }
  100. printf("\n");
  101. }
  102. else{
  103. printf("Neigh\n");
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement