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(){
- // freopen("in.txt","rt",stdin);
- // freopen("outDarr.txt","wt",stdout);
- 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;
- 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];
- int path[r][c];
- fill(&path[0][0], &path[0][0] + sizeof(path) / sizeof(path[0][0]), -1);
- for(int j = 0; j < r; j++){
- string line;
- cin >> line;
- for(int k = 0; k < line.length(); k++){
- if(line[k] == 'K'){
- sr = j;
- sc = k;
- }
- grid[j][k] = line[k];
- }
- }
- rqueue.push(sr);
- cqueue.push(sc);
- memset(visited,0,sizeof(visited));
- visited[sr][sc] = 1;
- while(!rqueue.empty()){
- cr = rqueue.front();
- cc = cqueue.front();
- rqueue.pop();
- cqueue.pop();
- 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){
- continue;
- }
- else if(tr >= r || tc >= c){
- continue;
- }
- else if(visited[tr][tc] == 1){
- continue;
- }
- else if(grid[tr][tc] == 'X'){
- continue;
- }
- else{
- rqueue.push(tr);
- cqueue.push(tc);
- visited[tr][tc] = 1;
- path[tr][tc] = l;
- }
- }
- }
- if(reached_end){
- vector<char> tiles;
- printf("Whinny\n");
- while(true){
- int m = path[cr][cc];
- if(m == -1)
- break;
- 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