hdu--1372 knight moves (BFS+数据结构)
2011-06-19 16:51
344 查看
#include "iostream" #include "queue" using namespace std; int dir[8][2]={1,2,1,-2,2,1,2,-1,-1,2,-1,-2,-2,1,-2,-1}; int vis[8][8]; struct node{ int x; int y; int lev; }; int BFS(node s, node e) { int i; node front,temp; queue <node> seq; memset(vis,0,sizeof(vis)); if(s.x==e.x&&s.y==e.y) return 0; while(!seq.empty()) seq.pop(); vis[s.x][s.y]=1; seq.push(s); while(!seq.empty()) { front=seq.front(); vis[front.x][front.y]=1; seq.pop(); for(i=0; i<8; i++) { temp.x = front.x + dir[i][0]; temp.y = front.y + dir[i][1]; if(temp.x>=0&&temp.x<8&&temp.y>=0&&temp.y<8&&!vis[temp.x][temp.y]) { temp.lev = front.lev + 1; if(temp.x == e.x && temp.y == e.y) return temp.lev; seq.push(temp); } } } return 0; } int main() { char a[3],b[3]; node start,end; while(cin >> a >> b) { start.x = a[0] - 'a'; start.y = a[1] - '1'; end.x = b[0] - 'a'; end.y = b[1] - '1'; start.lev = 0; cout<<"To get from "<<a[0]<<a[1]<<" to "<<b[0]<<b[1]<<" takes "<<BFS(start,end)<<" knight moves."<<endl; } return 0; }
相关文章推荐
- hdu 1372 Knight Moves(BFS)
- zoj 1091 && hdu 1372 Knight Moves
- HDU 1372 Knight Moves 广搜
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves
- ACM 搜索 hdu 1372 Knight Moves
- HDU 1372 Knight Moves (BFS)
- HDU 1372 Knight Moves
- HDU--杭电--1372--Knight Moves--广搜--就是象棋里面‘马’走日一样走,水题
- HDU 1372 (搜索方向稍有改变) Knight Moves
- HDU 1372Knight Moves(简单的BFS)
- hdu1372 Knight Moves (BFS)
- HDU 1372 Knight Moves(BFS)
- hdu 1372 Knight Moves 解题报告
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves <java>
- hdu1372 Knight Moves BFS 搜索
- HDU 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 ( Knight Moves ) BFS