HDU 1372 Knight Moves【BFS】
题意:给出8*8的棋盘,给出起点和终点,问最少走几步到达终点。
因为骑士的走法和马的走法是一样的,走日字形(四个象限的横竖的日字形)
另外字母转换成坐标的时候仔细一点(因为这个WA了两次---@_@)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxn 105
using namespace std;
struct node
{
int x,y;
int step;
} now,next,st,en;
queue<node>q;
char m,n;
int vis[maxn][maxn];
int dir[][]={{,},{,},{-,},{-,},{-,-},{-,-},{,-},{,-}};
void bfs(int x,int y)
{
now.x=x;now.y=y;now.step=;
while(!q.empty()) q.pop();
q.push(now);
vis[x][y]=;
while(!q.empty())
{
now=q.front();q.pop();
if(now.x==en.x&&now.y==en.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,now.step);
return;
};
for(int i=;i<;i++)
{
next.x=now.x+dir[i][];
next.y=now.y+dir[i][];
if(next.x<||next.y>||next.y<||next.y>) continue;
if(!vis[next.x][next.y])
{
vis[next.x][next.y]=;
next.step=now.step+;
q.push(next);
if(next.x==en.x&&next.y==en.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n",m,st.y,n,en.y,next.step);
return;
}
}
}
}
}
int main()
{
while(cin>>m>>st.y>>n>>en.y)
{
memset(vis,,sizeof(vis));
st.x=m-'a'+;
en.x=n-'a'+;
bfs(st.x,st.y);
}
}
HDU 1372 Knight Moves【BFS】的更多相关文章
- 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 ...
- 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 ...
- HDU 1372 Knight Moves(最简单也是最经典的bfs)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- (step4.2.1) hdu 1372(Knight Moves——BFS)
解题思路:BFS 1)马的跳跃方向 在国际象棋的棋盘上,一匹马共有8个可能的跳跃方向,如图1所示,按顺时针分别记为1~8,设置一组坐标增量来描述这8个方向: 2)基本过程 设当前点(i,j),方向k, ...
- HDU 1372 Knight Moves(BFS)
题目链接 Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) whe ...
- HDU 1372 Knight Moves
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- javascript实现数据结构与算法系列:队列 -- 链队列和循环队列实现及示例
1 队列的基本概念 队列(Queue):也是运算受限的线性表.是一种先进先出(First In First Out ,简称FIFO)的线性表.只允许在表的一端进行插入,而在另一端进行删除. 队首(fr ...
- this的使用、继承、super
1.this的使用 1)可以用于区分局部变量 Person(int age,string name) { this.age=age; this.name=name; } 2)构造方法中,用this调用 ...
- iOS多线程的初步研究(十)-- dispatch同步
GCD提供两种方式支持dispatch队列同步,即dispatch组和信号量. 一.dispatch组(dispatch group) 1. 创建dispatch组 dispatch_group_t ...
- 理解 JMeter 聚合报告(Aggregate Report)
Aggregate Report 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”.今天再次有同行问到这个报告中的各项数据表示什么意思,顺便在这里公布一下,以备大家查阅. 如果 ...
- java语言写文件内容
import java.io.File;import java.io.FileWriter;import java.io.IOException; public static void main(St ...
- 常用搜索引擎的算分,你get了嘛?
搜索引擎发展至今,已公布了多种算法.作为SEOER的你,还不懂,就out啦.懂了不会用,也是然并卵的一种行为.了解算法知识并不懂得如何把算法实践于SEO工作的你,还是处于学生思维,是时候该升级了.且听 ...
- MongoDB (二) MongoDB 优点
任何关系型数据库,具有典型的架构设计,显示表和这些表之间的关系.虽然在 MongoDB中,没有什么关系的概念. MongoDB比RDBMS的优势 架构:MongoDB是文档型数据库,其中一个集合保存不 ...
- 使用RockMongo管理MongoDB
http://blog.csdn.net/mydeman/article/details/7082730
- 线段树(区间合并) POJ 3667 Hotel
题目传送门 /* 题意:输入 1 a:询问是不是有连续长度为a的空房间,有的话住进最左边 输入 2 a b:将[a,a+b-1]的房间清空 线段树(区间合并):lsum[]统计从左端点起最长连续空房间 ...
- Linux下find一次查找多个指定类型文件,指定文件或者排除某类文件,在 GREP 中匹配多个关键 批量修改文件名等
http://blog.sina.com.cn/s/blog_62e7fe670101dg9d.html linux下二进制文件查找: strings 0000.ts | grep -o " ...