POJ-2243-Knight Moves
2013-05-21 18:45
225 查看
BFS题~
代码:
代码:
#include<cstdio> #include<cstring> #include<queue> #define MAX 10 #define inf 10000 using namespace std; int xa,xb,ya,yb,ans; int runx[10]={1,1,-1,-1,2,2,-2,-2},runy[10]={2,-2,2,-2,1,-1,1,-1}; queue<int> qx,qy; int Is(int x,int y) { if(x<1||x>8||y<1||y>8) return 1; return 0; } void BFS(int x,int y) { int coua=1,coub=0; qx.push(x); qy.push(y); ans=1; while(1) { for(int i=0;i<coua;i++) { int xx=qx.front(); qx.pop(); int yy=qy.front(); qy.pop(); for(int j=0;j<8;j++) { int ita,itb; ita=xx+runx[j]; itb=yy+runy[j]; if(Is(ita,itb)) continue; if(ita==xb&&itb==yb) return; qx.push(ita); qy.push(itb); coub++; } } coua=coub; coub=0; ans++; } } int main() { char stra[5],strb[5]; while(scanf("%s%s",stra,strb)!=EOF) { xa=stra[0]-'a'+1; ya=stra[1]-'0'; xb=strb[0]-'a'+1; yb=strb[1]-'0'; ans=0; if(!(xa==xb&&ya==yb)) BFS(xa,ya); printf("To get from %s to %s takes %d knight moves.\n",stra,strb,ans); while(!qx.empty()) qx.pop(); while(!qy.empty()) qy.pop(); } return 0; }
相关文章推荐
- 【POJ 2243】Knight Moves
- poj 2243 Knight Moves
- poj 2243 Knight Moves
- POJ 2243 Knight Moves
- POJ 2243-Knight Moves
- poj 2243 -- Knight Moves (bfs)
- POJ 2243 Knight Moves
- poj 2243 Knight Moves
- POJ 2243 Knight Moves
- poj2243 -- Knight Moves
- poj 2243 Knight Moves
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
- poj 2243:Knight Moves
- POJ 2243 Knight Moves
- poj 2243 Knight Moves
- POJ 2243 Knight Moves
- POJ---2243 Knight Moves 使用A*算法的广度优先搜索
- POJ 2243 || HDU 1372:Knight Moves(BFS)
- poj 2243 Knight Moves
- poj 1915 Knight Moves