codeforce exercise_r2

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.
大意是一个巡逻机器人,需要从(1,1)处走到(m,n)处,每次只能沿上,下,左,右4个方向走1格,且规定不能连续跨过k个障碍物,求出机器人的最短路径长度。

题目简述
输入/输出格式

输入格式:
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.
输出格式:
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 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
输出样例:
7
10
-1

问题分析

解题思路

这个题相较于一般的BFS而言做了一些拓展,就是可以跨越障碍物。这样,使用普通的二维数组存最短路径跑BFS的话是很难做出来的,因为存储的信息量不够。因此,在本题中需要使用三维数组vis[i][j][k],代表机器人走到(i,j)处还能连续跨越k次障碍时最短路径的长度。这样,由于终点一定是没有障碍的,因此,最后的结果一定在vis[m][n][k]中。

参考代码
#include <iostream>
#include <queue>
#include <cstring>
#include <algorithm>

using namespace std;

int vis[25][25][30];
int graph[25][25];
int T,m,n,k;
int dx[4]={0,0,-1,1};
int dy[4]={-1,1,0,0};

struct status
{
	int x,y;
	int k;
	status(int a,int b,int c)
	{
		x=a;
		y=b;
		k=c;
	}
};
queue<status> q;

void init()
{
	memset(vis,-1,sizeof(vis));
	memset(graph,-1,sizeof(graph));
	while(!q.empty()) q.pop();
} 

int bfs()
{
	vis[1][1][k]=0;
	status ss(1,1,k);
	q.push(ss);
	while(!q.empty())
	{
		status np=q.front();
		q.pop();
		int xx=np.x;
		int yy=np.y;
		int kk=np.k;
		for(int i=0;i<4;i++)
		{
			if(xx+dx[i]>=1&&xx+dx[i]<=m&&yy+dy[i]>=1&&yy+dy[i]<=n)
			{
				if(kk>0&&graph[xx+dx[i]][yy+dy[i]]==1&&vis[xx+dx[i]][yy+dy[i]][kk-1]==-1)
				{
					vis[xx+dx[i]][yy+dy[i]][kk-1]=vis[xx][yy][kk]+1;
					status t(xx+dx[i],yy+dy[i],kk-1);
					q.push(t);
				}
				else if(graph[xx+dx[i]][yy+dy[i]]==0&&vis[xx+dx[i]][yy+dy[i]][k]==-1)
				{
					vis[xx+dx[i]][yy+dy[i]][k]=vis[xx][yy][kk]+1;
					status t(xx+dx[i],yy+dy[i],k);
					q.push(t);
				}
			}
		}
	}
	return vis[m][n][k];
}

int main()
{
	scanf("%d",&T);
	for(int i=1;i<=T;i++)
	{
		init();
		scanf("%d %d",&m,&n);
		scanf("%d",&k);
		for(int j=1;j<=m;j++)
		{
			for(int k=1;k<=n;k++)
			{
				scanf("%d",&graph[j][k]);
			}
		}
		printf("%d\n",bfs());
	}
	return 0;
}

心得体会

BFS练习题,总体来说还是常规的BFS,但是也给我提了一个醒,跑BFS的时候不一定是在二维数组上跑的。这种解题的方法还是需要掌握的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值