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, ...
随机推荐
- Linux C/C++编译环境搭建
1. 配置GCC,LInux 在安装后已经有GCC了,但可能文件不全,我们可以利用 sudo apt-get install build-essential 安装 2. 安装文本编辑器 sudo ap ...
- osg蝴蝶纹理
#include <osgViewer/Viewer> #include <osgDB/WriteFile> #include <osg/StateSet> #in ...
- 自己写的POIUtil,主要解决从不同的HSSFWorkbook复制sheet以及根据单元格插入图片等
复制sheet的原始代码网上找的,但是小问题很多,然后自己动手改了一下: 根据单元格信息动态插入图片,如果单元格有文字,图片的位置会在文字之后,如果同样的位置已有图片则会往下插入. import or ...
- 基于多线程多用户的FTP服务器与客户端功能实现
项目介绍: 用户加密认证 允许同时多用户登录 每个用户有自己的家目录 ,且只能访问自己的家目录 对用户进行磁盘配额,每个用户的可用空间不同 允许用户在ftp server上随意切换目录 允许用户查看当 ...
- iOS开发的一些奇巧淫技(转载)
iOS开发的一些奇巧淫技 http://www.cocoachina.com/ios/20141229/10783.html iOS开发的一些奇巧淫技2 http://www.cocoachina.c ...
- MTK平台Android项目APK预置方案
项目开发中,通常需要向系统中预置一些APK,这里简单介绍一下MTK平台预置APK的方法. 需要预置的apk可以放置在目录:vendor/mediate/${Project}/artifacts/out ...
- Linux CPU 核数检查脚本
#!/bin/bash physicalNumber=0 coreNumber=0 logicalNumber=0 HTNumber=0 logicalNumber=$(grep "proc ...
- 当用反射获取一个model,这个model里面字段有nullable的时候,获取字段真实类型
Using Reflection to Determine whether an Type is Nullable And Get the underlying Type /// <summar ...
- dedecms 自定义标签的方法
function lib_demotest(&$ctag,&$refObj) { global $dsql,$envs; //属性处理 $attlist="row|12,ti ...
- Android中的selector
android背景选择器selector用法汇总 (2011-04-19 13:40:00) 转载▼ 标签: android selector 背景选择器 it 分类: java/vb/Android ...