Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

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 (ij) 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 (xy) to (x+ 1, y), (xy + 1), (x - 1, y) or (xy - 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 (mn). 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 (1mn20). The second line contains an integer number k(0k20). 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 (mn), 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*来着,g设为当前步数,h为曼哈顿距离,但似乎第三组死循环了……看来A*应用不够熟练啊,然后改为普通bfs,只用了二维的数组记录状态WA……抱着试一试的心态加了一维表示个当前点被穿过k步走过的记录,没想到A了……

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=25;
int m,n,k;
int pos[N][N];
int vis[N][N][N];
struct info
{
int x;
int y;
int g;
int kk;
};
queue<info>Q;
info direct[4]={{0,1,1,0},{1,0,1,0},{0,-1,1,0},{-1,0,1,0}};
inline info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.g=a.g+b.g;
return c;
}
bool check(const info &a)
{
return (a.x>=0&&a.x<m&&a.y>=0&&a.y<n&&a.kk<=k);
}
void init()
{
MM(pos,0);
MM(vis,0);
while (!Q.empty())
Q.pop();
}
int main(void)
{
int tcase,i,j;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d%d",&m,&n);
scanf("%d",&k);
for (i=0; i<m; ++i)
{
for (j=0; j<n; ++j)
scanf("%d",&pos[i][j]);
}
int r=-1;
info S={0,0,0,0};
vis[S.x][S.y][S.kk]=1;
Q.push(S);
while (!Q.empty())
{
info now=Q.front();
Q.pop();
if(now.x==m-1&&now.y==n-1)
{
r=now.g;
break;
}
for (i=0; i<4; i++)
{
info v=now+direct[i];
if(!pos[v.x][v.y])
v.kk=0;
else
v.kk=now.kk+1;
if(check(v)&&!vis[v.x][v.y][v.kk])
{
Q.push(v);
vis[v.x][v.y][v.kk]=1;
}
}
}
printf("%d\n",r);
}
return 0;
}

A*代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define MM(x,y) memset(x,y,sizeof(x))
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=25;
int m,n,k;
int pos[N][N];
int vis[N][N][N];
struct info
{
int x;
int y;
int g;
int kk;
int h;
bool operator<(const info &b)const
{
if(h+g!=b.h+b.g)
return h+g>b.h+b.g;
if(g!=b.g)
return g>b.g;
return kk>b.kk;
}
};
priority_queue<info>Q;
info direct[4]={{0,1,1,0,0},{1,0,1,0,0},{0,-1,1,0,0},{-1,0,1,0,0}};
inline info operator+(const info &a,const info &b)
{
info c;
c.x=a.x+b.x;
c.y=a.y+b.y;
c.g=a.g+b.g;
return c;
}
bool check(const info &a)
{
return (a.x>=0&&a.x<m&&a.y>=0&&a.y<n&&a.kk<=k&&!vis[a.x][a.y][a.kk]);
}
void init()
{
MM(pos,0);
MM(vis,0);
while (!Q.empty())
Q.pop();
}
int main(void)
{
int tcase,i,j;
scanf("%d",&tcase);
while (tcase--)
{
init();
scanf("%d%d",&m,&n);
scanf("%d",&k);
for (i=0; i<m; ++i)
{
for (j=0; j<n; ++j)
{
scanf("%d",&pos[i][j]);
}
}
int r=-1;
info S={0,0,0,0,n+m-2};
vis[S.x][S.y][S.kk]=1;
Q.push(S);
while (!Q.empty())
{
info now=Q.top();
Q.pop();
if(now.x==m-1&&now.y==n-1)
{
r=now.g;
break;
}
for (i=0; i<4; i++)
{
info v=now+direct[i];
if(!pos[v.x][v.y])
v.kk=0;
else
v.kk=now.kk+1;
if(check(v))
{
v.h=m-1+n-1-v.x-v.y;
Q.push(v);
vis[v.x][v.y][v.kk]=1;
}
}
}
printf("%d\n",r);
}
return 0;
}

UVa——1600Patrol Robot(A*或普通BFS)的更多相关文章

  1. UVa 439骑士的移动(BFS)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  2. UVA 11624 Fire!(两次BFS+记录最小着火时间)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  3. F - Robot Motion 栈加BFS

    A robot has been programmed to follow the instructions in its path. Instructions for the next direct ...

  4. [Uva 10085] The most distant state (BFS)

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. CodeForces 589J Cleaner Robot (DFS,或BFS)

    题意:给定n*m的矩阵,一个机器人从一个位置,开始走,如果碰到*或者边界,就顺时针旋转,接着走,问你最后机器人最多能走过多少格子. 析:这个题主要是题意读的不大好,WA了好几次,首先是在*或者边界才能 ...

  6. HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)

    题意:给一个二维地图,每个点为障碍或者空地,有一个机器人有三种操作:1.向前走:2.左转90度:3.右转90度.现给定起点和终点,问到达终点最短路的条数. 思路:一般的题目只是求最短路的长度,但本题还 ...

  7. UVa 816 Abbott的复仇(BFS)

    寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...

  8. [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]

      Robot Instructions  You have a robot standing on the origin of x axis. The robot will be given som ...

  9. UVA 11624 Fire!【两点BFS】

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

随机推荐

  1. Android程序中使用iconfont心得

    1.关于iconfont iconfont既是icon又是font,具体来说应该是用font形式展现的icon.与传统图片格式的图标不同,这一种图标因为是以字体形式展现的,所以更改大小.颜色.背景颜色 ...

  2. CodeForces 149D Coloring Brackets (区间DP)

    题意: 给一个合法的括号序列,仅含()这两种.现在要为每对括号中的其中一个括号上色,有两种可选:蓝or红.要求不能有两个同颜色的括号相邻,问有多少种染色的方法? 思路: 这题的模拟成分比较多吧?两种颜 ...

  3. vijos 1190 繁忙的都市

    描述 城市C是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造.城市C的道路是这样分布的:城市中有n个交叉路口,有些交叉路口之间有道路相连,两个交叉路口之间最多有一条道路 ...

  4. P2421 A-B数对(增强版)

    题目背景 woshiren在洛谷刷题,感觉第一题:求两数的和(A+B Problem)太无聊了,于是增加了一题:A-B Problem,难倒了一群小朋友,哈哈. 题目描述 给出N 个从小到大排好序的整 ...

  5. php随机生成国内ip地址

    获得一个国家所有ip段,随机生成国内ip地址的缩水实现.注意:  $ip_long数组中后5个值在64位系统中可能是错误的(下面代码中  $ip_long 数组的后五个值在32位系统中为负数,64位系 ...

  6. bzoj 2658

    首先考虑容斥 我们计算出所有没有点在其中的矩形,然后用所有矩形减去这些矩形即可 然后考虑如何计算没有点在其中的矩形 采用扫描线的思想,从上向下一行一行扫,假设我们扫到的行编号是$a$,然后考虑如果左右 ...

  7. MIPS程序设计实例

    第一题:用系统功能调用实现简单输入输出 题目要求 利用系统功能调用从键盘输入,转换后在屏幕上显示,具体要求如下: 1.如果输入的是字母(A~Z,区分大小写)或数字(0~9),则将其转换成对应的英文单词 ...

  8. CS 分解

    将学习到什么 CS 分解是分划的酉矩阵在分划的酉等价之下的标准型. 它的证明涉及奇异值分解.QR 分解以及一个简单习题.   一个直观的习题 设 \(\Gamma, L \in M_p\). 假设 \ ...

  9. 在行列都排好序的矩阵中找数 【题目】 给定一个有N*M的整型矩阵matrix和一个整数K, matrix的每一行和每一 列都是排好序的。实现一个函数,判断K 是否在matrix中。 例如: 0 1 2 5 2 3 4 7 4 4 4 8 5 7 7 9 如果K为7,返回true;如果K为6,返 回false。 【要求】 时间复杂度为O(N+M),额外空间复杂度为O(1)。

    从对角考虑 package my_basic.class_3; /** * 从对角开始 */ public class Code_09_FindNumInSortedMatrix { public s ...

  10. 相机 感光度iso,焦距,光圈,ccd 和 噪点, 景深关系表格

    表格 矩阵 感官度iso: 越低曝光速度越慢,所谓慢工出细活,成像质量会好,如果形成的话. 但是因为慢,所以要更多的光量,才能画完. 就需要更慢的快门 (但是太慢手抖的话就糊掉,或者动的物体形成轨迹. ...