POJ 2243解题报告
2012-05-22 19:46
393 查看
BFS
#include <stdio.h> #include <memory.h> #include <stdlib.h> const int maxn=15; int visit[maxn*maxn]; int dir[8][2]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}}; char a[3]; char b[3]; int bfs(int st,int fin); int main() { while(scanf("%s",a)!=EOF) { scanf("%s",b); int sy=a[0]-'a'; int sx=a[1]-'1'; int fy=b[0]-'a'; int fx=b[1]-'1'; memset(visit,0,sizeof(visit)); int sl=sx*8+sy; int fl=fx*8+fy; if(sl==fl) { printf("To get from %s to %s takes 0 knight moves.\n",a,b); } else { printf("To get from %s to %s takes %d knight moves.\n",a,b,bfs(sl,fl)); } } return 0; } int bfs(int st,int fin) { int que[maxn*maxn]; int font=0; int rear=0; int distance[maxn*maxn]; distance[st]=0; que[rear++]=st; while(font<rear) { int on=que[font++]; int x=on/8; int y=on%8; int i; for(i=0;i<8;i++) { int dx=x+dir[i][0]; int dy=y+dir[i][1]; int on2=dx*8+dy; if(dx>=0&&dx<8&&dy>=0&&dy<8&&!visit[on2]) { visit[on2]=1; distance[on2]=distance[on]+1; que[rear++]=on2; if(on2==fin) return distance[on2]; } } } }
相关文章推荐
- POJ 2243解题报告
- POJ 2243解题报告
- POJ 2243解题报告
- poj1062解题报告:
- poj_1068_Parencodings_解题报告
- 2011.11.23 POJ2531解题报告【谨以此文,纪念自己跨过POJ50道题的小里程碑】
- poj1416数字切割解题报告
- poj2377解题报告
- poj解题报告——3254
- POJ - 1062 昂贵的聘礼 解题报告
- POJ 2769 Reduced ID Numbers 解题报告
- POJ2377 解题报告
- poj 3617 Best Cow Line 解题报告
- hdu1501-poj2192详细解题报告
- POJ - 1094 Sorting It All Out解题报告
- poj解题报告——3537
- POJ 1159 Palindrome解题报告
- poj-openjudge 1037:City Metro 解题报告
- 【POJ 1664】 放苹果 解题报告
- POJ 4045 Power Station 解题报告