简单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. 深入理解hashCode

    1.hashCode的概念 (1)hashCode方法是Object类的方法,在Java里所有类都默认继承Object类,即所有类都有hashCode方法. (2)hashCode是jdk根据对象的存 ...

  2. GoLand配置数据库、远程host以及远程调试

    GoLand配置MySQL数据库: (1)右侧栏 -> Database -> +添加 (2)选择MySQL (3)修改Name -> Comment(可选) (4)选择MySQL版 ...

  3. 关于input

    form表单的使用: 1.iuput:type:类型有很多常用的: text:输入的内容为文本格式(内容可见) password:输入的内容为......(内容不可见) radio:显示为单选框(框为 ...

  4. Divisor Subtraction

    Description You are given an integer number nn. The following algorithm is applied to it: if n=0, th ...

  5. Exception: 'dlib.mmod_rectangle' object has no attribute 'right' - 例外:'dlib.mmod_rectangle'对象没有属性'right'

    I'm using dlib for face detection and getting this error Exception: 'dlib.mmod_rectangle' object has ...

  6. vue页面操作技巧

    // this.$router.push({ path: "https://www.baidu.com/"}); // POST请求的时候 // this.$router.push ...

  7. AVR 嵌入式单片机芯片的中断系统介绍

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  8. Tomcat基本配置

    第二十六课 Tomcat基本配置 目录 一. Tomcat介绍 二. 安装jdk 三. 安装Tomcat 四. 配置Tomcat监听80端口 五. 配置Tomcat虚拟主机 六. Tomcat日志 七 ...

  9. 谱聚类(Spectral Clustring)原理

    谱聚类(spectral clustering)是广泛使用的聚类算法,比起传统的K-Means算法,谱聚类对数据分布的适应性更强,聚类效果也很优秀,同时聚类的计算量也小很多,更加难能可贵的是实现起来也 ...

  10. vscode里使用.vue代码模板的方法

    1.设置.vue模板 打开编辑器,点击文件 —— 首选项 —— 用户代码片段,会弹出来一个输入框. 在输入框输入vue,回车,会打开一个vue.json文件. 在里面复制以下代码: { "P ...