439 - Knight Moves(bfs)
来源:互联网 发布:kali linux gnuradio 编辑:程序博客网 时间:2024/05/18 02:57
直接BFS就行了。。和迷宫差不多,都是在每一步出现分支判断。
d[][]存距离,将a-h转换成数字。
#include<bits/stdc++.h>using namespace std;int r1,c1,r2,c2,d[20][20];int r[10]={-1,-2,-2,-1,1,2,2,1};int c[10]={-2,-1,1,2,-2,-1,1,2};struct node { int r,c; node(int r=0,int c=0): r(r),c(c) {}};int moves() { queue<node> q; q.push(node(r1,c1)); memset(d,-1,sizeof(d)); d[r1][c1]=0; while(!q.empty()) { node u=q.front(); q.pop(); if(u.r==r2&&u.c==c2) return d[u.r][u.c]; for(int i=0;i<8;i++){ int r0=u.r+r[i],c0=u.c+c[i]; if(r0>=1&&r0<=8&&c0>=0&&c0<=7) { node v=node(r0,c0); if(d[r0][c0]<0){ d[v.r][v.c]=d[u.r][u.c]+1; q.push(v); } } } }}int main(){ char a[10],b[10]; while(scanf("%s",a)!=EOF) { scanf("%s",b); r1=a[1]-'0'; c1=a[0]-'a'; r2=b[1]-'0'; c2=b[0]-'a'; int maxn=moves(); printf("To get from %s to %s takes %d knight moves.\n",a,b,maxn); } return 0;}
0 0
- 439 - Knight Moves(bfs)
- 439 - Knight Moves//bfs
- Knight Moves(bfs)
- Knight Moves(BFS)
- Knight Moves(BFS)
- Knight Moves (BFS)
- uva 439 Knight Moves(BFS)
- uva 439 Knight Moves(BFS遍历)
- UVa 439 - Knight Moves (BFS)
- UVA - 439 - Knight Moves (BFS)
- [uva-439]Knight Moves(BFS)
- UVa 439 Knight Moves (bfs)
- UVA 439 Knight Moves (BFS)
- UVA 439 Knight Moves(BFS)
- UVA 439-Knight Moves(bfs)
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- hdu1372 Knight Moves (BFS)
- UIProgressView的详细使用
- 黑马程序员__泛型
- UIImage 详解
- UITableView的协议
- win7/ubuntu双系统安装(安装ubuntu黑屏后光标一直闪)
- 439 - Knight Moves(bfs)
- android应用程序安装位置分析
- 第六周
- UINavigationController使用详解
- Entropy(哈弗曼编码,优先队列)
- Android项目优化查错神器之android lint
- 22 【健身】
- android获取应用程序的安装位置
- 主机win7,vbox虚拟机中ubuntu上网和共享文档随笔