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;
}

最新文章

  1. 将表里的数据批量生成INSERT语句的存储过程 增强版
  2. 初学c# -- 学习笔记(五) winfrom自定义滚动条
  3. 如何用UE(UltraEdit)删除重复行?--转
  4. AngularJS学习笔记之directive&mdash;scope选项与绑定策略
  5. grunt压缩合并代码
  6. HDU4524+水题
  7. 可拖拽重排的CollectionView
  8. JavaScript--声明提前
  9. JS 实现 Tab标签切换功能
  10. PCL学习笔记(一)
  11. shell一些笔记
  12. POJ3614 Sunscreen 优先队列+贪心
  13. docker原理(转)
  14. laravel 控制器使用MODEL
  15. NOIP 2018 day1 题解
  16. make ;makefile; cmake; qmake的区分
  17. Python2.7-fnmacth
  18. 基础邮件原理(MUA,MTA,MDA)
  19. react native 示例代码
  20. 页面调用dll

热门文章

  1. Linux自带-系统级性能分析工具 — Perf(转)
  2. 4G牌照影响
  3. git处理时的问题
  4. RAM建模和初始化
  5. &quot;xxadmin&quot; user: No protocol specified 错误
  6. 如何找到SAP Cloud for Customer标准培训和认证方面的信息
  7. UVA - 11082 Matrix Decompressing (最大流,技巧)
  8. spark 之主成分分析
  9. Eclipse调试:Run on server 和 Debug on server 区别
  10. 基于PassThru的NDIS中间层驱动程序扩展