UVA439 Knight Moves
题目大意
有一个骑士,他可以骑马日字型跳跃,问他从A点到B点最少要几步
解题思路
这题就是一个特别裸的广搜板子
它的主要问题在于输入输出
输入的数据我们可以用\(pair\)读入,第一关键字存行(a~e),第二关键字存列(1 ~ 8)
然后我们为了方便处理,把行也映射成数组1 ~ 8
所以有了我们的读入代码
预编译指令
#define x first
#define y second
定义的\(A, B\)
pair<char, int> A, B;
while (cin >> A.x >> A.y >> B.x >> B.y)
{
int sx = A.x - 'a' + 1, sy = A.y,
fx = B.x - 'a' + 1, fy = B.y;
//printf这句是输出
然后就是巨坑无比的输出了
一定要有句号!!!!!!!!!
代码
#include <iostream>
#include <cstring>
#include <queue>
#define x first
#define y second
using namespace std;
struct Point
{
int x, y, dist;
Point(int a = 0, int b = 0, int c = 0) : x(a), y(b), dist(c) {}
} ;
pair<char, int> A, B;
int mp[9][9];
int bfs(int& sx, int& sy, int& fx, int& fy)
{
if (sx == fx && sy == fy) return 0;
queue<Point> q;
int dx[8] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[8] = {1, 2, 2, 1, -1, -2, -2, -1};
q.push(Point(sx, sy, 0));
while (q.size())
{
Point t = q.front(); q.pop();
for (int i = 0; i < 8; i ++ )
{
int x = t.x + dx[i], y = t.y + dy[i];
if (x < 1 || x > 8 || y < 1 || y > 8) continue ;
if (mp[x][y]) continue ;
Point now = Point(x, y, t.dist + 1);
if (now.x == fx && now.y == fy) return now.dist;
q.push(now);
}
}
}
int main()
{
while (cin >> A.x >> A.y >> B.x >> B.y)
{
int sx = A.x - 'a' + 1, sy = A.y,
fx = B.x - 'a' + 1, fy = B.y;
printf("To get from %c%d to %c%d takes %d knight moves.\n",
A.x, A.y, B.x, B.y, bfs(sx, sy, fx, fy));
}
return 0;
}
Accepted!
UVA439 Knight Moves的更多相关文章
- uva439 - Knight Moves(BFS求最短路)
题意:8*8国际象棋棋盘,求马从起点到终点的最少步数. 编写时犯的错误:1.结构体内没构造.2.bfs函数里返回条件误写成起点.3.主函数里取行标时未注意书中的图. #include<iostr ...
- UVA 439 Knight Moves
// 题意:输入标准国际象棋棋盘上的两个格子,求马最少需要多少步从起点跳到终点 BFS求最短路: bfs并维护距离状态cnt, vis记录是否访问过 #include<cstdio> ...
- 【习题 6-4 UVA-439】Knight Moves
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] bfs模板题 [代码] /* 1.Shoud it use long long ? 2.Have you ever test sev ...
- UVA439 骑士的移动 Knight Moves
#include<bits/stdc++.h> using namespace std; char a,c; int b,d; ][]; ]={,,,-,,-,-,-}; ]={,-,,, ...
- UVA-439, Knight Moves(深度优先搜索)
#include<iostream> #include<queue> #include<cstring> #include<string> #inclu ...
- 题解 UVA439 骑士的移动 Knight Moves
前言 最近板子题刷多了-- 题意 一个 \(8\times 8\) 的棋盘,问马从起点到终点的最短步数为多少. \(\sf Solution\) 要求最短路径嘛,显然 bfs 更优. 读入 这个读入处 ...
- Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...
- 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 ...
随机推荐
- 驱动通信:通过PIPE管道与内核层通信
在本人前一篇博文<驱动开发:通过ReadFile与内核层通信>详细介绍了如何使用应用层ReadFile系列函数实现内核通信,本篇将继续延申这个知识点,介绍利用PIPE命名管道实现应用层与内 ...
- C#-8 数组
一 关于数组 数组是由一个变量名称表示的一组同类型的数据元素.数组中的元素通过变量名和方括号索引来访问. int[] intArray = new int[] { 1, 2, 3 }; //声明了一个 ...
- SSM整合以及相关补充
SSM整合以及相关补充 我们在前面已经学习了Maven基本入门,Spring,SpringMVC,MyBatis三件套 现在我们来通过一些简单的案例,将我们最常用的开发三件套整合起来,进行一次完整的项 ...
- linux操作系统运行一个java程序并外网访问
(一)安装jdk 1.新建文档java : mkdir java 2.进入java并且下载jdk 下载jdk : wget --no-check-certificate --no-cooki ...
- C语言编译环境中的 调试功能及常见错误提示
文章目录 1 .调试功能 2 . 编译中的常见错误例析 3 .常见错误信息语句索引 1 .调试功能 1.常用健 <F10> : 激活系统菜单 <F6> : 将光标在编辑窗口和. ...
- cmd中pip加速的方法
临时加速: pip install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple/ 永久加速: 在user文件夹里新建pip文件夹,再建pip.in ...
- CF Round #829 题解 (Div. 2)
F 没看所以摆了 . 看拜月教教主 LHQ 在群里代打恰钱 /bx 目录 A. Technical Support (*800) B. Kevin and Permutation (*800) C. ...
- Centos镜像下载
1.进入官网,并点击下图所示的红框(alternative downloads) 官网网址:https://www.centos.org/download/ 2.在往下翻,可以看到如下图的历史版本, ...
- Python 包(package)
在比较大型的项目中常常需要编写.用到大量的模块,此时我们可以使用包(Package)来管理这些模块. (一)什么是包? Python包,就是里面装了一个__init__.py文件的文件夹. __ini ...
- AdsStream的使用
本例子是测试ads通信的. 1.首先添加TwinCAT.Ads引用 using System; using System.Collections.Generic; using System.Compo ...