UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=380
做了这道题之后对BFS总算是有了点认识了。
#include<iostream>
#include<cstring>
using namespace std; int map[][];
typedef struct node
{
int x, y, z;
node(int a, int b, int c) { x = a; y = b; z = c; }
node() {}
}queue; queue q[]; int d[][] = { { , }, { , - }, { -, - }, { -, }, { , }, { , - }, { -, }, { -, - } };
int bfs(int x1, int y1, int x2, int y2)
{
int move = , number = ;
if (x1 == x2 && y1 == y2) return ;
memset(map, , sizeof(map));
q[] = node(x1, y1, );
while (move < number)
{
queue p = q[move++];
for (int k = ; k < ; k++)
{
int xx = p.x + d[k][];
int yy= p.y + d[k][];
if (xx == x2 && yy == y2) return p.z + ;
if (!map[xx][yy] && xx> && xx< && yy> && yy < )
{
map[xx][yy] = ;
q[number++] = node(xx, yy, p.z + );
}
}
}
} int main()
{
char s1, s2;
int x1, x2, y1, y2;
while (cin >> s1 >> y1 >> s2 >> y2)
{
x1 = (int)(s1 - 'a' + );
x2 = (int)(s2 - 'a' + );
int x=bfs(x1, y1, x2, y2);
cout << "To get from "<<s1<<y1<< " to " <<s2<<y2<< " takes "<<x <<" knight moves."<< endl;
}
return ;
}
UVa 439骑士的移动(BFS)的更多相关文章
- UVA 439 Knight Moves(BFS)
Knight Moves option=com_onlinejudge&Itemid=8&category=11&page=show_problem&problem=3 ...
- UVA 816 -- Abbott's Revenge(BFS求最短路)
UVA 816 -- Abbott's Revenge(BFS求最短路) 有一个 9 * 9 的交叉点的迷宫. 输入起点, 离开起点时的朝向和终点, 求最短路(多解时任意一个输出即可).进入一个交叉 ...
- uva 439 Knight Moves 骑士移动
这道题曾经写过,bfs.用队列,不多说了,上代码: #include<stdio.h> #include<stdlib.h> #include<string.h> ...
- UVA 439 Knight Moves --DFS or BFS
简单搜索,我这里用的是dfs,由于棋盘只有8x8这么大,于是想到dfs应该可以过,后来由于边界的问题,TLE了,改了边界才AC. 这道题的收获就是知道了有些时候dfs没有特定的边界的时候要自己设置一个 ...
- UVa 11624,两次BFS
题目链接:http://vjudge.net/contest/132239#problem/A 题目链接:https://uva.onlinejudge.org/external/116/11624. ...
- 骑士问题(knight) (BFS)
题目描述 在一个标准8×8的国际象棋棋盘上,棋盘中有些格子可能是有障碍物的.已知骑士的初始位置和目标位置,你的任务是计算出骑士最少需要多少步可以从初始位置到达目标位置.有障碍物的格子当然不可以到达. ...
- uva 11234 Expressions 表达式 建树+BFS层次遍历
题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...
- UVA 810 - A Dicey Problem(BFS)
UVA 810 - A Dicey Problem 题目链接 题意:一个骰子,给你顶面和前面.在一个起点,每次能移动到周围4格,为-1,或顶面和该位置数字一样,那么问题来了,骰子能不能走一圈回到原地, ...
- UVA 11573 - Ocean Currents(BFS+优先队列)
UVA 11573 - Ocean Currents 题目链接 题意:给定一个海面.数字分别代表海流方向,顺着海流不用费能量,逆海流要费1点能量,每次询问给一个起点一个终点,问起点到终点耗费的最小能量 ...
随机推荐
- 智能设备逆向工程之外部Flash读取与分析篇
智能设备逆向工程之外部Flash读取与分析篇 唐朝实验室 · 2015/10/19 11:19 author: rayxcp 0x00 前言 目前智能家居设备的种类很多,本文内容以某智能豆浆机为例完成 ...
- Nginx模块之————RTMP模块的FFmpeg的配置问题是FFmpeg的连续退出
rtmp { server { listen ; application live { allow publish all; allow play all; live on; exec /root/b ...
- python uuid、hex study
由 import uuid product[“SourceInfo"]["ProductID"] = uuid.uuid4().hex 引起的uuid 一.概述 uuid ...
- Python简单时间日期处理
import datetime #日期初始化: d1 = datetime.datetime(2005, 2, 16) d2 = datetime.datetime(2004, 12, 31) #日期 ...
- [css] CSS相对定位|绝对定位
第一篇链接:http://www.zhangxinxu.com/wordpress/2010/12/css-%E7%9B%B8%E5%AF%B9%E7%BB%9D%E5%AF%B9%E5%AE%9A% ...
- php 判断是手机版还是电脑端
function isMobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) { ...
- First Day
以后这里将记录我的成长脚步啦~~ 欢迎吐槽 作为一个大三即将面临找工作的学渣,心中真的很焦急 要好好学前端!! Fighting~
- laravel开发微信公众号1 之基本配置
需要用到的packagist: https://github.com/overtrue/laravel-wechat ( 目前最优雅的laravel微信sdk) 首先安装 compose ...
- Spring知识点
IOC: Inversion of Control 控制反转 ①自己控制→容器控制 ②控制具体实现→控制抽象(接口) DI:Dependency Injection 依赖注入 依赖于容器注入的对象 注 ...
- guava学习--FluentIterable
public class FluentIterableTest { public static void main(String[] args) { Man man1 = new Man(" ...