poj 2243 bfs(还是骑士移动)
2012-01-18 01:31
253 查看
这道题跟 1915 是一样的,通过这道题主要学习了 从字符串中分离出字母和数字的方法。
#include<iostream> #include<string> using namespace std; #define max 10 int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; int vis[max][max],step[max][max],sx,sy,ex,ey; int xx[100],yy[100]; void bfs(int i,int j) { int front,rear,k,x,y; front=0;rear=0; vis[i][j]=1; step[i][j]=0; xx[rear]=i; yy[rear++]=j; while(rear>front) { i=xx[front]; j=yy[front++]; for(k=0;k<8;k++) { x=i+dir[k][0]; y=j+dir[k][1]; if(!vis[x][y]&&x>0&&x<=8&&y>0&&y<=8)//注意不要踏出棋盘 { vis[x][y]=1; step[x][y]=step[i][j]+1; xx[rear]=x; yy[rear++]=y; //把点 入队列 //printf("%d%d ",x,y); } } } } int main() { string s1,s2;//从字符串中分离出字母和数字,注意技巧 while(cin>>s1>>s2) { memset(vis,0,sizeof(vis)); memset(step,0,sizeof(step)); sx=s1[0]-'a'+1; sy=s1[1]-'0'; ex=s2[0]-'a'+1; ey=s2[1]-'0'; //printf("%d%d%d%d",sx,sy,ex,ey); //system("pause"); if(sx==ex&&sy==ey) {cout<<"To get from "<<s1<<" to "<<s2<<" takes 0 knight moves."<<endl; continue;} else { bfs(sx,sy); cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<step[ex][ey]<<" knight moves."<<endl; } } }
[/code]
相关文章推荐
- 骑士移动问题 实现的三种算法 POJ 2243,ZOJ 1091
- poj 1915 bfs(骑士移动)
- poj 2243 bfs 利用 结构体中的step成员保存步数 ,STL的队列
- hdu 1372 & poj 2243(bfs)
- poj2243+poj1915骑士问题
- POJ 2243 Knight Moves (BFS入门)
- poj 2243 bfs
- POJ 2243:Knight Moves(双向BFS)
- poj2243+poj1915骑士问题
- POJ 2243 Knight Moves【BFS|A*】
- POJ 2243 Knight Moves(BFS || 双向BFS)
- poj 2243 双向bfs和A*
- POJ 2243 || HDU 1372:Knight Moves(BFS)
- POJ 2243 BFS 和 简单的调试方法学习
- POJ 2243 Knight Moves (bfs)
- POJ 1573 Robot Motion【是搜索,就不要纠结是DFS还是BFS】
- BFS简单搜索--POJ 2243
- poj 2243 骑士游历2
- poj 2243 Knight Moves bfs
- POJ 2243 BFS