Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<algorithm>
- #include<queue>
- #include<map>
- #include<vector>
- #include<cstdio>
- #include<cstdlib>
- #include<cstring>
- using namespace std;
- #define ll long long
- #define PII pair<int,int>
- int dx[] = { -2, -1, -2, -1, 1, 2, 2, 1 };
- int dy[] = { -1, -2, 1, 2, 2, 1, -1, -2 };
- ll level[8][8] = {};
- ll BFS(PII src, PII dst)
- {
- queue< PII > q ;
- q.push(src) ;
- level[src.first][src.second] = 1 ;
- while(!q.empty())
- {
- PII now ;
- now = q.front();
- q.pop();
- for(ll i = 0 ; i< 8 ; i++)
- {
- ll tx, ty ;
- tx = now.first + dx[i] ;
- ty = now.second + dy[i] ;
- if(tx >= 0 && tx< 8 && ty >=0 && ty< 8 )
- {
- if(level[tx][ty]==0)
- {
- level[tx][ty] = level[now.first][now.second] + 1 ;
- q.push(make_pair(tx, ty)) ;
- }
- else
- {
- if(level[tx][ty] > level[now.first][now.second] + 1)
- {
- level[tx][ty] = level[now.first][now.second] + 1 ;
- }
- }
- }
- }
- }
- return level[dst.first][dst.second] ;
- }
- int main()
- {
- // freopen("in.txt","r",stdin);
- char s1[5], s2[5] ;
- ll ans;
- while(scanf(" %s %s", s1, s2)!= EOF)
- {
- ll r1, r2, c1, c2 ;
- r1 = s1[1] - '0' - 1 ;
- r2 = s2[1] - '0' -1 ;
- c1 = s1[0] - 'a' ;
- c2 = s2[0] - 'a' ;
- PII src, dst ;
- src = make_pair(r1, c1) ;
- dst = make_pair(r2, c2) ;
- ans = BFS(src,dst) - 1;
- printf("To get from %s to %s takes %lld knight moves.\n", s1, s2, ans ) ;
- memset(level, 0, sizeof(level));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement