Patrol Robot UVA - 1600(可以看思路)

A robot has to patrol around a rectangular area which is in a form of m × 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 i-th 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

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

本题没A,但思路差不多的,应该就某处的一点错误要找出来,找了蛮久,没找出来,等我慢慢找吧,有谁找出来了,记得告诉我!

我的思路,找最短路,但是多加一个判断就是走到Map[i][j] = 1时就判断当前的k是否大于0;不是就不入队列,其余的思路就是迷宫找最短路,然后就不知道哪里出问题了,最近一直在做这类题目,等回头再看吧!

#include <bits/stdc++.h>
using namespace std;
int n,m,k,vis[25][25],Map[25][25];
int dir[] = {0,0,1,-1};
int dic[] = {1,-1,0,0};
struct node
{
	int r,c,step,k;
};
node item;
queue<node> q;
int bfs()
{
	node temp,next;
	while(!q.empty())
	{
		temp = q.front();
		q.pop();
		if(temp.r == n&&temp.c == m)
			return temp.step;
		for(int i = 0; i < 4; i++)
		{
			next.r = temp.r+dir[i];
			next.c = temp.c+dic[i];
			next.step = temp.step; 
			if(next.r<1||next.r>n||next.c<1||next.c>m||(Map[next.r][next.c]==1&&temp.k == 0)||vis[next.r][next.c]!=0)//碰边界情况 
				continue;
			//能压入队列的情况 
			if(Map[next.r][next.c]==1&&temp.k>0)
				next.k = temp.k-1;
			else if(Map[next.r][next.c]==0)
					next.k = k;
			vis[next.r][next.c] = 1;
			next.step=temp.step+1;
			q.push(next);
		}
	}
	return -1;
}
void q_clear()
{
	while(!q.empty())
		q.pop();
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		q_clear();
		memset(Map,0,sizeof Map);
		memset(vis,0,sizeof vis);
		scanf("%d%d%d",&n,&m,&k);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= m; j++)
				scanf("%d",&Map[i][j]);
		item.r = 1;
		item.c = 1;
		item.step = 0;
		item.k = k;
		vis[item.r][item.c] = 1;
		q.push(item);
		printf("%d\n",bfs());
	}
	return 0;
} 
/*
3
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

*/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值