您的位置:首页 > 其它

UVA 1600 Patrol Robot(BFS扩展)

2015-05-21 01:14 477 查看
Patrol Robot

Time Limit: 3000MSMemory Limit: Unknown64bit IO Format: %lld & %llu
Submit Status

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.

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

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


题目大意:

  这道题是说,给你一个n*m的0-1矩阵,你要做的就是在这个矩阵中,从(1,1)走到(n,m)所需要的最小步数,但是你有一个功能,就是可以连续穿过k个1。

解题思路:

  看到最少操作次数,想都不用想,直接上bfs,然后node的设计就是x,y,k,step,这个k要注意下。

  当我们扩展到下一层的时候a[tx][ty]==1的时候,newnode.kk = now.k-1;当a[tx][ty] == 0 的时候,newnode.kk = k;

  相当于这个机器人在下一步走到1的时候就减少一点血,当走到0的时候,就满血满蓝了。

  一开始在newnode.kk = now.kk - 1;和 now.kk-=1;的设计上挂掉了第二组样例,原因很简单,就是说,如果我把提前把newnode搞出来,而是一味

  的靠now.kk去更新的话,会产生错误的结果.

代码:

# include<cstdio>
# include<iostream>
# include<queue>
# include<cstring>

using namespace std;

# define MAX 23

int a[MAX][MAX];
int nxt[4][2] = {{1,0},{0,-1},{-1,0},{0,1}};
int book[MAX][MAX][MAX];

int n,m,k;

struct node
{
int x,y;
int step;
int kk;
};
queue<node>Q;

void init()
{
while (!Q.empty())
{
Q.pop();
}
memset(book,0,sizeof(book));
}

int can_move( int x ,int y,int kk )
{
if ( x>=1&&x<=n&&y>=1&&y<=m&&book[x][y][kk]==0 )
return 1;
else
return 0;
}

int bfs ( node start )
{
init();
if ( start.x==n&&start.y==m )
{
return start.step;
}

Q.push(start);
while ( !Q.empty() )
{
node now = Q.front();
Q.pop();
if ( now.kk < 0 )
continue;
for ( int i = 0;i < 4;i++ )
{
node newnode;
int tx = now.x+nxt[i][0], ty = now.y+nxt[i][1];
if ( a[tx][ty]==1 )
newnode.kk = now.kk-1;
else
newnode.kk = k;
if ( can_move(tx,ty,newnode.kk) )
{
if ( tx==n&&ty==m )
{
return now.step+1;
}
newnode.x = tx; newnode.y = ty; newnode.step = now.step+1;
if( newnode.kk >= 0 )
{
Q.push(newnode);
book[tx][ty][newnode.kk] = 1;
}
}
}
}

return -1;

}

int main(void)
{
int t;scanf("%d",&t);
while ( t-- )
{
scanf("%d%d%d",&n,&m,&k);
for ( int i = 1;i <= n;i++ )
{
for ( int j = 1;j <= m;j++ )
{
scanf("%d",&a[i][j]);
}
}
node start;
start.x = 1; start.y = 1;start.step = 0; start.kk = k;
book[start.x][start.y][start.kk] = 1;
int ans = bfs(start);
if ( ans == -1 )
printf("-1\n");
else
printf("%d\n",ans);

}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: