uva 439
2015-09-09 09:55
260 查看
/************************************************************************* File Name: uva439.cpp Author: yubo Mail: yuzibode@126.com Created Time: 2014/5/18 23:15:55 reference:http://blog.sina.com.cn/s/blog_6730a3aa0100vkzd.html 相似于中国象棋中的马的走法,可是这道题改来改去最终自己做出来了 input: e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6 ************************************************************************/ #include<iostream> #include<cstring> #include<queue> #include<cstdio> using namespace std; int dir[8][2]={ {2,1},{2,-1},{1,2},{1,-2},{-1,2},{-1,-2},{-2,1},{-2,-1}, }; typedef struct{ int x,y; int times; }SE; int x1,y1,x2,y2;// int judge1(int i,int j) { if(i>=0&&i<8&&j>=0&&j<8) return 1; else return 0; } int vis[8][8]; char command[2][5];//记录命令 int ans; void bfs(int x,int y) { queue<SE> q; SE q0,q1,q2; q0.x=x; q0.y=y; q0.times=0; vis[x][y]=1; q.push(q0); while(!q.empty()){ q1=q.front(); q.pop(); for(int i=0;i<8;i++){ int temp1=q1.x+dir[i][0]; int temp2=q1.y+dir[i][1]; if(judge1(temp1,temp2)&&!vis[temp1][temp2]){ if(temp1==x2&&temp2==y2){ ans=q1.times+1; return ; } q2.x=temp1; q2.y=temp2; q2.times=q1.times+1; vis[temp1][temp2]=1; q.push(q2); } } } } int main() { // freopen("in.txt","r",stdin); while(cin>>command[0]>>command[1]){ memset(vis,0,sizeof(vis)); y1=command[0][0]-'a'; x1=command[0][1]-'1'; y2=command[1][0]-'a'; x2=command[1][1]-'1'; ans=0; // printf("%d %d %d %d\n",x1,y1,x2,y2); // printf("%s\n",command[0]); bfs(x1,y1); cout<<"To get from "<<command[0]<<" to "<<command[1]<<" takes "<<ans<<" knight moves."<<endl; } }
相关文章推荐
- Java 模板权重随机
- grep使用多个查询条件--或
- Linux 网卡高级管理
- sqlserver 存储过程中使用临时表到底会不会导致重编译
- 互联网秒杀业务设计
- [刷题]Maximum Subarray
- swift单例
- windows operate
- iOS xib 与StoryBoard 之间的互相,以及StoryBoard与StoryBoard之间跳转
- 用plsqldeveloper创建表为字段加注释时出现乱码解决方法
- ps怎么把两张图片合成一张 图解PS两张图片合成一张的技巧
- tomcat8 部署到Linux系统中,webService服务无法使用
- 移植OpenWrt到CuHead Pro WiFi
- QT5 动态链接库的创建和使用
- HDMI之EDID使用说明
- css 鼠标提示样式预览表[转]
- Android里ListView里面添加CheckBox实现多选。
- js正则表达式基本语法(精粹)
- 学习日志---图之最小生成树算法
- 安卓自带刷新SwipeRefreshLayout 上拉加载更多 自动刷新