标签:des style blog http color java os io
http://acm.hdu.edu.cn/showproblem.php?pid=1372
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6731 Accepted Submission(s): 4059
#include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; int mv[8][2] = {{-2,-1},{-1,-2},{-2,1},{-1,2},{1,-2},{2,-1},{1,2},{2,1}}; int v[9][9],map[9][9]; char a[3],b[3]; struct node { int x,y,ans; }q[1000001]; void bfs(int x,int y) { int e=0; int s=0; memset(v,0,sizeof(v)); struct node t,f; t.x=x; t.y=y; t.ans=0; v[t.x][t.y]=1; q[e++]=t; while(s<e) { t=q[s++]; if(map[t.x][t.y]==1) { printf("To get from %s to %s takes %d knight moves.\n",a,b,t.ans); } for(int i=0;i<8;i++) { f.x=t.x+mv[i][0]; f.y=t.y+mv[i][1]; f.ans=t.ans+1; if(f.x>=0&&f.x<8&&f.y>=0&&f.y<8&&v[f.x][f.y]==0) { q[e++]=f; v[f.x][f.y]=1; } } } } int main() { while(scanf("%s%s",a,b)!=EOF) { memset(map,0,sizeof(map)); map[b[1]-‘0‘-1][b[0]-‘a‘]=1;//因为a-‘0‘从0开始,所以a[1]-‘0‘-1; bfs(a[1]-‘0‘-1,a[0]-‘a‘); } return 0; }
Knight Moves(hdu1372 bfs模板题),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/zhangmingcheng/p/3917404.html