nyoj 58-最少步数

这是一道简单的搜索问题,通过无脑广度优先搜索即可轻松解决。博主分享了以前的解题经验,并提到使用STL可以显著减少代码量和提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

点击打开链接

最少步数

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述

这有一个迷宫,有0~8行和0~8列:

 1,1,1,1,1,1,1,1,1
 1,0,0,1,0,0,1,0,1
 1,0,0,1,1,0,0,0,1
 1,0,1,0,1,1,0,1,1
 1,0,0,0,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,1,0,0,1
 1,1,0,1,0,0,0,0,1
 1,1,1,1,1,1,1,1,1

0表示道路,1表示墙。

现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?

(注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从(3,1)到(4,1)。)

输入
第一行输入一个整数n(0<n<=100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0<=a,b,c,d<=8)分别表示起点的行、列,终点的行、列。
输出
输出最少走几步。
样例输入
2
3 1  5 7
3 1  6 7
样例输出
12
11

水题一道,无脑广搜就过了吧,以前写的,没什么可说的,有点对不起这个难度,现在看用stl代码可以减少一半以上

 
#include<stdio.h>
int map[9][9] = {1,1,1,1,1,1,1,1,1,
 1,0,0,1,0,0,1,0,1,
 1,0,0,1,1,0,0,0,1,
 1,0,1,0,1,1,0,1,1,
 1,0,0,0,0,1,0,0,1,
 1,1,0,1,0,1,0,0,1,
 1,1,0,1,0,1,0,0,1,
 1,1,0,1,0,0,0,0,1,
 1,1,1,1,1,1,1,1,1};
int queue[100][3] , haxi[9][9];
int head , tail;
int i , start_x , start_y , end_x , end_y;
void bfs()
{
	while(head != tail)
	{
		if(queue[head][0] > 0 && map[queue[head][0] - 1][queue[head][1]] == 0 && haxi[queue[head][0] - 1][queue[head][1]] == 0)
		{
			queue[tail][0] = queue[head][0] - 1;
			queue[tail][1] = queue[head][1];
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		if(queue[head][0] < 8 && map[queue[head][0] + 1][queue[head][1]] == 0 && haxi[queue[head][0] + 1][queue[head][1]] == 0)
		{
			queue[tail][0] = queue[head][0] + 1;
			queue[tail][1] = queue[head][1];
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		if(queue[head][1] > 0 && map[queue[head][0]][queue[head][1] - 1] == 0 && haxi[queue[head][0]][queue[head][1] - 1] == 0)
		{
			queue[tail][0] = queue[head][0];
			queue[tail][1] = queue[head][1] - 1;
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		if(queue[head][1] < 8 && map[queue[head][0]][queue[head][1] + 1] == 0  && haxi[queue[head][0]][queue[head][1] + 1] == 0)
		{
			queue[tail][0] = queue[head][0];
			queue[tail][1] = queue[head][1] + 1;
			queue[tail][2] = queue[head][2] + 1;
			haxi[queue[tail][0]][queue[tail][1]] = 1;
			if(queue[tail][0] == end_x && queue[tail][1] == end_y)
			{
				printf("%d\n" , queue[tail][2]); 
				return ;
			}
			tail++;
		}
		head++;
	}
	
}

int find_way(int x , int y)
{


	head = 0 ;
	tail = 1;
	queue[head][0] = start_x;
	queue[head][1] = start_y;
	haxi[queue[head][0]][queue[head][1]] = 1;
	bfs();
	if(head == tail)
		return 0;
	else return 1;
}
int main()
{

	
	int x,y;

	scanf("%d" , &i);
	while(i--)
	{
		scanf("%d %d %d %d" , &start_x , &start_y , &end_x , &end_y);
		for(x = 0 ; x < 9 ; x++)
			for(y = 0 ; y < 9 ; y++)
				haxi[x][y] = 0;
		if(find_way(start_x , start_y) == 0)
			printf("0\n");
	}
	return 0;
}        


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇敢的炮灰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值