UVa439——骑士的移动
简单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——骑士的移动的更多相关文章
- UVA439 骑士的移动 Knight Moves
#include<bits/stdc++.h> using namespace std; char a,c; int b,d; ][]; ]={,,,-,,-,-,-}; ]={,-,,, ...
- 题解 UVA439 骑士的移动 Knight Moves
前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...
- COGS746. [网络流24题] 骑士共存
骑士共存问题«问题描述:在一个n*n个方格的国际象棋棋盘上,马(骑士)可以攻击的棋盘方格如图所示.棋盘 上某些方格设置了障碍,骑士不得进入. «编程任务:对于给定的n*n个方格的国际象棋棋盘和障碍标志 ...
- 【BZOJ1671】[Usaco2005 Dec]Knights of Ni 骑士 BFS
[Usaco2005 Dec]Knights of Ni 骑士 Description 贝茜遇到了一件很麻烦的事:她无意中闯入了森林里的一座城堡,如果她想回家,就必须穿过这片由骑士们守护着的森林.为 ...
- 骑士游历/knight tour - visual basic 解决
在visual baisc 6 how to program 中文版第七章的练习题上看到了这个问题,骑士游历的问题. 在8x8的国际象棋的棋盘上,骑士(走法:一个方向走两格,另一个方向一格)不重复走完 ...
- uva-439
题意:骑士在一个8*8的棋盘上移动,1-8代表行号,a-h代表列号,给出骑士的初始位置和目的位置,求骑士最少的移动步数:题目隐含一层意思(骑士移动规则是中国象棋的“马”的走法) 输入:一串字符串,包含 ...
- BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]
1085: [SCOI2005]骑士精神 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1800 Solved: 984[Submit][Statu ...
- BZOJ 1040 【ZJOI2008】 骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战火 ...
- 【BZOJ-1040】骑士 树形DP + 环套树 + DFS
1040: [ZJOI2008]骑士 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3312 Solved: 1269[Submit][Status ...
随机推荐
- go-ethereum源码分析 PartII 共识算法
首先从共识引擎-Engine开始记录 Engine是一个独立于具体算法的共识引擎接口 Author(header) (common.Address, error) 返回打包header对应的区块的矿工 ...
- 关于input
form表单的使用: 1.iuput:type:类型有很多常用的: text:输入的内容为文本格式(内容可见) password:输入的内容为......(内容不可见) radio:显示为单选框(框为 ...
- myBatis简学
mybatis使用: ①拷贝相关mybits ②编写对象关系映射,一般都是实体类名+Mapper.xml的格式 ③编写mybits配置文件: a)配置环境 b)配置映射文件地址 ④编写对象操作方法: ...
- https://www.cnblogs.com/h2zZhou/p/5440271.html
https://www.cnblogs.com/h2zZhou/p/5440271.html
- JDBCUtils——C3P0
需要导入的包: mysql-connector-java-5.1.37-bin.jar c3p0-0.9.2-pre5.jar mchange-commons-java-0.2.3.jar 如果使用D ...
- OpenCV3如何使用SIFT和SURF Where did SIFT and SURF go in OpenCV 3?
Installation and Usage If you have previous/other version of OpenCV installed (e.g. cv2 module in th ...
- reinterpret_cast
reinterpret_cast强制类型转换符 用法: new_type a = reinterpret_cast <new_type> (value) 将value的值转成new_typ ...
- console.log()中的%d,%s等代表的输出类型
在console.log()或console.debug()中输出时会有%d,%s等符号. %s for a String value 字符类型 %d or %i for a Integer valu ...
- 巧用std::shared_ptr全局对象释放单例内存
巧用std::shared_ptr 单例的使用相对比较广泛,但是需要在程序退出前调用它的析构函数对数据进行释放,常规做法是在main函数末尾进行释放工作, 但是这样相对比较繁琐,因此便有了利用全局变量 ...
- JavaScript中的各种宽高总结
window和document首先我们来高清两个概念: window和document的区别是什么? window.location和document.location是一样吗?第一个问题 ...