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, ...
随机推荐
- 禁止使用test类的就是禁止使用本来的$this对象.可以调用父类的对象
public function __construct() { parent::__construct( ); parent::__construct( ); if(!APP_DEBUG ) die( ...
- LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- kvstore之memcached为存储介质
ecstore中kvstore选用memcached作为存储介质 kvstore存储类选用base_kvstore_memcached(app/base/lib/kvstore/memcached.p ...
- Python 修炼1
2016年11月21日 Python基础修炼第一篇 1.Python是什么?有什么优缺点呢? python是一个高级编程语言. 优点:开发效率比较高,不但有php写网页的功能,还有写后台的功能 缺点: ...
- 在自学php的路上不知道怎么走!!
在自学php的路上不知道怎么走!! 真希望有人给我指点一二!!!
- iosUISegmentedControl的基本设置
//创建segmentControl 分段控件 UISegmentedControl *segC = [[UISegmentedControl alloc]initWithFrame:CGRectMa ...
- 解题的小问题(C++)
1.判断一个数是否为整数 if(m==(int)m) 2.#include <bits/stdc++.h>using namespace std;int main(){ int n ...
- vb asp.net的一些属性值
AutoGenerateColumns 就是自动产生列的意思gridview等控件,如果设置了AutoGenerateColumns=true,就能够根据数据源的实际情况,自动生成gridview表格 ...
- aspx中如何加入javascript
Response.Write("<script>window.location ='aaa.aspx';</script>"); Response.Writ ...
- jQuery的dataTables插件实现中文排序
最近在写Java web. 写JSP的时候发现一个很好玩的插件dataTables.分页.过滤.排序等等手到擒来. 哎哎哎,有点点可惜的是排序这个功能不支持中文.于是网上查查找找,现在把方法整理一下, ...