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, ...
 
随机推荐
- C#正则表达式大全{转}
			
只能输入数字:"^[0-9]*$". 只能输入n位的数字:"^\d{n}$". 只能输入至少n位的数字:"^\d{n,}$". 只能输入m~ ...
 - Ambient Occlusion
			
一般在光照模型中,ambient light的计算方法为:A = l * m,其中l表示表面接收到的来自光源的ambient light的总量,而m表示表面接收到ambient light后,反射和吸 ...
 - 解高次同余方程  (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法
			
先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝 扩展Baby Step Gian ...
 - laravel笔记
			
向视图中传递变量 使用with()方法 return view('articles.lists')->with('title',$title); 直接给view()传参数 return view ...
 - 【leetcode】Multiply Strings(middle)
			
Given two numbers represented as strings, return multiplication of the numbers as a string. Note: Th ...
 - iOS音效
			
//AudioToolbox.framework是一套基于C语言的框架,使用它来播放音效其本质是将短音频注册到系统声音服务(System Sound Service) //System Sound S ...
 - 概述Log4j简介
			
在强调可重用组件开发的今天,除了自己从头到尾开发一个可重用的日志操作类外,Apache为我们提供了一个强有力的日志操作包-Log4j. Log4j是Apache的一个开放源代码项目,通过使用Log4j ...
 - Codeforces Round #259 (Div. 2) C - Little Pony and Expected Maximum (数学期望)
			
题目链接 题意 : 一个m面的骰子,掷n次,问得到最大值的期望. 思路 : 数学期望,离散时的公式是E(X) = X1*p(X1) + X2*p(X2) + …… + Xn*p(Xn) p(xi)的是 ...
 - Linux Shell查看磁盘分区,内存使用,CPU使用率
			
Linux Shell查看磁盘分区,内存使用,CPU使用率 #!/bin/bash #disk_used_rate Location=/dev/xvdb Disk_Used_Rate=$(df -h ...
 - iOS 开发--动画
			
在iOS开发中,制作动画效果是最让开发者享受的环节之一.一个设计严谨.精细的动画效果能给用户耳目一新的效果,吸引他们的眼光 —— 这对于app而言是非常重要的.我们总是追求更为酷炫的实现,如果足够仔细 ...