您的位置:首页 > 其它

UVa 1600 Patrol Robot

2016-05-04 20:54 274 查看
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

m,
n

20). The second line contains an integer number
k (0

k

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有一个特点就是每次在枚举每一种可能的时候都采用一套临时变量来储存,并采用这套临时变量当做参数去进行下一次的递归,这样不容易出现忘记更改状态的情况。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: