简单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. learning websocket protocol

    websocket的产生背景: 众所周知,Web应用的通信过程通常是客户端通过浏览器发出一个请求,服务器端接收请求后进行处理并返回结果给客户端,客户端浏览器将信息呈现.这种机制对于信息变化不是特别频繁 ...

  2. 关于在UNIcode环境下得TCHAR转string类型以及string转TCHAR

    using namespace System::Text: String ^TCHARtoStr(TCHAR *temp) //TCHAR转String { array<unsigned cha ...

  3. js实现原生选项卡

        <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...

  4. springboot秒杀课程学习整理1-5

    1)交易模型设计 交易模型(用户下单的交易模型)OrderModel id(String 交易单号使用String), userId,itemId,amount(数量),orderAmount(总金额 ...

  5. rebar3自动编译

    功能:修改完代码可以自动编译加载到VM中 必须安装的软件: Linux: inotify  链接https://github.com/rvoicilas/inotify-tools/wiki 配置: ...

  6. django自定义模板标签

    # 创建自定义模板标签目录 django_project_name app_name templatetags (创建Python Packge,注意一定要用templatetags这个名字) my_ ...

  7. unity中编辑器直接截屏代码

    using UnityEngine; using System.Collections; using System.Windows.Forms; public class screenshots : ...

  8. 无聊的js(马赛克)

    <!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...

  9. C++中_T的用途

    1    C++语言中“_T”是什么意思? Visual C++里边定义字符串的时候,用_T来保证兼容性,VC支持ascii和unicode两种字符类型,用_T可以保证从ascii编码类型转换到uni ...

  10. idea 工具中项目文件上有灰色的小X号去除方法

    初使用idea,在项目中发现类上有这样的灰色X号,启动项目后idea会报找不到这个类的错误,原因是它没有被编译, 解决方法 setting->Build->Compiler->Exc ...