hdu Knight Moves
来源:互联网 发布:python 反转二叉树 编辑:程序博客网 时间:2024/05/16 01:22
这题真是醉了。一直不知道题目在描述什么,后来问了一个朋友才知道,题目是这样的:一个国际象棋骑士(走法和中国象棋的马一样),最短多少步能跳到目标位置。
分析:直接广搜,把位置转化为数字,然后传入bfs函数里面。
代码如下
#include <iostream>#include <cstring>#include <string>#include <queue>using namespace std;int visit[10][10],ans;struct node{int x;int y;};void bfs(int sx,int sy,int ex,int ey){node d,D;queue<node>q;while(!q.empty())q.pop();d.x=sx,d.y=sy;q.push(d);int move[8][2]={-2,1, -2,-1, 2,-1, 2,1, 1,2, -1,2, -1,-2, 1,-2 };while(!q.empty()){d=q.front();q.pop();if(d.x==ex && d.y==ey){ans=visit[d.x][d.y];return ;}for(int i=0;i<8;i++){D.x=d.x+move[i][0];D.y=d.y+move[i][1];if(!visit[D.x][D.y] && D.x>=1 && D.y>=1 && D.x<=8 && D.y<=8){visit[D.x][D.y]=visit[d.x][d.y]+1;q.push(D);}}}}int main(){string s1,s2;while(cin>>s1>>s2){memset(visit,0,sizeof(visit));ans=0;int sx,sy,ex,ey;sx=s1[1]-'0';ex=s2[1]-'0';sy=s1[0]-'a'+1;ey=s2[0]-'a'+1;bfs(sx,sy,ex,ey);cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<ans<<" knight moves."<<endl;}}
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 2243 Knight Moves
- HDU 1372 Knight Moves
- HDU-1372Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- hdu-1372-Knight Moves
- Knight Moves hdu BFS
- HDU 1372 Knight Moves
- hdu-Knight Moves
- HDU-1372-knight moves
- Remove Duplicates from Sorted List II
- chown命令
- python学习——WSGI接口
- CV/ML/DL关键词汇中英文对照
- 杭电OJ2018-母牛的故事
- hdu Knight Moves
- Android不同层次开启硬件加速的方式
- Codeforces Round #363 (Div. 2) B. One Bomb
- 每日一句
- [Leetcode]238. Product of Array Except Self
- Activity启动以及创建
- G-Milk
- How To Create A Client Server Application In Minutes[如何在几分钟的时间里编写一个客户机/服务器控制台程序]
- 古文构词之法