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
Sample Output
1.dfs递归结构进行方式有点模糊,在使用dfs的时候尽量避免使用返回值这种方式来返回解得答案,这样递归结构容易出现复杂的bug。
2.上面这段代码的dfs有一个特点就是每次在枚举每一种可能的时候都采用一套临时变量来储存,并采用这套临时变量当做参数去进行下一次的递归,这样不容易出现忘记更改状态的情况。
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 Input3 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有一个特点就是每次在枚举每一种可能的时候都采用一套临时变量来储存,并采用这套临时变量当做参数去进行下一次的递归,这样不容易出现忘记更改状态的情况。
相关文章推荐
- mysqld.cc启动分析及运行过程(题目取大了,不好意思)
- iOS自定义backBarButtonItem的点击事件
- C#学习笔记之string
- NIO边看边记 之 概述(一)
- libevent总结(中)
- 获得代理ip并验证
- C语言版的16进制与字符串互转函数
- C++构造函数和析构函数
- 一个完整的网站seo优化方案
- 2015安徽省赛 J.镜像树
- Centos安装php高版本
- 799
- 比较美观的ListView下拉刷新上拉加载
- reshape2揉数据
- 在移动硬盘里装多个linux系统
- Java学习笔记之输入输出流(二) 缓冲流、随机流、数组流、数据流、对象流
- 笔记6:WebClient 类 一个更简捷的类
- 16.二叉树的镜像
- 1006 ProblemF
- 粒子群优化算法