题目链接: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. sql where 1=1和 0=1 的作用

    sql where 1=1和 0=1 的作用 摘自: http://www.cnblogs.com/junyuz/archive/2011/03/10/1979646.html where 1=1; ...

  2. WinFrom子窗体向父窗体传值

    父窗框mainForm;子窗体childForm,利用事件进行传值 在子窗体中的操作: public event EventHandler accept;public string value; pr ...

  3. 获取datatable更新之前的数据

    string dd = ds.Tables[0].Rows[0][0, DataRowVersion.Original].ToString() ;

  4. JavaScripts 基础详细笔记整理

    一.JS简介 JavaScript 是 Web 的编程语言,浏览器内置了JavaScript语言的解释器,所以在浏览器上按照JavaScript语言的规则编写相应代码之,浏览器可以解释并做出相应的处理 ...

  5. sqlserver允许远程连接的配置

    如果无法通过IP地址远程连接你服务器上的SQL Server服务器,可以参考下面的内容进行设置 在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器已经正常连接到网络中. 1.单击Window ...

  6. Hosts文件小结

    今天又遇到Hosts,小结一下: Hosts文件是什么? Hosts文件从表象上来看是一个没有扩展名的系统文件.其基本作用就是将一些常用的网址域名与其对应的IP地址建立一个关联"数据库&qu ...

  7. 穷举法破解 zebrone1.1

    系统 : Windows xp 程序 : zebrone1.1 程序下载地址 :http://pan.baidu.com/s/1boqVcU7 要求 : 编写注册机 使用工具 :OD 可在看雪论坛中查 ...

  8. 上传Android代码到Jcenter(解决了字符映射的问题)

    请先阅读:http://blog.saymagic.cn/2015/02/16/release-library-to-jcenter.html 最外面的build.gradle // Top-leve ...

  9. Android 开关按钮切换,类似于iphone 效果,view实现

    1.实现的效果 gitHub :  https://github.com/zcweng/ToggleButton

  10. 关于停止AsyncTask和Thread的问题

    在java的线程中,没有办法停止一个正在运行中的线程.在Android的AsyncTask中也是一样的.如果必须要停止一个线程,可以采用这个线程中设置一个标志位,然后在线程run方法或AsyncTas ...