HDU<1372>/bfs
简单bfs搜索
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> pa;
typedef long long LL;
int dir[8][2]={2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
struct node
{
int x;
int y;
int step;
};
int vis[10][10];//标记数组
int st,sd,et,ed;
queue<node>que;
string c1,c2;
void init()
{
for(int i=0;i<=8;i++)
for(int j=0;j<=8;j++)
vis[i][j]=0;
}
void dfs()
{
node now,next;
now.x=st;
now.y=sd;
vis[st][sd]=1;
now.step=0;
while(!que.empty())
que.pop();
que.push(now);
while(!que.empty())
{
now=que.front();
que.pop();
if(now.x==et&&now.y==ed)
{
cout<<"To get from "<<c1<<" to "<<c2<<" takes "<<now.step<<" knight moves."<<endl;
break;
}
for(int i=0;i<8;i++)
{
int X=now.x+dir[i][0];
int Y=now.y+dir[i][1];
if(X>=1&&X<=8&&Y>=1&&Y<=8&&!vis[X][Y])
{
next.x=X;
next.y=Y;
next.step=now.step+1;
vis[X][Y]=1;
que.push(next);
}
}
}
}
int main ()
{
while(cin>>c1>>c2)
{
init();
st=c1[0]-'a'+1;
sd=c1[1]-'1'+1;
et=c2[0]-'a'+1;
ed=c2[1]-'1'+1;
dfs();
}
return 0;
}
HDU<1372>/bfs的更多相关文章
- hdu 1372 BFS
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the sh ...
- hdu 4531 bfs(略难)
题目链接:点我 第一次不太清楚怎么判重,现在懂了,等下次再做 /* *HDU 4531 *BFS *注意判重 */ #include <stdio.h> #include <stri ...
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves(bfs)
嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点. 因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形) 另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@) ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
- HDOJ/HDU 1372 Knight Moves(经典BFS)
Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where yo ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
随机推荐
- 创建透明CEdit控件
1.在资源编辑器中将Edit控件的Border设为none(当然也可以在代码中修改窗口样式) 2.在重载CEdit的类中响应MSG_OCM_CTLCOLOREDIT消息,在处理函数中将背景模式设为透明 ...
- lucene索引的创建与搜索
package com.cs.multi; import java.io.File;import java.io.IOException; import org.apache.lucene.analy ...
- Pivot Index--Google
Return the pivot index of the given array of numbers. The pivot index is the index where the sum of ...
- 浅谈 zookeeper 原理,安装和配置
当前云计算流行, 单一机器额的处理能力已经不能满足我们的需求,不得不采用大量的服务集群.服务集群对外提供服务的过程中,有很多的配置需要随时更新,服务间需要协调工作,那么这些信息如何推送到各个节点?并且 ...
- SQL Server删除表信息的三种方法
1.使用DELETE实现SQL Server删除表信息 (1)删除表中的全部信息 USE student GO DELETE student --不加where条件,删除表中的所有记录 go ...
- Codeforces Round #375 (Div. 2)A. The New Year: Mee
A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...
- 制作、烧写根文件系统,使用NFS,编译使用驱动程序
制作YAFFS2.JFFS2文件系统映象文件制作文件系统映象非常简单.① 制作YAFFS2文件系统映象文件对于YAFFS2,要用到mkyaffs2image工具,它在/work/linux/tools ...
- HttpUtil工具类
HttpUtil工具类 /** * 向指定URL发送GET方法的请求 * * @param url * 发送请求的URL * @param params * 请求参数,请求参数应该是name1=val ...
- MFC中获取系统当前时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是%Y年% ...
- 第十一节,编辑器软件PyCharm 5.0.3
编辑器软件PyCharm 5.0.3 设置 主题方案 字体大小 行距 文件模板 文件编码 版本切换