博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA 1600
阅读量:4314 次
发布时间:2019-06-06

本文共 2793 字,大约阅读时间需要 9 分钟。

Description

A robot has to patrol around a rectangular area which is in a form of mxn 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.

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.

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.

3 2 5 0 0 1 0 0 0 0 0 0 1 0 4 6 1 0 1 1 0 0 00 0 1 0 1 10 1 1 1 1 00 1 1 1 0 02 2 0 0 1 1 0

7 10 -1
题意:
在一个m*n的矩形格子中,要求求出从点(0,0)到点(m,n)的最短步伐。可是途中会设有障碍。所以要用一个三维数字标记起来。
思路:
 

用一个vis[x][y][z]表示走到x,y的时候 穿过了z个墙,如今的步数是什么

进行递归的条件是,走到下一步时候,之前走到这里的步数必须下与之后走到这里的步数。

代码:

#include 
#include
#include
#include
#include
#include
#define maxn 20+5using namespace std;int map[maxn][maxn],vis[maxn][maxn][maxn];int m,n,k,ans;int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};struct Node{ int x,y; int cnt; int k;};int init(){ memset(map,0,sizeof(map)); memset(vis,0,sizeof(vis)); cin>>n>>m>>k; for (int i=0;i
>map[i][j];}int bfs(){ queue
q; Node u; u.x=0;u.y=0;u.cnt=0;u.k=k; vis[0][0][k]=1; q.push(u); while (!q.empty()){ u=q.front();q.pop(); if (u.x==n-1&&u.y==m-1){ ans=u.cnt; return 0; } Node v; for (int i=0;i<4;i++){ v.x=u.x+dx[i]; v.y=u.y+dy[i]; v.cnt=u.cnt+1; if (map[v.x][v.y]) v.k=u.k-1; else v.k=k;//碰到0就恢复满命 if (v.x>=0&&v.x
=0&&v.y
=0) {q.push(v);vis[v.x][v.y][v.k]=1;} } } } if (q.empty()) ans=-1;}int main(){ int T; cin>>T; while (T--){ init(); bfs(); cout<
<

 

转载于:https://www.cnblogs.com/mengfanrong/p/5335823.html

你可能感兴趣的文章
Python内置函数(19)——eval
查看>>
怎样录制屏幕并将结果保存为Gif
查看>>
别名设置 alias
查看>>
练习3.34
查看>>
oracle加减操作
查看>>
dp乱写3:环形区间dp(数字游戏)
查看>>
【Beta阶段】启程会议——第零次Scrum Meeting!
查看>>
Apple Tree
查看>>
JS 中对变量类型的五种判断方法
查看>>
学习进度十五
查看>>
解决Android Studio启动项目后一直处于refreshing 'View' gradle project,快速解决亲测有效...
查看>>
4.12 | 学习笔记
查看>>
python开发【第一篇】:基础知识
查看>>
javascript的window.onload()方法和jQuery的$(document).ready()的对比
查看>>
mysql数据库维护(备份和还原)和性能提高
查看>>
第八章 springboot + mybatis + 多数据源
查看>>
Arab and North African Region,2002(Snakes & ladders)
查看>>
React中的Refs
查看>>
自己使用MySQL中的GROUP_CONCAT(CONCAT_WS())函数查询的数据显示不全的问题. 以及在后台开发中怎么设置使用....
查看>>
Mysql强制修改密码
查看>>