题目链接: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 NM (2 <= NM <= 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(优先队列--模板)的更多相关文章

  1. BFS+模拟 ZOJ 3865 Superbot

    题目传送门 /* BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: BFS分4种情况入队,最后在终点4个方向寻找最小值:) */ #include <cstdio&g ...

  2. zoj.3865.Superbot(bfs + 多维dp)

    Superbot Time Limit: 2 Seconds      Memory Limit: 65536 KB Superbot is an interesting game which you ...

  3. 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索

    不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...

  4. ZOJ - 3865 Superbot 【BFS】

    题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865 思路 一个迷宫题 但是每次的操作数和普通的迷宫题不一样 0 ...

  5. Zoj 3865 Superbot

    按规则移动机器人 , 问是否能拾得宝藏 . 加了一个控制板 , 还增加了一个控制板移动周期 p 将移动周期变换一下 , 移动一次  就相当于光标向左不耗费时间的移动了一格 搜索思路 : 搜索当前格子到 ...

  6. POJ-数据结构-优先队列模板

    优先队列模板 优先队列是用堆实现的,所以优先队列中的push().pop()操作的时间复杂度都是O(nlogn). 优先队列的初始化需要三个参数,元素类型.容器类型.比较算子. 需要熟悉的优先队列操作 ...

  7. ZOJ Problem Set - 3865 Superbot (bfs)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5477 大牛博客:http://www.cnblogs.com/kylehz/p ...

  8. HDU 1242 rescue (优先队列模板题)

    Rescue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  9. POJ 1511 Invitation Cards (ZOJ 2008) 使用优先队列的dijkstra

    传送门: http://poj.org/problem?id=1511 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1008 ...

随机推荐

  1. 全新重装win8.1系统后 配置开发及办公环境步骤

    全新重装win8.1系统后 配置开发及办公环境步骤 这两天,系统因配置开发环境出错,重装了一下,为日后方便,故此记录系统配置流程,防日后重装系统计划不周. 安装前,对照步骤,准备好下列安装文件. 0. ...

  2. 在ASP.NET MVC环境中使用加密与解密

    在.NET Framework 4.5的NET框架中,在程序中加密与解密很方便.现在均学习ASP.NET MVC程序了,因此Insus.NET也在此写个学习的例子.在需要时可以参考与查阅. 写一个Ut ...

  3. Ext.NET 4.1 最新版本破解

    Ext.NET 4.1 最新版本破解 今天在将Ext.NET 4.1版本的程序发布到公网时居然要license(localhost和127.0.0.1不收费),而且一年$4999,突然间觉得这是什么鬼 ...

  4. WPF listbox UI虚拟化

    ListBox  默认是UI虚拟化的. 1. 原生使用  <ListBox VirtualizingPanel.IsVirtualizing="True" Virtualiz ...

  5. 【JS复习笔记】07 复习感想

    好吧,其实<JavaScript语言精粹>后面还简单介绍了代码风格,优美特性,以及包含的毒瘤.糟粕. 但我很快就看完了,发现其实都在前面讲过了,所以就不写了. 至今为止已经算是把JavaS ...

  6. iis到w3wp的数据流及工作原理

    HTTP.sys->IO线程-CLR线程池中的worker线程处理 IO线程只负责把请求交给Worker线程或者放入进程池级别的队列,然后又去HTTP.SYS的队列中处理其它的请求

  7. kFreeBSD 7.0于2013/05/04发布 桌面环境 GNOME 3....

    kFreeBSD 7.0于2013/05/04发布 桌面环境 GNOME 3.4, KDE 4.8.4, Xfce 4.8, and LXDE

  8. JMS学习(四) Selector详解

    一.前言 在掌握了消息的结构之后,我们接下来看一下JMS的一个重要功能:选择器.有些时候,作为消费者只希望处理自己感兴趣的消息.如果某个消息只有一个消费者,我们可以在让该客户端根据规则来处理自己感兴趣 ...

  9. 关于html标签和属性的基本理解

    一.关于标签和属性的基本理解: html页面的内容主要由"元素"或"标签"组成.使用标签来描述网页的内容. 标签tag一般都是成对出现,开始标签和结束标签,或者 ...

  10. sphinx使用随笔

    为什么需要进行全文搜索呢? 一个表中有a.b.c多个字段.我们使用sql进行like搜索的时候,往往只能匹配某个字段.或者是这样的形式:a LIKE “%关键词%”or b LIKE “关键词” 这样 ...