A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumns). 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(1≤mn≤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 ofaij is 1 if there is an obstacle on the cell (ij), 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

Simple Output

7
10
-1

题意

机器人从起点(0,0)走到(m,n),图中有障碍,机器人最多连续翻越k个障碍,求走到(m,n)的最短路径长度

题解1

一开始在二维上想怎么连续,而且已经访问过的点有可能再访问(本来的连续大),最后硬着头皮想了个蠢方法(if去判断)

代码1

 #include<bits/stdc++.h>
using namespace std;
int Map[][],Cnt[][];//Map的值为连续越过的数量,Cnt的值为步数
int dx[]={,,,-};
int dy[]={,-,,};
int n,m,k;
struct Node
{
int x,y;
Node(int x=,int y=):x(x),y(y){}
};
int bfs()
{
queue<Node> qu;
Cnt[][]=;
qu.push(Node(,));
Node h,t;
while(!qu.empty())
{
h=qu.front();
qu.pop();
if(h.x==n&&h.y==m)return Cnt[n][m];
for(int i=;i<;i++)
{
t.x=h.x+dx[i];
t.y=h.y+dy[i];
if(t.x>=&&t.x<=n&&t.y>=&&t.y<=m)
{
//h表示当前,t表示下一个
if(Map[t.x][t.y]!=&&Map[h.x][h.y]!=)//当前和下一个!=0表示连续
if(Cnt[t.x][t.y]==-)//未访问过
Map[t.x][t.y]=Map[h.x][h.y]+;//下一个连续=当前连续+1
else if(Map[t.x][t.y]>Map[h.x][h.y]+)//若已访问过,如果下一个已经有的连续>当前+1
{
Map[t.x][t.y]=Map[h.x][h.y]+;//保证当前连续为最小连续,为了下一步走得更舒服
Cnt[t.x][t.y]=-;//标记未访问
}
if(Map[t.x][t.y]!=&&Map[h.x][h.y]==)//表示这是第一个连续
if(Cnt[t.x][t.y]==-)//未访问过
Map[t.x][t.y]=;//这个点为初始连续1
else if(Map[t.x][t.y]>)//若已访问过,如果下一个连续不是初始连续1,设为初始连续1
{
Map[t.x][t.y]=;//保证当前连续为最小连续,为了下一步走得更舒服
Cnt[t.x][t.y]=-;//标记未访问
}
if(Map[t.x][t.y]<=k&&Cnt[t.x][t.y]==-)//连续<=k,未访问
{
Cnt[t.x][t.y]=Cnt[h.x][h.y]+;
qu.push(t);
}
//上面处理的很蠢
}
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
memset(Cnt,-,sizeof(Cnt));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&Map[i][j]);
printf("%d\n",bfs());
}
return ;
}

题解2

后来发现可以用三维广搜去写,增加的第三维表示到这个点[X,Y]步数为STEP的情况

代码2

 #include<bits/stdc++.h>
using namespace std;
int Map[][],Cnt[][],Vis[][][];//Vis[x][y][step]
int dx[]={,,,-};
int dy[]={,-,,};
int n,m,k;
struct Node
{
int x,y,step;//step为连续数
Node(int x=,int y=,int step=):x(x),y(y),step(step){}
};
int bfs()
{
queue<Node> qu;
Cnt[][]=;
Vis[][][]=;
qu.push(Node(,,));
Node h,t;
while(!qu.empty())
{
h=qu.front();qu.pop();
if(h.x==n&&h.y==m)return Cnt[n][m];
for(int i=;i<;i++)
{
t.x=h.x+dx[i];
t.y=h.y+dy[i];
t.step=h.step;
if(t.x>=&&t.x<=n&&t.y>=&&t.y<=m)
{
if(Map[t.x][t.y]==)t.step=h.step+;//墙,连续+1
else t.step=;//不是墙,连续=0
if(t.step<=k&&Vis[t.x][t.y][t.step]==)//连续<=k并且未访问
{
Vis[t.x][t.y][t.step]=;//标记访问
if(Cnt[t.x][t.y]==-)//未走到过(第一次走到的肯定是最小路径)
Cnt[t.x][t.y]=Cnt[h.x][h.y]+;
qu.push(t);
}
}
}
}
return -;
}
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;
scanf("%d",&t);
while(t--)
{
memset(Cnt,-,sizeof(Cnt));
memset(Vis,,sizeof(Vis));
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&Map[i][j]);
printf("%d\n",bfs());
}
return ;
}

最新文章

  1. 趣说游戏AI开发:曼哈顿街角的A*算法
  2. css常用的特效代码
  3. strace命令(收集整理,常看常新)
  4. BeanFactory not initialized or already closed - call &#39;refresh&#39; before accessing beans解决办法
  5. 仿SDWebImage
  6. hadoop2 作业执行过程之map过程
  7. hadoop2.2.0伪分布式搭建
  8. Java 开发@ JDBC链接SQLServer2012
  9. 15个实用的Linux find命令示例
  10. HDU 3008 Warcraft(DP)
  11. aspx生成验证码
  12. SQL SERVER2012新分页方式 轉載
  13. [HEOI2016/TJOI2016]游戏
  14. hiho一下 第168周
  15. ubuntu1604安装tensorflow
  16. PHP开发者成长图
  17. December 26th 2016 Week 53rd Monday
  18. Linux命令-文件搜索命令:which
  19. Scrapy框架Windows下安装
  20. Java基础22-Static关键字

热门文章

  1. 机器学习进阶-案例实战-图像全景拼接-书籍SIFT特征点连接 1.cv2.drawMatches(对两个图像的关键点进行连线操作)
  2. BBS-基于用户认证组建和Ajax实现登陆验证
  3. es6 初级之箭头函数
  4. Cookie安全小结
  5. 在WINDOWS上开发IOS应用的方法
  6. avalon2学习教程05属性操作
  7. 利用jQuery扩展接口为jQuery框架定义了两个自定义函数,然后调用这两个函数
  8. C++ 将数据转为字符串的几种方法
  9. java的acm输入输出格式+大数语法
  10. Applese走方格-dfs