洛谷P1443-马的遍历(BFS)

news/2025/2/25 22:51:19

题目描述:

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

输入格式:

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

输出格式:

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

样例输入: 

3 3 1 1  

样例输出: 

0 3 2

3 -1 1

2 1 4  

AC Code: 

#include<bits/stdc++.h>
using namespace std;
const int N=401;//棋盘大小 
struct node {//定义结构体 
	int x,y;//分别存储队列在该位置的横纵坐标 
}que[N*N];
int vis[N][N];//该数组存储到达每个点的步数 
int dx[]={-1,-2,-2,-1,1,2,2,1};//马为中心,可以扩展出8个点 
int dy[]={2,1,-1,-2,-2,-1,1,2};//即8个方向 
int main() {
	int n,m,sx,sy,head,tail;
	scanf("%d %d %d %d",&n,&m,&sx,&sy);
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			vis[i][j]=-1;//全部初始化为-1 
		}
	}
	vis[sx][sy]=0;//起始点,显然需要0步 
	head=tail=1;//队头队尾初始化 
	que[head].x=sx;//起始点入队 
	que[head].y=sy;
	tail++;//起始点入队,队尾向后移动一格 
	while(head<tail) {
		int s=vis[que[head].x][que[head].y]+1;//扩展到下一个点的步数,对应第33行的代码语句 
		for(int i=0;i<8;i++) {//8个方向搜索 
			int tx=que[head].x+dx[i];
			int ty=que[head].y+dy[i];
			if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&vis[tx][ty]==-1) {
			//没有走出棋盘且这个点没有走过 
				que[tail].x=tx;//新的点入队 
				que[tail].y=ty;
				tail++;//同时队尾向后移动一格 
				vis[tx][ty]=s;//由上一个点扩展到该点,步数加1,对应第24行的代码语句 
			}
		}
		head++;//队头向后移动一格,搜索下一个点 
	}
	for(int i=1;i<=n;i++) {
		for(int j=1;j<=m;j++) {
			printf("%-5d",vis[i][j]);//注意格式,左对齐,占5列 
		}
		printf("\n");
	}
	return 0;
}


http://www.niftyadmin.cn/n/712644.html

相关文章

HDU 2050:折线分割平面(找规律,递推)

题目地址&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2050 此题是有规律的: f(n)2n2−n1可以参考《具体数学》&#xff0c;即《Concrete Mathematics》1.2节 #include <iostream> using namespace std;int main() {int cases, n;cin >> cases;while …

php标准输出文件功能

为什么80%的码农都做不了架构师&#xff1f;>>> <?php $file monkey.gif;if (file_exists($file)) {header(Content-Description: File Transfer);header(Content-Type: application/octet-stream);header(Content-Disposition: attachment; filename.basenam…

hadoop集群不用重启集群使配置生效及配置查询

1、修改配置生效 hadoop dfsadmin -refreshNodes 2、查看配置 hdfs:http://172.16.10.10:50070/conf yarn:http://172.16.10.10:8088/conf 转载于:https://www.cnblogs.com/stone1989/p/10840813.html

UVA439-骑士的移动 Knight Moves(BFS)

AC Code&#xff1a; #include<bits/stdc.h> using namespace std; #define N 101 struct node {//定义结构体 int x,y,s;//分别为该点的横坐标、纵坐标、步数 }que[N*N]; int book[N][N];//标记数组 int dx[]{-1,-2,-2,-1,1,2,2,1};//马可以向8个方向移动 int dy[]…

高并发高流量网站架构

2019独角兽企业重金招聘Python工程师标准>>> Web2.0的兴起&#xff0c;掀起了互联网新一轮的网络创业大潮。以用户为导向的新网站建设概念&#xff0c;细分了网站功能和用户群&#xff0c;不仅成功的造就了一大批新生的网站&#xff0c;也极大的方便了上网的人们。但…

HDU 2084:数塔(动态规划)

题目地址&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2084 很简单的DP #include <iostream> #include <memory.h> #define MAXHEIGHT 105 using namespace std; int d[MAXHEIGHT][MAXHEIGHT]; int nums[MAXHEIGHT][MAXHEIGHT]; int cases, height;in…

pep8 python 编码方式_PEP8 python 编码规范整理

决定开始Python之路了&#xff0c;利用业余时间&#xff0c;争取更深入学习Python。编程语言不是艺术&#xff0c;而是工作或者说是工具&#xff0c;所以整理并遵循一套编码规范是十分必要的。PEP8 python 编码规范一.代码编排1.缩进、4个空格的缩进(编辑器都可以完成此功能)&a…

【JVM】学习JVM垃圾回收理论

参考链接&#xff1a;https://www.cnblogs.com/aspirant/p/8662690.html 一&#xff0c;垃圾回收算法 JVM内存结构&#xff1a;程序计数器、虚拟机栈、本地方法栈、堆区、方法区 1&#xff0c;引用计数法&#xff1a; 早期JAVA使用的算法 原理&#xff1a;当对象被创建时&#…