Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #include<queue>
- #include<string.h>
- using namespace std;
- struct Node
- {
- int r,c,d;
- };
- char from[4];
- char to[4];
- char chess[12][12];
- char vst[12][12];
- queue<Node>Q;
- int dx[]={2,2,-2,-2,1,-1,1,-1};
- int dy[]={1,-1,1,-1,2,2,-2,-2};
- int bfs(Node S,Node T)
- {
- int i;
- memset(vst,0,sizeof(vst));
- while(!Q.empty())Q.pop();
- /*for(i=0;i<12;i++)
- {
- printf("\n");
- for(int j=0;j<12;j++)printf("%d",vst[i][j]);
- }*/
- Q.push(S);
- vst[S.r][S.c]=1;
- Node u,v;
- while(!Q.empty())
- {
- u=Q.front();
- //printf("%d %d %d\n",u.r,u.c,u.d);
- Q.pop();
- for(i=0;i<8;i++)
- {
- v=u;
- v.r+=dx[i];
- v.c+=dy[i];
- if(chess[v.r][v.c]!=1)continue;
- if(!vst[v.r][v.c])
- {
- vst[v.r][v.c]=1;
- v.d=u.d+1;
- //printf("%d %d %d\n",v.r,v.c,v.d);
- if(v.r==T.r && v.c==T.c)return v.d;
- Q.push(v);
- }
- }
- }
- return 0;
- }
- int main()
- {
- int i,j,x;
- for(i=2;i<=9;i++)memset(chess[i]+2,1,8);
- while(scanf("%s %s",from,to)==2)
- {
- //for(i=2;i<=9;i++)memset(chess[i]+2,1,8);
- /*for(i=0;i<12;i++)
- {
- printf("\n");
- for(j=0;j<12;j++)printf("%d",chess[i][j]);
- }*/
- Node S,T;
- S.r=from[1]-47;
- S.c=from[0]-95;
- S.d=0;
- T.r=to[1]-47;
- T.c=to[0]-95;
- //printf("%d %d %d %d\n",S.r,S.c,T.r,T.c);
- x=bfs(S,T);
- //printf("%d\n",x);
- printf("To get from %s to %s takes %d knight moves.\n",from,to,x);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement