hdu1372 Knight Moves
来源:互联网 发布:贝尔 知乎 编辑:程序博客网 时间:2024/06/07 10:15
需要国际象棋的知识,棋盘是1-8,a-h,骑士就是马啦,走的是2*3的格子的对角线,那么就是有八个方位
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#include<algorithm>#include<queue>#define N 105using namespace std;struct node{int x,y,step; };int n,m;int vis[N][N],map[N][N];int ex,ey;char s1[10],s2[10];int check(int x,int y){if(x<=0||x>n||y<=0||y>m||map[x][y])return 1;return 0;}int bfs(){int i;queue<node>Q;node a,next;a.x=s1[0]-'a'+1;a.y=s1[1]-'0';ex=s2[0]-'a'+1;ey=s2[1]-'0';//printf("%d %d\n",a.x,a.y);a.step=0;memset(map,0,sizeof(map));map[a.x][a.y]=1;Q.push(a);while(!Q.empty()){//printf("111\n"); a=Q.front(); Q.pop(); if(a.x==ex&&a.y==ey)return a.step; next.x=a.x+1; next.y=a.y+2; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x+2; next.y=a.y+1; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x+2; next.y=a.y-1; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x+1; next.y=a.y-2; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x-1; next.y=a.y+2; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x-2; next.y=a.y+1; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x-2; next.y=a.y-1; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); } next.x=a.x-1; next.y=a.y-2; if(next.x==ex&&next.y==ey)return a.step+1; if(!check(next.x,next.y)){ next.step=a.step+1; map[next.x][next.y]=1; Q.push(next); }}return 0;}int main(){n=8;m=8;while(scanf("%s%s",s1,s2)!=EOF){printf("To get from %s to %s takes %d knight moves.\n",s1,s2,bfs()); }}
阅读全文
0 0
- Knight Moves hdu1372 bfs
- hdu1372 Knight Moves
- hdu1372-Knight Moves--搜索
- HDU1372:Knight Moves
- hdu1372题目Knight Moves
- HDU1372:Knight Moves(BFS)
- HDU1372 Knight Moves
- hdu1372 Knight Moves (BFS)
- HDU1372 Knight Moves
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves
- hdu1372 Knight Moves(BFS)
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves BFS
- HDU1372:Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- Knight Moves hdu1372
- HDU1372----Knight Moves
- bzoj 5055: 膜法师
- Android 自定义沉浸式标题
- 向系统日历添加日程提醒的规则
- Java爬虫-利用jsoup(抓安居客房源)
- JAVA中String类型的几种替换空格的方式
- hdu1372 Knight Moves
- 关于model的运用
- Android 图片加载框架最全解析(五),Glide强大的图片变换功能
- ceph工作原理及安装
- jenkins自动部署tomcat脚本
- mybatis入门(二)——mybatis开发项目五部曲
- 浅谈数据库索引
- opencv开发笔记(十四):创建包围轮廓的矩形边界
- springmvc常用注解标签详解