简单bfs

 #include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
const double Pi=3.14159265358979323846;
typedef long long ll;
const int MAXN=+;
int dx[]={-,-,-,-,,,,};
int dy[]={-,,-,,-,,-,};
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll mod=1e9+;
int M[][];
struct node{
int x;int y;int step;
node (int x=,int y=,int step=)
{
this->x=x;
this->y=y;
this->step=step;
}
}now,tim; int bfs(int x1,int y1,int x2,int y2,string str1,string str2)
{ tim.x=x1;tim.y=y1;tim.step=;
queue <node> Q;
Q.push(tim);
//cout <<x2<<" "<<y2<<endl;
while(!Q.empty())
{ tim=Q.front();Q.pop();
if(tim.x==x2&&tim.y==y2)
{
cout <<"To get from "<<str1<<" to "<<str2<<" takes "<<M[tim.x][tim.y]<<" knight moves.\n";
return ;
}
for(int i=;i<;i++)
{
now.x=tim.x+dx[i];
now.y=tim.y+dy[i];
if(now.x>=&&now.y<=&&now.x<=&&now.y>=&&M[now.x][now.y]==-)
{
now.step=tim.step+;
M[now.x][now.y]=M[tim.x][tim.y]+;
Q.push(now);
}
}
}
return -;
} int main()
{
string str1,str2;
while(cin>>str1)
{
cin>>str2;
int x1,x2,y1,y2;
memset(M,-,sizeof(M));
x1=-str1[]+'';
y1=str1[]-'a'+;
y2=str2[]-'a'+;
x2=-str2[]+'';
M[x1][y1]=;
//cout <<x1<<y1<<x2<<y2<<endl;
bfs(x1,y1,x2,y2,str1,str2);
}
return ;
}

UVa439——骑士的移动的更多相关文章

  1. UVA439 骑士的移动 Knight Moves

    #include<bits/stdc++.h> using namespace std; char a,c; int b,d; ][]; ]={,,,-,,-,-,-}; ]={,-,,, ...

  2. 题解 UVA439 骑士的移动 Knight Moves

    前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...

  3. COGS746. [网络流24题] 骑士共存

    骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...

  4. 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS

    [Usaco2005 Dec]Knights of Ni 骑士 Description  贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...

  5. 骑士游历/knight tour - visual basic 解决

    在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...

  6. uva-439

    题意:骑士在一个8*8的棋盘上移动,1-8代表行号,a-h代表列号,给出骑士的初始位置和目的位置,求骑士最少的移动步数:题目隐含一层意思(骑士移动规则是中国象棋的“马”的走法) 输入:一串字符串,包含 ...

  7. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  8. BZOJ 1040 【ZJOI2008】 骑士

    Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...

  9. 【BZOJ-1040】骑士 树形DP + 环套树 + DFS

    1040: [ZJOI2008]骑士 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3312  Solved: 1269[Submit][Status ...

随机推荐

  1. 在Mac上安装office2016(破解版)

    doffice2016下载地址: 链接:https://pan.baidu.com/s/1V6I_QQiK2VK1rf0v4aVk5g  密码:3ijf 执行破解程序: 链接:https://pan. ...

  2. springboot国际化

    Session方式的国际化/** * @descripte 请求中如果有{@Param lang},则按照lang的格式国际化 * @descripte 请求中如果无{@Param lang},但se ...

  3. 网页布局中页面内容不足一屏时页脚footer固定底部

    方法一:给html.body都设置100%的高度,确定body下内容设置min-height有效,然后设置主体部分min-height为100%,此时若没有header.footer则刚好完美占满全屏 ...

  4. mysql的简单安装方法

    准备工作MySQL-Front与mysql-5.5.15-win32 开始安装 选择compelete,完整安装 自动弹出配置界面 选择标准配置 设置root密码 成功界面 MySQL-Front 的 ...

  5. Database First/Code First

  6. Python入门学习例子——从Hao123获取图片

    import urllib.requestimport re #获取html页面内容def getHtml(url): data=urllib.request.urlopen(url) pageCon ...

  7. 查询数据库游标使用情况以及sql

    ----查询游标使用情况以及游标最大数---- SELECT MAX(A.VALUE) AS HIGHEST_OPEN_CUR, P.VALUE AS MAX_OPEN_CUR FROM V$SESS ...

  8. css flex布局,小程序flex布局,垂直居中完美解决

    flex弹性布局,很好的解决了垂直居中的问题,上代码: wxml: <view class='container'> <view class='item item1'>item ...

  9. Charles篡改请求,在手机上抓包,以及弱网设置

    篡改请求 可以测试各种异常 原理:clint->server正常是客户端发送请求到服务端,charles相当于一个拦截器,拦住客户端的请求,并进行修改,修改后再发送到server端 Server ...

  10. 解决nim db_mysql could not load: libmysql.dll的问题

    title: 解决nim db_mysql could not load: libmysql.dll的问题 nim中使用db_mysql 操作数据库的代码看起来很简单: import db_mysql ...