UVa 1600 Patrol Robot

本文探讨了在一个含障碍物的网格中,机器人如何寻找从起点到终点的最短路径,特别关注在有限次穿越障碍的情况下,利用深度优先搜索算法解决问题。

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

A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and n columns). The rows are labeled from 1 to m. The columns are labeled from 1 to n. A cell (i, j) denotes the cell in row i and column j in the grid. At each step, the robot can only move from one cell to an adjacent cell, i.e. from (x, y) to (x + 1, y), (x, y + 1), (x - 1, y) or (x, y - 1). Some of the cells in the grid contain obstacles. In order to move to a cell containing obstacle, the robot has to switch to turbo mode. Therefore, the robot cannot move continuously to more than k cells containing obstacles.

Your task is to write a program to find the shortest path (with the minimum number of cells) from cell (1, 1) to cell (m, n). It is assumed that both these cells do not contain obstacles.

Input

The input consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 20. The following lines describe the data sets.

For each data set, the first line contains two positive integer numbers m and n separated by space (1$ \le$m, n$ \le$20). The second line contains an integer number k (0$ \le$k$ \le$20). The ith line of the next m lines contains n integer aij separated by space (i = 1, 2,..., m;j = 1, 2,..., n). The value of aij is 1 if there is an obstacle on the cell (i, j), and is 0 otherwise.

Output

For each data set, if there exists a way for the robot to reach the cell (m, n), write in one line the integer number s, which is the number of moves the robot has to make; -1 otherwise.

Sample Input

Sample Input

3 
2 5 
0 
0 1 0 0 0 
0 0 0 1 0 
4 6 
1 
0 1 1 0 0 0
0 0 1 0 1 1
0 1 1 1 1 0
0 1 1 1 0 0
2 2 
0 
0 1 
1 0

Sample Output

7 
10 
-1

一.分析

本问题是在一个权值平均的图上搜索最短路径,应该用bfs来做比较快速,但是一开始没有想到的一个变量来储存还可以穿过几个墙的方法用bfs没有办法解决,所以使用的dfs来解毫无疑问的又超时了。。。。。。。。

二.代码

#include <iostream>
#include <cstring>
using namespace std;
int m,n,k,map[23][23],flag=1,minnum=9999999;
int dx[]={-1,1,0,0};
int dy[]={0,0,-1,1};
void print ()
{
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			cout<<map[i][j]<<" ";
		}
		cout<<endl;
	}
	cout<<endl;
}
int avil(int x,int y,int tk)
{
	if(map[x][y]==-1||x>m||x<=0||y>n||y<=0||(tk==0&&map[x][y]==1)) return 0;
	else return 1;
}
void dfs(int tk,int x,int y,int counter)
{
	int tempk,tempx,tempy,tempc,tempm;
	//tempm=map[x][y];
	for(int i=0;i<=3;i++)
	{
		tempx=x+dx[i],tempy=y+dy[i];
		if(avil(tempx,tempy,tk)==1)
		{
			tempm=map[tempx][tempy];
			tempc=counter+1;
			if(map[tempx][tempy]==1) tempk=tk-1;
			else tempk=k;
			map[tempx][tempy]=-1;
			if(tempx==m&&tempy==n)
			{
				if(counter<=minnum) minnum=tempc;
//				if(tempc==8){
//				cout<<"this is"<<tempc<<endl;
//				print();}
				flag=0;
			}
			else dfs(tempk,tempx,tempy,tempc);
			map[tempx][tempy]=tempm;
		}
	}
}
int main()
{
//	freopen("input.txt","r",stdin);
	int T;
	cin>>T;
	for(int t=1;t<=T;t++)
	{
		cin>>m>>n;
		cin>>k;
		flag=1;
		minnum=999999;
		for(int i=1;i<=m;i++)
		{
			for(int j=1;j<=n;j++)
			{
				cin>>map[i][j];
			}
		}
		//print();
		map[1][1]=-1;
		dfs(k,1,1,0);
		if(flag==1) cout<<"-1"<<endl;
		else cout<<minnum<<endl;
	}
	return 0;
}

三.总结

虽然超时了,但是写代码的过程中出现的许多问题还是要总结一下的,争取下次不要再犯了吧

1.dfs递归结构进行方式有点模糊,在使用dfs的时候尽量避免使用返回值这种方式来返回解得答案,这样递归结构容易出现复杂的bug。

2.上面这段代码的dfs有一个特点就是每次在枚举每一种可能的时候都采用一套临时变量来储存,并采用这套临时变量当做参数去进行下一次的递归,这样不容易出现忘记更改状态的情况。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值