NYOJ-58-最小步数(搜索)

最少步数

时间限制: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

搜索的典型题目,两种方法都能做。

1.BFS 广度

#include<cstdio>
#include<cstring>											
#include<algorithm>
#include<queue>
#include<cstring> 
using namespace std;
int dir[4][2] = {1,0,-1,0,0,1,0,-1};//注 1 
struct node
{
	int x;
	int y;
	int step;
};

int bfs ( node start, node end)
{
	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,
					};
	queue <node> q;
	node current;
	node next;
	map[start.x][start.y] = 1;//起点要做好标记,因为不能回去了,相当于走过 
	q.push(start);
	while(!q.empty())
 {
	current = q.front();		//	当前的node 为队首的以一个结构体元素 
	q.pop();							//	清空首位置的结构体元素,留下位置 
	if(current.x == end.x && current.y == end.y) //	如果满足条件,即找到出口,则返回当前步数也即是minstep 
	{
		return current.step;
	}
	for(int i = 0 ; i < 4; i++ )				//		四个方向遍历,找到下一个可能走得位置 
	{
		next.x = current.x + dir[i][0];//注 1 :	 这里的 0 和 1 是“列 ”的意思(L && R),
		next.y = current.y + dir[i][1];//是0代表第一列 1 代表第二列 
		if(map[next.x][next.y] == 0)
		{				
			next.step = current.step + 1;	//下一个格子如果可以走的话,当前步数要加 1  
			map[next.x][next.y] = 1;
			q.push(next); 				//		再次进队 
		}
   	}
   }	
}
int main()
{
	int T;
	int minstep;
	scanf("%d",&T);
	while(T--)
	{
		node start;
		node end;
		scanf("%d%d%d%d",&start.x,&start.y,&end.x,&end.y);
		start.step = 0;	
		minstep = bfs(start,end);
		printf("%d\n",minstep);	
	}
	
	return 0;
}


2.DFS算法



#include<stdio.h>
#define ACCESS 0
#define IMPASSABILITY 1
#define INFINITE 1000

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 dir[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int exitX,exitY;
void dfs(int currentX, int currentY,int currentstep, int minstep)
{
	int nextstepX,nextstepY;
	if(currentX == exitX && currentY == exitY)
	{
		if(currentstep < minstep)
		{
			minstep = currentstep;
		}
		return;
	}
	map[currentX][currentY] = IMPASSABILITY;
	for(int i = 0; i < 4; i++)
	{
		nextstepX = currentX + dir[i][0];
		nextstepY = currentY + dir[i][1];
		if(map[nextstepX][nextstepY] == ACCESS && currentstep+1 < minstep)
		{
			dfs(nextstepX,nextstepY,currentstep+1,minstep);
			map[nextstepX][nextstepY] = ACCESS;
		}
	}
}
 int main()
 {
 	int T,startX,startY,minstep;
 	scanf("%d",&T);
 	while(T--)
 	{
 		scanf("%d%d%d%d",&startX,&startY,&exitX,&exitY);
 		minstep = INFINITE;
 		dfs(startX,startY,0,minstep);
 		map[startX][startY] = ACCESS;
 		printf("%d\n",minstep);
	 }
 	return 0;
 }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值