迷宫求最短路的话一般用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. DomeOS部署

    http://gitbook.domeos.org/ 按照官方一键部署脚本完之后,发现监听的IP地址是在IPv6上面. vim /etc/default/grubadd ipv6.disable=1 ...

  2. Angular2+ 编译后部署到服务器上页面刷新404问题

    原因:NG2+ 会默认不显示URL后面的文件名 解决方案:使用LocationStrategy方式,然后把URL后的# 替换成index.html# app.module.ts import {Has ...

  3. JQuery案例二:实现全选、全不选和反选

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. web前端框架之Vue hello world

    [博客园cnblogs笔者m-yb原创,转载请加本文博客链接,笔者github: https://github.com/mayangbo666,公众号aandb7,QQ群927113708] http ...

  5. Ubuntu下Caffe实现物体分类

    参考链接: ubuntu下配置Caffe:https://blog.csdn.net/a_z666666/article/details/72853346 https://www.cnblogs.co ...

  6. k-means缺陷

    k均值算法非常简单且使用广泛,但是存在的缺陷有: 1. K值需要预先给定: 属于预先知识,很多情况下K值的估计非常困难. 2. K-Means算法对初始选取的聚类中心点是敏感的: 不同的随机种子点得到 ...

  7. C++的qsort函数

    void qsort(void * base,int nelem,int width,int (*fcmp)(const void*,const void *) 1.待排序数组首地址 2.数组中待排序 ...

  8. GitHub网站使用的基础入门

    github基本入门,首先需要掌握一些ssh非对称加密的知识和一些基本的git命令操作.下面是我推荐的两个比较好的网站,然后我再专门对GitHub网站使用进行步骤讲解. git 命令:http://b ...

  9. TP5框架 nginx服务器 配置域名 隐藏index.php

    server { listen ; #server_name localhost; server_name hhy.com;/**这里写自己的域名*/ #charset koi8-r; #access ...

  10. bind啊你返回的函数到底是个虾米

    一般apply().call()和bind()会一起比较. 他们三个都是改变this对象指向的方法. apply()和cal()方法是会立即执行,apply把参数作为数组,call方法接收一个一个的参 ...