迷宫求最短路的话一般用bfs都可以解决,但是这个加了个状态,那么就增加一个维度,用来判断k的值。比较简单的三维bfs。写搜索题的话一定要注意细节。这个题花了好长的时间。因为k的原因,一开始用了k的原因,dfs好想一些,因为可以回溯,k的值搜完一个方向,然后回溯。那样就很简单。而且数据是20*20.

但是最后dfs还是T了。

AC的bfs

 #include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
const double Pi=3.14159265358979323846;
typedef long long ll;
const int MAXN=+;
int dx[]={-,,,};
int dy[]={,,,-};
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll mod=1e9+;
int vis[][][];
int M[][];
int m,n;
struct node{
int x,y,step,k;
node (int x=,int y=,int step=,int k=)
{
this->x=x;
this->y=y;
this->step=step;
this->k=k;
}
}tim,now;
int bfs(int x,int y,int k)
{
queue <node> Q;
Q.push(node(,,,k));
vis[][][k]=;
while(!Q.empty())
{
tim=Q.front();Q.pop();
if(tim.x==m&&tim.y==n)
{
return tim.step;
}
for(int i=;i<;i++)
{
now.x=dx[i]+tim.x;
now.y=dy[i]+tim.y;
if(M[now.x][now.y]==) now.k=tim.k-;
else now.k=k;
if(now.x>=&&now.x<=m&&now.y>=&&now.y<=n&&(M[now.x][now.y]==||now.k>=)&&!vis[now.x][now.y][now.k])
{
now.step=tim.step+;
Q.push(now);
//cout << now.x<<" "<< now.y<< " "<< now.step<<endl;
vis[now.x][now.y][now.k]=;
}
}
}
return -;
}
int main()
{
int t;cin>>t;
while(t--)
{
cin>>m>>n;
int k;cin>>k;
memset(vis,,sizeof(vis));
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>M[i][j];
cout <<bfs(,,k)<<endl;
}
return ;
}

TLE的dfs

 #include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
using namespace std;
const double Pi=3.14159265358979323846;
typedef long long ll;
const int MAXN=+;
int dx[]={-,,,};
int dy[]={,,,-};
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll mod=1e9+;
int m,n;
int vis[][];
int M[][];
int step[][];
int ans=INF;
int tk;
void dfs(int x,int y,int k)
{
if(x==m&&y==n)
{
if(ans>step[x][y])
ans=step[x][y];
return;
}
for(int i=;i<;i++)
{
int sx=dx[i]+x;
int sy=dy[i]+y;
if(sx>=&&sx<=m&&sy>=&&sy<=n&&!vis[sx][sy]&&(k>||M[sx][sy]==))
{
int pk=k;
if(M[sx][sy]==) pk--;
else pk=tk;
vis[sx][sy]=;
step[sx][sy]=step[x][y]+;
dfs(sx,sy,pk);
vis[sx][sy]=;
}
}
}
int main()
{
int t;cin>>t;
while(t--)
{
ans=INF;
memset(vis,,sizeof(vis));
memset(step,-,sizeof(step));
cin>>m>>n;cin>>tk;
for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
cin>>M[i][j];
step[][]=;
vis[][]=;
dfs(,,tk);
if(step[m][n]!=-) cout <<ans<<endl;
else cout <<-<<endl;
}
return ;
}

UVa——1600(巡逻机器人)的更多相关文章

  1. UVA:1600 巡逻机器人

    机器人要从一个m*n(m和n的范围都在1到20的闭区间内)的网格的左上角(1,1)走到右下角(m,n).网格中的一些格子是空地,用0表示,其它格子是障碍,用1表示.机器人每次可以往四个方向走一格,但不 ...

  2. UVA 1600 Patrol Robot(机器人穿越障碍最短路线BFS)

    UVA 1600 Patrol Robot   Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   ...

  3. BFS 巡逻机器人

    巡逻机器人 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=83498#problem/F 题目大意: 机器人在一个矩形区域巡逻, ...

  4. 巡逻机器人(BFS)

    巡逻机器人问题(F - BFS,推荐) Description   A robot has to patrol around a rectangular area which is in a form ...

  5. 数据结构——UVA 1600 机器人巡逻

    描述 A robot has to patrol around a rectangular area which is in a form of mxn grid (m rows and n colu ...

  6. UVA 1600 Patrol Robert 巡逻机器人 (启发搜索BFS)

    非常适合A*的一道题. 比普通的迷宫问题加一个信息k表示当前穿过的障碍物的数量. #include<cstdio> #include<cstring> #include< ...

  7. UVA - 1600 Patrol Robot (巡逻机器人)(bfs)

    题意:从(1,1)走到(m,n),最多能连续穿越k个障碍,求最短路. 分析:obstacle队列记录当前点所穿越的障碍数,如果小于k可继续穿越障碍,否则不能,bfs即可. #pragma commen ...

  8. UVa 1600 Patrol Robot (习题 6-5)

    传送门: https://uva.onlinejudge.org/external/16/1600.pdf 多状态广搜 网上题解: 给vis数组再加一维状态,表示当前还剩下的能够穿越的墙的次数,每次碰 ...

  9. UVa 1600 Patrol Robot(三维广搜)

    A robot has to patrol around a rectangular area which is in a form of m x n grid (m rows and ncolumn ...

随机推荐

  1. Java 自增原理

    很多人都知道 i++ 和 ++i 的区别 a = i++: a = i; i = i+1; a = ++ i; i = i + 1; a = i; 但碰到 i = i ++;的时候很多人就懵了? i是 ...

  2. 8ci

  3. rabbitmq 日志存储路径

    Linux      下/var /log/rabbitmq/ windows下C:\Users\Administrator\AppData\Roaming\RabbitMQ\log

  4. bash配色

    Table of Contents PS1格式 基本格式 其它可能的格式 PS1配色方案 配置文件 bash的命令提示符和终端外观由环境变量PS1定义 PS1格式 基本格式 \u 显示当前用户名 \h ...

  5. javascript20150124

    表达式与运算符 表达式 与数学中的定义相似,表达式是指具有一定的值的.用运算符把常数和变量连接起来的代数式.一个表达式可以只包含一个常数或一个变量.运算符可以是四则运算符.关系运算符.位运算符.逻辑运 ...

  6. CSS1 !important

    CSS1 !important 提升指定样式规则的应用优先权 ie6并不支持.还是会被后面的样式覆盖

  7. Thread线程join方法自我理解

    Thread线程join方法自我理解 thread.join():等待thread线程运行终止,指的是main-thread(main线程)必须等待thread线程运行结束,才能继续thread.jo ...

  8. Java 平时作业七

    以下是几本计算机书籍的基本信息 编号  书名         价格      出版社 1  JAVA 基础   32   清华大学出版社 2  JAVA WEB 开发  40   电子工业出版社 3  ...

  9. Java语法基础学习DayTwentyOne(网络编程)

    一.IP地址和端口号 1.作用 通过IP地址,唯一的定位互联网上一台主机. 端口号标识正在计算机上运行的进程,不同进程有不同的端口号,被规定为一个16位的整数0~65535,其中0~1023被预先定义 ...

  10. js /Date(1550273700000)/ 格式转换

    self.FormatJsonDate = function (jsonStr) { var tmp = ""; if (jsonStr == null || jsonStr == ...