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 (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 2ndposition; 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)


Author: DAI, Longao

Source: The 15th Zhejiang University Programming Contest

Submit    Status

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>

using namespace std;

const int dir_x[5]={0,0,0,-1,1};
const int dir_y[5]={0,-1,1,0,0};

int n,m,p;

char mp[20][20];

bool inside(int x,int y)
{
    if((x<0||x>=n)||(y<0||y>=m)) return false;
    return true;
}

struct ZT
{
    int x,y; ///位置
    int cmd; ///命令顺序
    int time;/// 时间
};

int getcmd(int x){return x/1000;}
int shiftL(int x){return (x%1000)*10+x/1000;}
int shiftR(int x){int t=x%10;return x/10+t*1000;}

ZT st,ed;

bool vis[20][20][4];

int bfs()
{
    queue<ZT> q;
    int ans=-1;
    ZT e;
    e.x=st.x; e.y=st.y; e.cmd=1234; e.time=0;
    memset(vis,false,sizeof(vis));
    vis[st.x][st.y][getcmd(e.cmd)]=true;
    q.push(e);
    while(!q.empty())
    {
        ZT u=q.front(); q.pop();

        //cout<<"x: "<<u.x<<" , y: "<<u.y<<endl;
        //cout<<"cmd: "<<u.cmd<<" time "<<u.time<<endl;
        //getchar();

        if((u.x==ed.x&&u.y==ed.y)||(mp[u.x][u.y]=='$'))
        {
            ans=u.time;
            break;
        }

        ZT v;
        /// L shift
        v=u;
        v.cmd=shiftL(v.cmd); v.time++;
        if(v.time%p==0) v.cmd=shiftR(v.cmd);
        if(vis[v.x][v.y][getcmd(v.cmd)]==false)
        {
            vis[v.x][v.y][getcmd(v.cmd)]=true;
            q.push(v);
        }

        /// R shift
        v=u;
        v.cmd=shiftR(v.cmd); v.time++;
        if(v.time%p==0) v.cmd=shiftR(v.cmd);
        if(vis[v.x][v.y][getcmd(v.cmd)]==false)
        {
            vis[v.x][v.y][getcmd(v.cmd)]=true;
            q.push(v);
        }

        /// do nothing
        v=u; v.time++;
        if(v.time%p==0) v.cmd=shiftR(v.cmd);
        if(vis[v.x][v.y][getcmd(v.cmd)]==false)
        {
            vis[v.x][v.y][getcmd(v.cmd)]=true;
            q.push(v);
        }

        /// do it
        v=u;
        int dr=getcmd(v.cmd);
        v.x=v.x+dir_x[dr];v.y=v.y+dir_y[dr];
        if(inside(v.x,v.y)==false) continue;
        if(mp[v.x][v.y]=='*') continue;
        v.time++;
        if(v.time%p==0) v.cmd=shiftR(v.cmd);
        if(vis[v.x][v.y][getcmd(v.cmd)]==false)
        {
            vis[v.x][v.y][getcmd(v.cmd)]=true;
            q.push(v);
        }
    }

    if(ans==-1) puts("YouBadbad");
    else printf("%d\n",ans);
}

int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--)
    {
        memset(mp,0,sizeof(mp));
        scanf("%d%d%d",&n,&m,&p);
        for(int i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for(int j=0;j<m;j++)
            {
                if(mp[i][j]=='@') st.x=i,st.y=j;
                if(mp[i][j]=='$') ed.x=i,ed.y=j;
            }
        }
        bfs();
    }
    return 0;
}
时间: 2024-08-26 16:03:39

ZOJ 3865 Superbot BFS的相关文章

ZOJ 3865 Superbot BFS 搜索

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

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

[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

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 1649 Rescue (bfs+队列)

Rescue Time Limit: 2 Seconds      Memory Limit: 65536 KB Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want

poj 1465 &amp; zoj 1136 Multiple (BFS+余数重判)

Multiple Time Limit: 1000MS   Memory Limit: 32768K Total Submissions: 6177   Accepted: 1346 Description a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the small

ZOJ 1136 Multiple(BFS + 数论 同余剪枝 搜索数字的倍数 )

ZOJ Problem Set - 1136 Multiple Time Limit: 10 Seconds Memory Limit: 32768 KB a program that, given a natural number N between 0 and 4999 (inclusively), and M distinct decimal digits X1,X2..XM (at least one), finds the smallest strictly positive mult

zoj 3890 Wumpus bfs

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3890 Wumpus Time Limit: 2 Seconds      Memory Limit: 65536 KB One day Leon finds a very classic game called Wumpus.The game is as follow. Once an agent fell into a cave. The legend said