zoj 3865

Superbot


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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 4thposition; 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; 3rd moves 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
.....
[email protected]
.*...
$.*..
.....
2 3 1
*[email protected]
$.*
5 5 2
*****
[email protected]
*****
$....
.....

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)



题目描述:

给定一张图,有一个键盘,通过键盘可以控制机器人的行走方向,

求从@到¥处的最短距离,‘.‘表示通路,;‘*‘表示不同,每隔p秒键盘旋转一次。

这题第一反应不知为何是深搜,其实深搜也可以吧,通过不同的行走方式,到达¥处,在@的四个方向处调用

dfs(),维护最小值,不过没写。还是写宽搜吧。

渐渐的感到搜索算法跟dp差不多嘛。状态*选择。

不过之前的想法是按机器人所在位置为状态,进行四个方向的搜索,如果到达p

秒,进行旋转,不过要是在对方向键的移动中,旋转时间到了,怎么办?。

看了别人的代码,就设立状态[i][j][cur][times]表示每秒机器人所在位置,以及光标所处方向键,

此时所用时间,用结构体存储。

有四种选择,1,按着光标方向走 2 光标左移 3 光标右移 4 光标不动 ,至于每隔p秒旋转一次,

如果下一秒要旋转,那么有两种方式,a 这一秒先旋转,再做选择,b 先做选择,下一秒再做旋转。

不知为何b方式代码不能过。设立d[i][j][cur]表示此种状态有没有被选择过;用v[i][j]表示i,j位置有没有走过。

由于是宽搜,先搜到的就是最短距离。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define  maxn 50
using namespace std;
int N,M,P;
char a[maxn][maxn];
int d[maxn][maxn][10];
int v[maxn][maxn];
struct node
{
    int x,y,cur,times;
};
 int walk[4][2]={0,-1,0,1,-1,0,1,0};
inline void init()
{
   memset(d,0,sizeof(d));
   memset(v,0,sizeof(v));
   memset(a,0,sizeof(a));
}
int rolate(int des)
{
   switch(des)
   {
     case 0 :
       return 3;
     case 1:
       return 0;
     case 2:
       return 1;
     case 3:
       return 2;
   }
}
void solve()
{
     node sou;
     int ok=0;
    for(int i=1;i<=N;i++)
    {
          for(int j=1;j<=M;j++)
        {
            if(a[i][j]==‘@‘)
            {
              sou.x=i;sou.y=j;sou.cur=0;sou.times=0;
              ok=1;
              break;
            }
        }
        if(ok==1)
         break;
    }

    queue <node>  que;
   // v[sou.x][sou.y]=1;
   d[sou.x][sou.y][sou.cur]=1;
    que.push(sou);
    while(!que.empty())
    {
        node _current=que.front();
        que.pop();
//printf("%d %d %d当前时间:%d\n",_current.x,_current.y,_current.cur,_current.times);
        node _next;

        _next.x=_current.x+walk[_current.cur][0];
        _next.y=_current.y+walk[_current.cur][1];
        _next.cur=_current.cur;    //按下光标
        _next.times=_current.times;
        if(_next.x>=1 && _next.x<=N && _next.y>=1 && _next.y<=M)
        {
            if(v[_next.x][_next.y]==0)
          {
            if(a[_next.x][_next.y]==‘.‘)
             {
               // v[_next.x][_next.y]=1;
                //d[_next.x][_next.y][_next.cur]=1;
                _next.times++;
                if(_next.times%P==0)
                {
                 _next.cur=rolate(_next.cur);
                 d[_next.x][_next.y][_next.cur]=1;

                }
                // printf("走%d %d %d %d\n",_next.x,_next.y,_next.cur,_next.times);
                que.push(_next);
            }
            else if(a[_next.x][_next.y]==‘$‘)
            {
               _next.times++;
                printf("%d\n",_next.times);
                return ;
            }
           }

        }

        if((_current.times+1)%P==0)
         _current.cur=rolate(_current.cur);
        _next.x=_current.x;
        _next.y=_current.y;

        _next.cur=(_current.cur+3)%4; //光标左移
        _next.times=_current.times;
        if(d[_next.x][_next.y][_next.cur]==0)
        {
           d[_next.x][_next.y][_next.cur]=1;
            _next.times++;
           // if(_next.times%P==0)  //选择此时旋转,为何不行?
               // _next.cur=rolate(_next.cur);
               // printf("左移%d\n",_next.times);
            que.push(_next);
        }

         _next.cur=(_current.cur+1)%4;  //光标右移
         _next.times=_current.times;
         if(d[_next.x][_next.y][_next.cur]==0)
         {
            d[_next.x][_next.y][_next.cur]=1;
            _next.times++;
           // if(_next.times%P==0)
               // _next.cur=rolate(_next.cur);
               // printf("右移%d\n",_next.times);
             que.push(_next);
         }

       // if(que.size()==1)
          // break;
        _next.cur=_current.cur;  //光标不动
        _next.times=_current.times;
      if( d[_next.x][_next.y][_next.cur]==0 )
      {
        d[_next.x][_next.y][_next.cur]=1;
            _next.times++;
           //if(_next.times%P==0)
              //  _next.cur=rolate(_next.cur);
               // printf("不动%d\n",_next.times);
            que.push(_next);
      }
    }
    printf("YouBadbad\n");
}
int main()
{
 //freopen("test.txt", "r", stdin);
    int T;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d%d%d",&N,&M,&P);
      for(int i=1;i<=N;i++)
        scanf("%s",a[i]+1);
      solve();
    }

}
时间: 2024-10-05 05:11:53

zoj 3865的相关文章

BFS+模拟 ZOJ 3865 Superbot

题目传送门 1 /* 2 BFS+模拟:dp[i][j][p] 表示走到i,j,方向为p的步数为多少: 3 BFS分4种情况入队,最后在终点4个方向寻找最小值:) 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <queue> 11 usi

ZOJ 3865 Superbot BFS

地图很小,根据题意BFS Superbot Time Limit: 2 Seconds      Memory Limit: 65536 KB 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 (1s

zoj 3865 Superbot

题目的状态是比较少的,可以BFS一一个单位时间为一步,暴力化的搜索所有状况的.不熟悉这种类型,又被题目的条件吓到了,各种处理,结果却做不出来. 对于路径搜索或是其他采用bfs其每一步的花费可能不同时,可以采用优先队列,将一步分为多步走方法较好,能保持简单情况时的模型. #include<iostream> #include<cstring> #include<cstdio> #include<queue> using namespace std; const

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

ZOJ 3865 Superbot BFS 搜索

不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4}; 我定义了一个方向数组,其实题目意思中的,指针移动还有操作版的变化本质上都是指针的移动 在此只需要 额外定义一个变量 cur 在数组 commandi 中循环移动即可 这道题目还是因为数据量不大吧,直接用 STL 中的 Queue 即可,优先队列肯定会更快. 总体来说,还是一道容易题. Sour

[bfs] zoj 3865 Superbot

题意: 给一个n*m的图. '@'代表你的位置,'.'代表空地,'*'代表墙,'$'代表钻石. 在每一秒钟你有四种选择. 1.站着不动. 2.光标往左移动一格. 3.光标往右移动一格. 4.点击光标让自己按光标的方向移动一格. 然后题目还给了一个k,代表每k秒光标整体循环右移一格. 现在问你拿到钻石的最少步数. 思路: 本弱开了一个四维数组判重use[x][y][f][l] 在(x,y)位置光标在f,面板移动了l次. 然后搜就可以了~ 代码: #include"stdio.h" #in

模拟3

    ID Origin Title 6 / 12 Problem A ZOJ 3860 Find the Spy 6 / 27 Problem B ZOJ 3861 Valid Pattern Lock     Problem C ZOJ 3862 Intersection   0 / 1 Problem D ZOJ 3863 Paths on the Tree     Problem E ZOJ 3864 Quiz for EXO-L     Problem F ZOJ 3865 Supe

概率dp ZOJ 3640

Help Me Escape Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3640 Appoint description:  System Crawler  (2014-10-22) Description Background     If thou doest well, shalt thou not be accepted? an

zoj 2156 - Charlie&#39;s Change

题目:钱数拼凑,面值为1,5,10,25,求组成n面值的最大钱币数. 分析:dp,01背包.需要进行二进制拆分,否则TLE,利用数组记录每种硬币的个数,方便更新. 写了一个 多重背包的 O(NV)反而没有拆分快.囧,最后利用了状态压缩优化 90ms: 把 1 cents 的最后处理,其他都除以5,状态就少了5倍了. 说明:貌似我的比大黄的快.(2011-09-26 12:49). #include <stdio.h> #include <stdlib.h> #include <