Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cmath>
- #include<string>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<vector>
- #include<stack>
- #include<queue>
- #include<map>
- #define FRU freopen("out.txt","w",stdout)
- #define FRO freopen("in.txt","r",stdin)
- #define pb push_back
- const int row[]= {-1,0,1, 0,-1,-1,1, 1}; // Kings Move
- const int col[]= {0,1,0,-1,-1, 1,1,-1}; // Kings Move
- const int row1[]= {-2, -2, -1, -1, 0, 1, 1, 2, 2}; // Knights Move
- const int col1[]= {-1, 1, -2, 2, 0, -2, 2, -1, 1}; // Knights Move
- //const int row[]={-1,0,0,1,0};
- //const int col[]={0,-1,1,0,0};
- using namespace std;
- int grid[201][201],dis[201][201],limit,limit1;
- struct data
- {
- int rw,cl;
- };
- bool vis[201][201];
- void bfs(int r,int c)
- {
- for(int i=0; i<201; i++)for(int j=0; j<201; j++)
- {
- vis[i][j]=0;
- dis[i][j]=-1;
- }
- queue<data>q;
- data u,v;
- u.rw=r;
- u.cl=c;
- vis[r][c]=1;
- dis[r][c]=0;
- q.push(u);
- while(!q.empty())
- {
- u=q.front();
- q.pop();
- for(int i=0; i<8; i++)
- {
- v.rw=u.rw+row[i];
- v.cl=u.cl+col[i];
- if(vis[v.rw][v.cl]==0&& v.rw>=0&& v.rw<limit&& v.cl>=0&& v.cl<limit1&& grid[v.rw][v.cl]==0)
- {
- q.push(v);
- vis[v.rw][v.cl]=1;
- dis[v.rw][v.cl]=dis[u.rw][u.cl]+1;
- }
- }
- }
- }
- int main()
- {
- //FRO;
- //FRU;
- int a,b,i,j,k,tc,t;
- int n,m,cnt=0,u,v,R,C,r,c;
- char ch,s[201][201];
- scanf("%d",&tc);
- for(t=1; t<=tc; t++)
- {
- for(i=0; i<201; i++)
- {
- memset(grid[i],0,200);
- }
- scanf("%d%d",&R,&C);
- limit=R;
- limit1=C;
- for(i=0; i<limit; i++)
- {
- scanf("%s",s[i]);
- }
- for(i=0; i<limit; i++)
- {
- for(j=0; j<limit1; j++)
- {
- ch=s[i][j];
- if(ch=='Z')
- {
- for(k=0; k<9; k++)
- {
- a=i+row1[k];
- b=j+col1[k];
- if(a>=0&& b>=0&& a<limit&& b<limit1)
- {
- grid[a][b]=1;
- }
- }
- }
- else if(ch=='A')
- {
- u=i;
- v=j;
- }
- else if(ch=='B')
- {
- n=i;
- m=j;
- }
- }
- }
- grid[u][v]=0;
- grid[n][m]=0;
- bfs(u,v);
- if(dis[n][m]==-1)printf("King Peter, you can't go now!\n");
- else printf("Minimal possible length of a trip is %d\n",dis[n][m]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement