HDU 1372 Knight Moves
来源:互联网 发布:算法 第四版 pdf 编辑:程序博客网 时间:2024/05/16 05:50
http://acm.hdu.edu.cn/showproblem.php?pid=1372
#include <queue>#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>using namespace std;struct node{ int x,y; void init(int nx, int ny){ x = nx; y = ny; }};char str1[10],str2[10];int dis[10][10],vis[10][10];int x1,y1,x2,y2;int dir[][2] = {{-2,-1},{-1,-2},{1,-2},{2,-1},{2,1},{1,2},{-1,2},{-2,1}};bool Isin(int x, int y){ if(x < 1 || x > 8 || y < 1 || y > 8) return false; return true;}int bfs(int x, int y){ queue <node> q; while(!q.empty()) q.pop(); node a,b; a.init(x,y); vis[a.x][a.y] = 1; q.push(a); while(!q.empty()){ a = q.front(); q.pop(); if(a.x == x2 && a.y == y2) return 1; for(int i=0; i<8; i++){ int xx = a.x + dir[i][0],yy = a.y + dir[i][1]; b.init(xx, yy); if(!vis[xx][yy] && Isin(xx, yy)){ vis[xx][yy] = 1; dis[xx][yy] = dis[a.x][a.y] + 1; q.push(b); } } } return 0;}int main(){// freopen("in.txt", "r", stdin); while(scanf("%s%s", str1, str2) != EOF){ x1 = str1[0] - 'a' + 1, x2 = str2[0] - 'a' + 1; y1 = str1[1] - '0', y2 = str2[1] - '0'; memset(vis, 0, sizeof(vis)); memset(dis, 0, sizeof(dis)); if(bfs(x1, y1)) printf("To get from %s to %s takes %d knight moves.\n",str1, str2, dis[x2][y2]); } return 0;}
0 0
- HDU 1372 knight moves
- HDU 1372 Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 Knight Moves
- Hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU-1372Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- hdu-1372-Knight Moves
- HDU 1372 Knight Moves
- HDU-1372-knight moves
- hdu(1372) Knight Moves
- hdu 1372 Knight Moves
- hdu 1372 Knight Moves
- 实现全排列的两种算法:字典序列法以及递归算法(java)
- 我的VC++学习
- 根据属性配置文件为bean赋值
- opencv程序十七:运动目标检测之背景减除法
- 天津航天中为面试
- HDU 1372 Knight Moves
- 怎么将后缀为.opt,.frm,.myd,.myi文件还原或者是导入mySQL中
- HDU 4627 The Unsolvable Problem 数学
- SQL注入攻击的原理与防范
- vim环境搭建
- struts2.3.16+spring4.0.0+hibernate4.4.0整合
- java JFreechart开发报表的实例demo下载
- 解决Mac OS 升级至10.10 Yosemite后Python IDE Pycharm 3.4 闪退问题
- Centos 6.5 安装 Scrapy