题目链接: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. Linq专题之查询操作

    前面我们主要讲解的是Linq的查询表达式,Linq不但提供了一些基本的查询表达式,还提供了数十个查询操作.比如筛选操作.聚合操作.投影操作等等.通过这些查询操作可以更方便的对数据源进行处理. Linq ...

  2. C#如何获取CPU处理器核心数量

    有几条不同的处理器信息,您可以获得有关的信息:物理处理器数量.核心数量和逻辑处理器数量,这些可以不同.两颗双核超线程(启用)处理器的机器情况下有:2个物理处理器.4个核心和8个逻辑处理器. 逻辑处理器 ...

  3. 怎样用C#代码屏蔽任务管理器?

    这是我在网上找的并多加了一些我自己需要的代码,经过我的测试,可以屏蔽任务管理器,但还有一些瑕疵. 首先,我在vs2012中新建一个项目,选择window下的window窗体应用程序,把窗体form1拉 ...

  4. csharp:引入app.manifest,程序在win7下以管理员权限运行配置方法

    https://msdn.microsoft.com/en-us/library/windows/desktop/hh848036(v=vs.85).aspx https://msdn.microso ...

  5. 设计模式总结篇系列:命令模式(Command)

    在程序设计中,经常会遇到一个对象需要调用另外一个对象的某个方法以达到某种目的,在此场景中,存在两个角色:请求发出者和请求接收者.发出者发出请求,接收者接收请求并进行相应处理.有时候,当需要对请求发出者 ...

  6. Runtime -----那些被忽略的技能

            有人说现在的程序员都被惯坏了,尤其使用一些面向对象的语言开发的时候,只是简单的调用一些系统封装好的接口或者是调用一些“便利的”第三方,对于一个程序的真正实现有了解吗???又有多少了解呢 ...

  7. AutoMapper映射ExpressionTree

    问题描述 项目中使用AutoMapper进行VO&DTO&Entity的互相映射,但是默认Map方法不支持Expression的转换.如 Expression<Func<E ...

  8. webpack学习(入门基础)

    webpack的学习webpack是什么?1:加载模块(将JS.sass/less.图片等作为模块来处理使用) 2:进行打包 webpack的优势?1:webpack以commonJS(JS的规范)的 ...

  9. js的基本数据类型有哪些?

    js的基本数据类型有哪些? ECMAScript中有5中简单数据类型(也称为基本数据类型): Undefined.Null.Boolean.Number和String.还有1中复杂的数据类型----O ...

  10. UnityShader之Shader分类篇【Shader资料2】

    关于ShaderLab,从我个人的理解上来看应该是分为三种类型. 1.Fixed function shader 固定渲染管线Shader,基于用于高级Shader在老显卡无法显示时的Fallback ...