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 ...
随机推荐
- 数据结构~Sqlserver索引使用的B树
B树相关概念 在B-树中查找给定关键字的方法是,首先把根结点取来,在根结点所包含的关键字K1,…,Kn查找给定的关键字(可用顺序查找或二分查找法),若找到等于给定值的关键字,则查找成功:否则,一定可以 ...
- CentOS 配置防火墙操作实例
注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status<回车> 停止防火墙: [root@localh ...
- AI 帮助涂鸦
这个小工具挺有意思,可以在涂鸦的同时自动猜测你要画什么,并自动完成. https://quickdraw.withgoogle.com/
- 类型限定词——const
类型限定词有三个:const volatile restrict. const:一般也叫常量修饰符. 作用:是修饰变量,被修饰的变量就变成常量了,不能被二次修改了. const int a=12:a ...
- 电脑小白和ta的小白电脑——Tomcat服务器
配置web服务器tomcat,这里默认了已经配置JAVA开发环境↓ https://www.cnblogs.com/gifted35/p/9775112.html (一)下载tomcat 我安装的服务 ...
- python 捕获异常顺序
catch 异常的时候,有关的异常(若是抛出子类异常,则父类异常的except也算.反之不算)except的语句是按代码顺序执行, 也就是说,当一个异常发生时,从若干except中若遇见异常类基类,父 ...
- 使用飞冰组件关于点击行回填在input内(React)
import { Table,Grid } from "@icedesign/base"; import { FormBinderWrapper as IceFormBinderW ...
- SQL-50 将employees表中的所有员工的last_name和first_name通过(')连接起来
题目描述 将employees表中的所有员工的last_name和first_name通过(')连接起来.CREATE TABLE `employees` (`emp_no` int(11) NOT ...
- 命令:curl
在227服务器上执行 curl -i -X PUT --url http://192.168.1.227:8001/apis/ --data 'name=getweather' --data 'ups ...
- js(含有for if函数)
1.在定义变量时,尽可能让变量的访问范围最小化 2.弹出单选性别,嵌套for和if函数 <script type="application/javascript"> ...