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 ...
随机推荐
- ASP.NET运行时详解 集成模式和经典模式
遗留问题 在<ASP.NET运行时详解 生命周期入口分析>中遗留两个问题,包括Application的InitInternal方法执行细节.IIS6和II7经典模式请求管道管理类Appli ...
- 三分套三分 --- HDU 3400 Line belt
Line belt Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3400 Mean: 给出两条平行的线段AB, CD,然后一 ...
- C#设计模式——装饰者模式(Decorator Pattern)
一.例子在软件开发中,我们往往会想要给某一类对象增加不同的功能.比如要给汽车增加ESP.天窗或者定速巡航.如果利用继承来实现,就需要定义无数的类,Car,ESPCar,CCSCar,SunRoofCa ...
- Asp.Net 三层架构之泛型应用
一说到三层架构,我想大家都了解,这里就简单说下,Asp.Net三层架构一般包含:UI层.DAL层.BLL层,其中每层由Model实体类来传递,所以Model也算是三层架构之一了,例外为了数据库的迁移或 ...
- 改造一下C# Substring()函数
C#的Substring()函数中,如果我们一不小心输入一个截取长度大于字符串的长时,就会收到一个异常:startIndex cannot be larger than length of strin ...
- Chrome弹窗的简单应用(选择结构与循环结构)
★选择结构★ ★JS实现弹窗显示随机数 示例代码效果图 ★ 弹窗实现对随机数的进一步判断 示例代码效果图 ★综合应用 比较大小 ★ 判断成绩等级 ): : : : : alert(" ...
- 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract
[源码下载] 重新想象 Windows 8 Store Apps (37) - 契约: Settings Contract 作者:webabcd 介绍重新想象 Windows 8 Store Apps ...
- 重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性
[源码下载] 重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Apps 之文件系统的新特 ...
- 【NOIP训练】【Tarjan求割边】上学
题目描述 给你一张图,询问当删去某一条边时,起点到终点最短路是否改变. 输入格式 第一行输入两个正整数,分别表示点数和边数.第二行输入两个正整数,起点标号为,终点标号为.接下来行,每行三个整数,表示有 ...
- c语言笔试题(带答案)
填空: 1,short int a[10]={123, 456, 789}; sizeof(a)= 对于64位机来说,指针为8字节表示.其中 sizeof是一运算符,返回编译器为其分配的数组空间大小, ...