标签:pac dep str1 des 百度 book div move put
题目链接:https://vjudge.net/problem/HDU-1372#author=swust20141567
// // Created by hjy on 2019/7/10. // #include<iostream> #include<queue> #include<map> #include<cstring> #include<cstdio> using namespace std; typedef long long ll; struct node{ int x; int y; int dept; }; int book[10][10]; int dic[8][2]={{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1},{-2,-1},{-1,-2}}; int bfs(int stx,int sty,int lax,int lay) { queue<node>qu; node now,next; now.x=stx; now.y=sty; now.dept=0; book[stx][sty]=1; qu.push(now); while(!qu.empty()) { now=qu.front(); qu.pop(); if (now.x == lax && now.y == lay) return now.dept; for(int i=0;i<8;i++) { next.x = now.x + dic[i][0]; next.y = now.y + dic[i][1]; if (next.x < 0 || next.y < 0 || next.x>=8 || next.y>=8) continue; if (next.x>=0&&next.x<8&&next.y>=0&&next.y<8&&!book[next.x][next.y]) { book[next.x][next.y] = 1; next.dept=now.dept+1; qu.push(next); } } } return 0; } int main() { char str[5],str1[5]; map<char,int>mp; mp[‘a‘]=0; mp[‘b‘]=1; mp[‘c‘]=2; mp[‘d‘]=3; mp[‘e‘]=4; mp[‘f‘]=5; mp[‘g‘]=6; mp[‘h‘]=7; while(cin>>str) { getchar(); cin>>str1; memset(book,0,sizeof(book)); int stx=str[1]-‘0‘-1; int sty=mp[str[0]]; int lax=str1[1]-‘0‘-1; int lay=mp[str1[0]]; //cout<<stx<<" "<<sty<<" "<<lax<<" "<<lay<<endl; cout<<"To get from "<<str<<" to "<<str1<<" takes "<<bfs(stx,sty,lax,lay)<<" knight moves."<<endl; } return 0; } /* e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6 */
HDU - 1372 Knight Moves(bfs入门)
标签:pac dep str1 des 百度 book div move put
原文地址:https://www.cnblogs.com/Vampire6/p/11167444.html