zoj 3865 Superbot 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 Tindicating 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
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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)





#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int mov[4][2]={{0,-1},{0,1},{-1,0},{1,0}},n,m;
struct point{
    int x,y,time,cur;
};
queue<point>q;
int judge(int x,int y)
{
    if(x<n&&x>=0&&y<m&&y>=0) return 1;
    else return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--){
        int i,j,p,mp[11][11]={0},v[11][11][5]={0},flag=0;
        memset(v,0,sizeof(v));
        char a[11][11]={0};
        point start,en;
        scanf("%d%d%d",&n,&m,&p);
        for(i=0;i<n;i++) scanf("%s",a[i]);
        for(i=0;i<n;i++)
          for(j=0;j<m;j++){
            if(a[i][j]!='*') mp[i][j]=1;
            if(a[i][j]=='@') {start.x=i;start.y=j; }
            if(a[i][j]=='$') {en.x=i;en.y=j; }
        }
        start.time=start.cur=0;
        while(!q.empty()) q.pop();
        q.push(start);
        while(!q.empty()){
            point b=q.front(),c;
            q.pop();
            if(v[b.x][b.y][b.cur]) continue;
            v[b.x][b.y][b.cur]=1;
            //printf("%d %d %d %d\n",b.x,b.y,b.time,b.cur);
            if(b.x==en.x&&b.y==en.y) {flag=1;printf("%d\n",b.time);break;}
            c=b;
            c.x=b.x+mov[b.cur][0]; c.y=b.y+mov[b.cur][1];
            if(judge(c.x,c.y)&&mp[c.x][c.y])
            {
                c.time=b.time+1;
                if(c.time>0&&c.time%p==0) c.cur=(b.cur+3)%4;
                if(!v[c.x][c.y][c.cur])
                    q.push(c);
            }
            c=b;c.time=b.time+1;
            if(c.time>0&&c.time%p==0) c.cur=(c.cur+3)%4;
            if(!v[c.x][c.y][c.cur]) q.push(c);

            c=b;c.time=b.time+1;c.cur=(b.cur+1)%4;
            if(c.time>0&&c.time%p==0) c.cur=(c.cur+3)%4;
            if(!v[c.x][c.y][c.cur]) q.push(c);

            c=b;c.time=b.time+1;c.cur=(b.cur+3)%4;
            if(c.time>0&&c.time%p==0) c.cur=(c.cur+3)%4;
            if(!v[c.x][c.y][c.cur]) q.push(c);

        }
        if(flag==0) printf("YouBadbad\n");
    }
    return 0;
}
/*
10
5 5 2
.....
..@..
.*...
$.*..
.....
2 3 1
*.@
$.*

*/
</pre><pre class="cpp" name="code">另加上同学的代码,觉得代码风格比我好的太多太多
<pre name="code" class="cpp">/*
题目F.Superbot
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5468
题意:给一个图,机器人要按规则到达目的地,问最短时间是多少?
每一秒钟,有三种操作:
1.光标左/右循环移动一格
2.按当前光标所指按键
3.什么都不干
每到一个周期P,仪表器将循环右移一格(相当于光标循环左移一格)(周期检查是在每一次操作后)。
分析:常规BFS。
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
const int maxn = 15;
typedef long long LL;
int N, M, P;
char a[maxn][maxn];
bool vis[maxn][maxn][4];//状态标记vis[x][y][cur]表示当前点坐标为(x,y),光标在第cur个格子。
int dx[4] = { 0, 0, -1, 1 };
int dy[4] = { -1, 1, 0, 0 };
int ans;
struct NODE
{
    int x, y, t, cur;
    NODE(int x = 0, int y = 0, int t = 0, int cur = 0) :x(x), y(y), t(t), cur(cur){}
};
queue<NODE> q;
NODE findch(char ch)
{
    for (int i = 0; i<N; i++)
    for (int j = 0; j<M; j++)
    if (a[i][j] == ch)
        return NODE(i, j);
}
bool check(NODE &New)//检查当前点的时间是否为P的倍数,是则将仪表器循环右移一格
{
    if (New.t%P == 0)
        New.cur = (New.cur - 1 + 4) % 4;
    if (vis[New.x][New.y][New.cur] == true)
        return false;
    return true;
}
bool BFS(NODE st, NODE en)
{
    vis[st.x][st.y][st.cur] = true;
    q.push(st);
    while (!q.empty())
    {
        NODE u = q.front(); q.pop();
        for (int i = 0; i<4; i++)//三种操作(这里将第一种分开两种)
        {
            NODE New;
            if (i == 0)
            {
                int CUR = (u.cur + 1) % 4;
                New = NODE(u.x, u.y, u.t + 1, CUR);
                if (!check(New))    continue;
                vis[New.x][New.y][New.cur] = true;
                q.push(New);
            }
            else if (i == 1)
            {
                int CUR = (u.cur - 1 + 4) % 4;
                New = NODE(u.x, u.y, u.t + 1, CUR);
                if (!check(New))    continue;
                vis[New.x][New.y][New.cur] = true;
                q.push(New);
            }
            else if (i == 2)
            {
                int x = u.x + dx[u.cur];
                int y = u.y + dy[u.cur];
                if (x<0 || N <= x || y<0 || M <= y)  continue;//越界
                if (a[x][y] == '$')
                {
                    ans = u.t + 1;
                    return true;
                }
                else if (a[x][y] == '.')
                {
                    New = NODE(x, y, u.t + 1, u.cur);
                    if (!check(New))    continue;
                    vis[New.x][New.y][New.cur] = true;
                    q.push(New);
                }
            }
            else if (i == 3)
            {
                New = NODE(u.x, u.y, u.t + 1, u.cur);
                if (!check(New))    continue;
                vis[New.x][New.y][New.cur] = true;
                q.push(New);
            }
        }
    }
    return false;
}
void solve()
{
    NODE st = findch('@');//找起点
    NODE en = findch('$');//找终点
    memset(vis, false, sizeof(vis));
    ans = 0;
    while (!q.empty())q.pop();
    if (BFS(st, en))
        printf("%d\n", ans);
    else
        printf("YouBadbad\n");
}
int main()
{
    //freopen("e:\\inputF.txt","r",stdin);
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d", &N, &M, &P);
        for (int i = 0; i<N; i++)
            scanf("%s", a[i]);
        solve();
    }
    return 0;
}


 
 
</pre><pre class="cpp" name="code">


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值