Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int dr[] = {-1,-2,-2,-1,+1,+2,+2,+1};
- int dc[] = {-2,-1,+1,+2,+2,+1,-1,-2};
- int sr,sc,cr,cc,tc,tr;
- int main(){
- int t,r,c;
- char temp;
- scanf("%d",&t);
- //freopen("output.txt", "w", stdout);
- for(int i = 0; i < t; i++){
- queue<int> rqueue;
- queue<int> cqueue;
- //queue<char> moves;
- int moves = 0;
- int nodes_left_in_layer = 1;
- int nodes_in_next_layer = 0;
- char move[] = {'A','B','C','D','E','F','G','H'};
- bool reached_end = false;
- scanf("%d %d",&r,&c);
- char grid[r][c];
- int visited[r][c] = {0};
- int path[r][c];
- for(int j = 0; j < r; j++){
- for(int k = 0; k < c; k++){
- cin >> temp;
- if(temp == 'K'){
- sr = j;
- sc = k;
- }
- grid[j][k] = temp;
- }
- }
- rqueue.push(sr);
- cqueue.push(sc);
- visited[sr][sc] = 1;
- while(!rqueue.empty()){
- cr = rqueue.front();
- cc = cqueue.front();
- rqueue.pop();
- cqueue.pop();
- //printf("%c\n",grid[cr][cc]);
- if(grid[cr][cc] == 'F'){
- reached_end = true;
- break;
- }
- for(int l = 0; l < 8; l++){
- tr = cr + dr[l];
- tc = cc + dc[l];
- //
- if(tr < 0 || tc < 0){
- //printf("Pasok sa 1st\n");
- continue;
- }
- else if(tr >= r || tc >= c){
- //printf("Pasok sa 2nd\n");
- continue;
- }
- else if(visited[tr][tc] == 1){
- //printf("Pasok sa 3rd\n");
- continue;
- }
- else if(grid[tr][tc] == 'X'){
- //printf("Pasok sa 4th\n");
- continue;
- }
- else{
- //printf("Pasok sa 5th\n");
- //printf("%d Row : %d Column : %d\n",l,tr,tc);
- rqueue.push(tr);
- cqueue.push(tc);
- visited[tr][tc] = 1;
- path[tr][tc] = l;
- nodes_in_next_layer ++;
- }
- }
- nodes_left_in_layer--;
- if(nodes_left_in_layer == 0){
- nodes_left_in_layer = nodes_in_next_layer;
- nodes_in_next_layer = 0;
- moves++;
- }
- }
- if(reached_end){
- vector<char> tiles;
- printf("Whinny\n");
- //printf("%d %d",cr,cc);
- for(int i = 0; i < moves; i++){
- int m = path[cr][cc];
- tiles.push_back(move[m]);
- cr -= dr[m];
- cc -= dc[m];
- }
- reverse(tiles.begin(),tiles.end());
- for(int i = 0; i < tiles.size();i++){
- printf("%c",tiles[i]);
- }
- printf("\n");
- }
- else{
- printf("Neigh\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement