博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷 P1443 马的遍历(BFS)
阅读量:4126 次
发布时间:2019-05-25

本文共 1373 字,大约阅读时间需要 4 分钟。

                                                 P1443 马的遍历

题目链接:

题目描述

有一个n*m的棋盘(1<n,m<=400),在某个点上有一个马,要求你计算出马到达棋盘上任意一个点最少要走几步

输入输出格式

输入格式:

一行四个数据,棋盘的大小和马的坐标

输出格式:

一个n*m的矩阵,代表马到达某个点最少要走几步(左对齐,宽5格,不能到达则输出-1)

输入输出样例

输入样例#1: 
3 3 1 1
输出样例#1: 
0    3    2    3    -1   1    2    1    4

题解:直接BFS即可,唯一的wa点是宽五格。

AC代码:

/** @Author: 王文宇* @Date:   2018-03-10 18:16:18* @Last Modified by:   王文宇* @Last Modified time: 2018-03-10 22:33:37*/#include 
using namespace std;const int maxn =405;const int inf = 100000000;#define _for(i,a,b) for(int i=a;i<=b;i++)int n,m,ans,sx,sy,a[maxn][maxn];int xx[] = {1,2,2,1,-1,-2,-2,-1},yy[] = {-2,-1,1,2,2,1,-1,-2};struct node{ int x,y;}b[maxn];queue
Q;int main(int argc, char const *argv[]){ node begin; cin>>n>>m>>begin.x>>begin.y; _for(i,1,n) { _for(j,1,m)a[i][j]=-1; } a[begin.x][begin.y]=0; Q.push(begin); while(!Q.empty()) { node now = Q.front(); Q.pop(); _for(i,0,7) { int nowx = now.x+xx[i]; int nowy = now.y+yy[i]; if(nowx>=1&&nowx<=n&&nowy<=m&&nowy>=1&&a[nowx][nowy]==-1) { a[nowx][nowy]=a[now.x][now.y]+1; node next; next.x=nowx; next.y=nowy; Q.push(next); } } } _for(i,1,n) { _for(j,1,m) { printf("%-5d",a[i][j]); } cout<

转载地址:http://snhpi.baihongyu.com/

你可能感兴趣的文章
phpstorm 集成 xdebug 进行调试
查看>>
npm和node升级的正确方式
查看>>
laravel事务
查看>>
springcloud 连续请求 500
查看>>
vue复用新增和编辑表单
查看>>
Ubuntu 16.04 apt-get更换为国内阿里云源
查看>>
laravel部署到宝塔步骤
查看>>
小程序获取access_token
查看>>
navicat远程连接mysql数据库
查看>>
tp5令牌数据无效 解决方法
查看>>
自己的网站与UCenter整合(大致流程)
查看>>
laravel 制作通用的curd 后台操作
查看>>
【小红书2017年笔试】求一个数组中平均数最大的子数组
查看>>
Linux基础系列-定时器与时间管理
查看>>
Linux基础系列-可执行程序的产生过程
查看>>
Linux基础系列-Kernel 初始化宏
查看>>
Linux子系统系列-I2C
查看>>
<iOS>关于自定义description的一点用法
查看>>
Unix 命令,常用到的
查看>>
DLL中建立进程共享数据段需要注意的语法问题
查看>>