uva-439 骑士的移动
来源:互联网 发布:ubuntu怎么上传lrzsz包 编辑:程序博客网 时间:2024/05/01 00:50
bfs,用队列记录步数
#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <iostream>using namespace std;#define DEBUGconst int maxn=1000+5,maxv=26;int a,b,c,d;bool visited[maxn][maxn];char s[maxn];int qx[maxn],qy[maxn],qcnt[maxn],f,t,cnt;int mv[8][2]={1,2,2,1,2,-1,1,-2,-1,-2,-2,-1,-2,1,-1,2};void bfs(){ qx[t]=a; qcnt[t]=0; qy[t++]=b; while(t>f){ int u=qx[f]; int tcnt=qcnt[f]; int v=qy[f++]; if(u<0||u>7||v<0||v>7)continue; if(visited[u][v])continue; // printf("%d-%d-%d\n", u,v,tcnt); visited[u][v]=1; if(u==c&&v==d)printf("To get from %c%c to %c%c takes %d knight moves.\n",a+'a',b+'1',c+'a',d+'1',tcnt); for(int i=0;i<8;i++){ qx[t]=u+mv[i][0]; qcnt[t]=tcnt+1; qy[t++]=v+mv[i][1]; } }}int main(){#ifdef DEBUG freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endif while(fgets(s,6,stdin)){ memset(visited,0,sizeof(visited)); memset(qx,0,sizeof(qx)); memset(qy,0,sizeof(qy)); memset(qcnt,0,sizeof(qcnt)); f=t=0; a=s[0]-'a'; b=s[1]-'1'; c=s[3]-'a'; d=s[4]-'1'; // cout<<s<<endl; // cout<<a<<b<<c<<d<<endl; bfs(); }#ifdef DEBUG fclose(stdin); fclose(stdout);#endif return 0;}
阅读全文
0 0
- uva-439 骑士的移动
- Uva 439 骑士移动
- 439UVa骑士移动
- 骑士的移动(Knight Moves,UVa 439)
- UVa 439 Knight Moves 骑士的移动
- 骑士的移动(Knight Moves, UVa 439)
- 习题6-4 骑士的移动 UVa 439
- 习题 6-4 UVA 439 Knight Moves 骑士的移动
- uva 439 Knight Moves(骑士的移动)BFS
- UVa OJ 439 - Knight Moves 骑士移动
- uva 439 Knight Moves 骑士移动
- uva 439骑士的移动(图的最短路 bfs)
- 习题6-4 骑士的移动(Knight Moves, UVa 439)
- UVA 439 (骑士移动 第一道DFS 13.07.17)
- uva刷题之路--439 骑士移动
- uva 439 Knight Moves 骑士移动 —— bfs
- uva439_骑士的移动
- UVA 439 骑士游历
- 【nodejs】快速入门使用
- C语言 Linux文件系统调用 分组
- 了解TCP三次握手、四次挥手以及syn攻击
- Json web token 详解(二)
- 数据结构排序法之堆排序he归并排序
- uva-439 骑士的移动
- 【枚举标记】HDU4907Task schedule【BestCoder Round #3】
- OpenGL 03 绘制命令
- Linux 之GIT 安装(centos6.5)
- 文件上传与下载权限的控制
- 2017.08.06
- tcp头部没有数据长度信息
- LeetCode之Search Insert Position
- this super