HDU 1372 (搜索方向稍有改变) Knight Moves
其实手写模拟一个队列也挺简单的,尤其是熟练以后。
尼玛,这题欺负我不懂国际象棋,后来百度了下,国际象棋里骑士的走法就是中国象棋里面的马
所以搜索就有八个方向
对了注意初始化标记数组的时候,不要把起点标记为已走过。
因为测试数据里面有一组 f6 f6,此时样例输出的是0
//#define LOCAL
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std; struct Point
{
int x, y;
int steps;
}start, end, qu[]; int vis[][];
int dir[][] = {{, }, {-, }, {, -}, {-, -}, {, }, {-, }, {, -}, {-, -}};
char col1, col2;
int row1, row2;
int head, tail; bool islegal(int x, int y)
{
return (x>= && x< && y>= && y< && (!vis[x][y]));
} void BFS(void)
{
head = , tail = ;
qu[].x = start.x;
qu[].y = start.y;
qu[].steps = ;
while(head < tail)
{
if(qu[head].x == end.x && qu[head].y == end.y)
{
printf("To get from %c%d to %c%d takes %d knight moves.\n", col1, row1, col2, row2, qu[head].steps);
return;
}
for(int i = ; i < ; ++i)
{
int xx = qu[head].x + dir[i][];
int yy = qu[head].y + dir[i][];
if(islegal(xx, yy))
{
qu[tail].x = xx;
qu[tail].y = yy;
qu[tail++].steps = qu[head].steps + ;
vis[xx][yy] = ;
}
}
++head;
}
} int main(void)
{
#ifdef LOCAL
freopen("1372in.txt", "r", stdin);
#endif while(cin >> col1 >> row1 >> col2 >> row2)
{
start.x = row1 - , start.y = col1 - 'a';
end.x = row2 - , end.y = col2 - 'a';
memset(vis, , sizeof(vis));
BFS();
}
return ;
}
代码君
HDU 1372 (搜索方向稍有改变) Knight Moves的更多相关文章
- 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
最近在学习广搜 这道题同样是一道简单广搜题=0= 题意:(百度复制粘贴0.0) 题意:给出骑士的骑士位置和目标位置,计算骑士要走多少步 思路:首先要做这道题必须要理解国际象棋中骑士的走法,国际象棋中 ...
- [宽度优先搜索] HDU 1372 Knight Moves
Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- 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, ...
随机推荐
- cache应用(asp.net 2.0 SQL数据缓存依赖 [SqlCacheDependency ] )
Asp.net 2.0 提供了一个新的数据缓存功能,就是利用sql server2005 的异步通知功能来实现缓存 1.首先在sqlserver2005 中创建一个test的数据库. 在SQL Ser ...
- asp.net各种类型视频播放代码(全)
1.avi格式 代码片断如下: <object id="video" width="400" height="200" border= ...
- Java正则表达式匹配例子
Java正则表达式匹配例子 package com.ibm.test; import java.util.regex.Matcher; import java.util.regex.Pattern; ...
- HDOJ 1429 胜利大逃亡(续)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- mysql SQLyog导入导出csv文件
1.选择数据库表 --> 右击属性 --> 备份/导出 --> 导出表数据作为 --> 选择cvs --> 选择下面的“更改” --> 字段 --> 变量长度 ...
- 移动平台WEB前端开发技巧汇总
原文 :http://uecss.com/mobile-platform-web-front-end-development-skills-summary.html 开发者们都知道在高端智能手机系统中 ...
- NodeJS异常处理uncaughtException篇
很多 NodeJS 的开发者在抱怨异常处理太麻烦,我们会通过一些列博客梳理一下NodeJS中常见的异常处理的手段. 和大多数编程语言一样,在 NodeJS 里可以通过throw抛出一个异常: thro ...
- SGU 107
107. 987654321 problem time limit per test: 0.25 sec. memory limit per test: 4096 KB For given numbe ...
- JDBC 程序的常见错误及调试方法
详细介绍:http://dev.mysql.com/doc/refman/5.5/en/error-handling.html http://dev.mysql.com/doc/refman/5.5/ ...
- 关于解压覆盖IIS文件后,新的文件不具备权限导致DMS系统无法正常运行
向DMS的服务器端站点bin目录覆盖任何补丁文件,请注意:Web站点的bin目录中的文件,IIS的服务进程(Windows2003以上,都是对应Network Services账户)必须对这些文件具 ...