UVa——1600Patrol Robot(A*或普通BFS)
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 (i, j) 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 (x, y) to (x+ 1, y), (x, y + 1), (x - 1, y) or (x, y - 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 (m, n). 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 (1m, n20). 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 (m, n), 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;
}
UVa——1600Patrol Robot(A*或普通BFS)的更多相关文章
- UVa 439骑士的移动(BFS)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 11624 Fire!(两次BFS+记录最小着火时间)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- F - Robot Motion 栈加BFS
A robot has been programmed to follow the instructions in its path. Instructions for the next direct ...
- [Uva 10085] The most distant state (BFS)
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- CodeForces 589J Cleaner Robot (DFS,或BFS)
题意:给定n*m的矩阵,一个机器人从一个位置,开始走,如果碰到*或者边界,就顺时针旋转,接着走,问你最后机器人最多能走过多少格子. 析:这个题主要是题意读的不大好,WA了好几次,首先是在*或者边界才能 ...
- HDU 4166 & BNU 32715 Robot Navigation (记忆化bfs)
题意:给一个二维地图,每个点为障碍或者空地,有一个机器人有三种操作:1.向前走:2.左转90度:3.右转90度.现给定起点和终点,问到达终点最短路的条数. 思路:一般的题目只是求最短路的长度,但本题还 ...
- UVa 816 Abbott的复仇(BFS)
寒假的第一道题目,在放假回家颓废了两天后,今天终于开始刷题了.希望以后每天也能多刷几道题. 题意:这道BFS题还是有点复杂的,给一个最多9*9的迷宫,但是每个点都有不同的方向,每次进入该点的方向不同, ...
- [ACM_模拟] UVA 12503 Robot Instructions [指令控制坐标轴上机器人移动 水]
Robot Instructions You have a robot standing on the origin of x axis. The robot will be given som ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...
随机推荐
- 自定义可伸缩的imageView
直接上代码 /** * 自定义可伸缩的ImageView */ public class ZoomImageView extends ImageView { /** 画笔类 **/ private P ...
- python-mysql软件下载地址
http://sourceforge.net/projects/mysql-python/?source=dlp
- php类重载
首先,因为PHP是弱类型语言,是不直接支持重载的,不过可以通过一些方法来实现重载. 先说一下重写 在子类继承父类方法后,自己重新定义父类的方法,包含函数名,形参(个数,顺序)完全相同,但权限修饰符可不 ...
- vue+element ui项目总结点(五)Carousel 走马灯组件、Collapse 折叠面板、Tree 树形控件
<template> <div class="ele_test_box"> <!-- 常用效果 Popover 弹出框组件 具体属性查看官方文档--& ...
- LeetCode 3Sum Closest 最近似的3sum(2sum方法)
题意:找到最接近target的3个元素之和,并返回该和. 思路:用2个指针,时间复杂度O(n^2). int threeSumClosest(vector<int>& nums, ...
- 洛谷 First Step (ファーストステップ) 3月月赛T1
题目背景 知らないことばかりなにもかもが(どうしたらいいの?) 一切的一切 尽是充满了未知数(该如何是好) それでも期待で足が軽いよ(ジャンプだ!) 但我仍因满怀期待而步伐轻盈(起跳吧!) 温度差なん ...
- asp.net mvc集成log4net
第一步:在web项目的引用中添加log4net.dll,可以通过Nuget直接下载并安装: 第二步:在web项目的web.config配置文件的configuration节点内添加log4net节点, ...
- UVA 12563 Jin Ge jin Qu [h] ao 劲歌金曲 (01背包)
每首只能唱一次,而且中间不能不唱歌,所以先把状态赋值为-1,以区别合法状态和非法状态,在唱歌曲目最多的条件下,离开时间应该尽量晚. 状态定义f[i][j]考虑前i首歌唱歌时间为j的最大唱歌曲目 #in ...
- Robot Framework(十) 执行测试用例——测试执行
3.2测试执行 本节描述如何执行从解析的测试数据创建的测试套件结构,如何在失败后继续执行测试用例,以及如何正常停止整个测试执行. 3.2.1执行流程 执行套房和测试 设置和拆卸 执行顺序 3.2.2继 ...
- CPP-基础:String类
已知类String的原型为: class String { public: String(const char *str = NULL); // 普通构造函数 String(const String ...