迷宫求最短路的话一般用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. 搜索引擎中index、attribute和summary概念

    index:倒排索引 attribute: 正排索引 summary:数据集合,用于数据结果展示.

  2. PROJ.4学习——初识PROJ

    PROJ.4介绍——初始认识 前言 PROJ是一个通用的坐标转换软件,它将地理空间坐标从一个坐标系转换为另一个坐标系.这包括地图投影和大地坐标变换. PROJ包含命令行应用程序,可以方便地从文本文件或 ...

  3. IntelliJ IDEA 安装和破解教程

    1.首先下载IntelliJ IDEA,下载链接:http://www.jetbrains.com/idea/download/#section=windows:记得一定要选择UItimate版! 2 ...

  4. 【转载】如何查看Mysql是否已经安装

    原文地址: https://jingyan.baidu.com/article/fd8044fa2ecaf35030137a42.html MySQL是关系型数据库管理系统,是目前最流行的关系型数据库 ...

  5. 【转载】安装 gephi 软件

    作者:小小爽链接:https://www.zhihu.com/question/21268129/answer/354924066来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  6. 关于STL的map的注意事项

    关于map是什么,这里就不多叙述了. 直接正题,常用的map插入操作有三种方法:通过pair<key_type,value_type>.通过value_type插入数据.还有一种类似于数组 ...

  7. .NET界面控件DevExpress发布v18.2.8|附下载

    DevExpress Universal Subscription(又名DevExpress宇宙版或DXperience Universal Suite)是全球使用广泛的.NET用户界面控件套包,De ...

  8. CentOS7.4+OpenStack-Queens版本部署

    一.准备工作.网络选择NAT 创建两台虚拟机:linux-node1.linux-node2 node1: 修改主机名 [root@localhost ~]# hostnamectl set-host ...

  9. 使用PuTTY软件远程登录root被拒:access denied

    PuTTY是一个Telnet.SSH.rlogin.纯TCP以及串行接口连接软件. 使用PuTTY软件远程登录root时,提示:ACCESS DENIED,很有可能是由sshd的默认配置造成的. 可以 ...

  10. 预热ASP.NET MVC 的View

    ASP.NET MVC 的View 预设是Load on Demand(按需加载),也就是说View 第一次要Render 的时候才会去载入跟编译,这个就会造成一个现象,即使Web 应用程式已经完成启 ...