ZOJ 3865 Superbot(优先队列--模板)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477
主要思路:1.从一个点(cur)到它相邻的点(next),所需要的时间数(t)其实是固定的,而且这个移动过程后,到达next时,相应的方向也是固定的,找到求t的办法就好了。
2.到达一个未到达的点可能有多条路,优先队列取时间最短的路,则答案最优
题目:
Superbot
Superbot is an interesting game which you need to control the robot on an N*M grid map.

As you see, it's just a simple game: there is a control panel with four direction left (1st position), right (2nd), up (3rd) and down (4th). For each second, you can do exact one of the following operations:
- Move the cursor to left or right for one position. If the cursor is on the 1st position and moves to left, it will move to 4th position; vice versa.
- Press the button. It will make the robot move in the specific direction.
- Drink a cup of hot coffee and relax. (Do nothing)
However, it's too easy to play. So there is a little trick: Every P seconds the panel will rotate its buttons right. More specifically, the 1st position moves to the 2nd position; the 2nd moves to 3rd; 3rdmoves to 4th and 4th moves to 1st. The rotating starts at the beginning of the second.
Please calculate the minimum time that the robot can get the diamond on the map.
At the beginning, the buttons on the panel are "left", "right", "up", "down" respectively from left to right as the picture above, and the cursor is pointing to "left".
Input
There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:
The first line contains three integers N, M (2 <= N, M <= 10) and P (1 <= P <= 50), which represent the height of the map, the width of the map and the period that the panel changes, respectively.
The following lines of input contains N lines with M chars for each line. In the map, "." means the empty cell, "*" means the trap which the robot cannot get in, "@" means the initial position of the robot and "$" means the diamond. There is exact one robot and one diamond on the map.
Output
For each test case, output minimum time that the robot can get the diamond. Output "YouBadbad" (without quotes) if it's impossible to get the diamond.
Sample Input
4
3 4 50
@...
***.
$...
5 5 2
.....
..@..
.*...
$.*..
.....
2 3 1
*.@
$.*
5 5 2
*****
..@..
*****
$....
.....
Sample Output
12
4
4
YouBadbad
Hint
For the first example:
0s: start
1s: cursor move right (cursor is at "right")
2s: press button (robot move right)
3s: press button (robot move right)
4s: press button (robot move right)
5s: cursor move right (cursor is at "up")
6s: cursor move right (cursor is at "down")
7s: press button (robot move down)
8s: press button (robot move down)
9s: cursor move right (cursor is at "left")
10s: press button (robot move left)
11s: press button (robot move left)
12s: press button (robot move left)
For the second example:
0s: start
1s: press button (robot move left)
2s: press button (robot move left)
--- panel rotated ---
3s: press button (robot move down, without changing cursor)
4s: press button (robot move down)
For the third example:
0s: start
1s: press button (robot move left)
--- panel rotated ---
2s: press button (robot move down)
--- panel rotated ---
3s: cursor move left (cursor is at "right")
--- panel rotated ---
4s: press button (robot move left)
Author: DAI, Longao
Source: The 15th Zhejiang University Programming Contest
代码:
#include "cstdio"
#include "cstring"
#include "queue"
using namespace std; #define N 15
#define INF 0x3333333
char map[N][N];
int n,m,p;
int mark[N][N][]; //到达每个点4个方向上的最短时间
int dir[][] = {{,-},/*0:left*/{,},/*1:right*/{-,}/*2:up*/,{,}/*3:down*/}; typedef struct node
{
int x,y; //coordinate
int t; //time
int di; //direction 0:left 1:right 2:up 3:down
bool operator < (const node &b) const{
return t > b.t;
}
/*
friend bool operator<(node a,node b)
{
return a.di > b.di;
}
*/
} Point; Point st,en; void Init() //标记数组初始化
{
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
for(int k=; k<; k++)
mark[i][j][k] = INF;
}
}
} void Init_st_en()
{
for(int i=; i<=n; i++)
{
for(int j=; j<=m; j++)
{
if(map[i][j]=='@')
st.x=i,st.y=j,st.t=,st.di=;
if(map[i][j]=='$')
en.x=i,en.y=j;
}
}
} int DFS(); int main()
{
int i,T;
int ans;
scanf("%d",&T);
while(T--)
{
scanf("%d %d %d",&n,&m,&p);
for(i=; i<=n; i++)
scanf("%s",map[i]+);
Init_st_en();
Init();
ans = DFS();
if(ans==-)
printf("YouBadbad\n");
else
printf("%d\n",ans);
}
return ;
} int DFS()
{
int i;
int x,y,di,t;
priority_queue<Point> q;
Point cur,next;
q.push(st);
map[st.x][st.y] = '*';
while(!q.empty())
{
cur = q.top(); q.pop();
if(cur.x==en.x && cur.y==en.y)
return cur.t;
for(i=; i<; i++)
{
next.x = x = cur.x+dir[i][];
next.y = y = cur.y+dir[i][];
next.di= i;
if(x< || x>n || y< || y>m || map[x][y]=='*') continue;
di = cur.di; //若从点cur到点next,到next的时候,方向一定是i
t = cur.t;
if(t!= && t%p==)
di = (di+-)%;
if(di==i) //可以1s从点cur到点next的条件
t++;
else if( ((t+)%p!= && ((di+)%==i || (di+-)%==i)) //可以2s从点cur到点next的条件
|| ((t+)%p== && ((di+-)%==i || (di+-)%==i)))
t+=;
else //最多3s可以从点cur到点next
t+=;
next.t = t;
if(t<mark[x][y][i])
{
q.push(next);
mark[x][y][i] = t;
}
}
}
return -;
}
ZOJ 3865 Superbot(优先队列--模板)的更多相关文章
- BFS+模拟 ZOJ 3865 Superbot
题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...
- zoj.3865.Superbot(bfs + 多维dp)
Superbot Time Limit: 2 Seconds Memory Limit: 65536 KB Superbot is an interesting game which you ...
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...
- ZOJ - 3865 Superbot 【BFS】
题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865 思路 一个迷宫题 但是每次的操作数和普通的迷宫题不一样 0 ...
- Zoj 3865 Superbot
按规则移动机器人 , 问是否能拾得宝藏 . 加了一个控制板 , 还增加了一个控制板移动周期 p 将移动周期变换一下 , 移动一次 就相当于光标向左不耗费时间的移动了一格 搜索思路 : 搜索当前格子到 ...
- POJ-数据结构-优先队列模板
优先队列模板 优先队列是用堆实现的,所以优先队列中的push().pop()操作的时间复杂度都是O(nlogn). 优先队列的初始化需要三个参数,元素类型.容器类型.比较算子. 需要熟悉的优先队列操作 ...
- ZOJ Problem Set - 3865 Superbot (bfs)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 大牛博客:http://www.cnblogs.com/kylehz/p ...
- HDU 1242 rescue (优先队列模板题)
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra
传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...
随机推荐
- 微信公众平台入门开发教程.Net(C#)框架
一.序言 一直在想第一次写博客,应该写点什么好?正好最近在研究微信公众平台开发,索性就记录下,分享下自己的心得,也分享下本人简单模仿asp.net运行机制所写的通用的微信公众平台开发.Net(c#)框 ...
- 2016C#模拟谷歌Google登陆Gmail&Youtube小案例
之所以写这个,是因为本来想写一个Youtube刷评论的工具,把登录做出来了,后面就没继续做下去. 涉及到基本的HttpWatch的应用以及Fiddler的应用(Fd主要用来排查问题,通过对比 浏览器和 ...
- 【原创】网站抓包HttpWebRequest不返回Javascript生成的Cookie的解决办法
前言: 最近在做中国移动爬虫的过程中,首先遇到的就是 在某个请求中,有一个名为“WT_PFC"的cookie键值是由前端JavaScript生成的,没有进入到HttpWebResponse中 ...
- Microsecond and Millisecond C# Timer[转]
文章转至:http://www.codeproject.com/Articles/98346/Microsecond-and-Millisecond-NET-Timer IntroductionAny ...
- sql联合查询去除重复计算总和
1.首先来个联合查询 SELECT 字段1, 字段2, 字段3, 字段4 FROM 表1 INNER JOIN 表2 ON 表1.字段x = 表2.字段x x:代表随意的一个,只要在联合查询的两张表都 ...
- jquery ajax 用 data 和 headers 向 java RESTful 传递参数区别
jquery 的 ajax 是非常方便的一个函数,记录一下 $.ajax 生成的 http 报文 一.使用 data 传递参数: $.ajax({ url : "webrs/test/add ...
- 回文串---Palindrome
POJ 3974 Description Andy the smart computer science student was attending an algorithms class whe ...
- (旧)子数涵数·DW——网页制作的流程
PS:这是我很早以前的一个废掉的项目. 当时用的还是table排版,现在基本都是div了吧. 这个项目前段时间,我还抢救过一次,后来还是放弃了. 先行.网页制作的流程分为哪些呢? 一.网站策划(当时, ...
- PHP学习笔记:APACHE配置虚拟目录、一个站点使用多域名配置方式
我用的是xmapp lite2016的集成包,配置虚拟目录教程如下: 找到httpd-vhosts.conf这个文件,这个文件一般是在xampp\apache\conf\extra这个路径下面,找不到 ...
- Eclipse环境下使用Maven注意事项
在最新版本的Eclipse Java EE IDE for Web Developers中已经包含Maven 2 在File,New中可以看到Maven Project,新建, 按照步骤一路下来,要求 ...