ZOJ 3865 Superbot(BFS)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3865

题意:给你一个n*m的地图,求出从@走到$的最小时间.使用按钮去控制机器人走的方向;

四个按钮的初始位置是:左右上下,每隔P秒按钮就会向右移动,变成:下左右上、上下左右、右上下左……

思路:

为了简化按钮的移动步骤,我们直接用二进制给四个位置标记,1代表光标所处位置,0代表无效位置,ex:初始状态就标记为1000;

用dp[i][j][status]标记走到该点所花的最短时间,用vis[i][j][status]标记走过的位置;

机器人有4种状态:1.光标向左移; 2.光标向右移; 3.沿着指定方向走动; 4.什么也不干 

但是一旦到p秒就要被迫转向。

代码如下:

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

using namespace std;
const int N = 15;
int n , m , p;
int dp[N][N][16];
bool vis[N][N][16];
char str[N][N];
int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
struct Point
{
    int x,y;
    int status;
    Point(int x=0, int y=0, int status=0):x(x),y(y),status(status){}
};

Point st , en;

queue<Point> q;

bool ok(int x , int y)
{
    if(x<0 || x>=n || y<0 || y>=m || str[x][y]=='*') return false;
    return true;
}

int getSt(int pre , int flag)
{
    //left move
    if(flag == 1){
        if(pre == 8) return 1;
        else return pre<<1;
    }
    //right move
    else{
        if(pre == 1) return 8;
        else return pre>>1;
    }
}
/*4个步骤,每一步都要判断时间是否会到达p的倍数,到达了因为自动按钮右移
,if(curT % p == 0) curst = getSt(curst , 1);
相当于光标左移,这个不是机器人选择的,而是随着时间被迫的*/
void bfs()
{
    memset(dp,0x3f,sizeof(dp));
    memset(vis,0,sizeof(vis));
    q.push(st);
    dp[st.x][st.y][st.status]=0;
    while(!q.empty())
    {
        Point u = q.front();
        vis[u.x][u.y][u.status]=false;
        q.pop();
        int xx , yy;

        //1.光标左移
        xx = u.x , yy = u.y;
        int curst = getSt(u.status , 1);
        int curT = dp[u.x][u.y][u.status]+1;
        if(curT % p == 0) curst = getSt(curst , 1);
        if(curT < dp[xx][yy][curst])
		{
            dp[xx][yy][curst] = curT;
            if(!vis[xx][yy][curst])
			{
                vis[xx][yy][curst]=true;
                q.push(Point(xx , yy , curst));
            }
        }
        //2.光标右移
        curst = getSt(u.status , 0);
        curT = dp[u.x][u.y][u.status]+1;
        if(curT % p == 0) curst = getSt(curst , 1);
        if(curT < dp[xx][yy][curst])
		{
            dp[xx][yy][curst] = curT;
            if(!vis[xx][yy][curst])
			{
                vis[xx][yy][curst]=true;
                q.push(Point(xx , yy , curst));
            }
        }
        //3.站在原地不动
        curst = u.status;
        curT = dp[u.x][u.y][u.status]+1;
        if(curT % p == 0) curst = getSt(curst , 1);
        if(curT < dp[xx][yy][curst]){
            dp[xx][yy][curst] = curT;
            if(!vis[xx][yy][curst]){
                vis[xx][yy][curst]=true;
                q.push(Point(xx , yy , curst));
            }
        }
        int i;
        //去寻找机器人移动的方向
        for(i=0 ; i<4 ; i++){
            if((1<<i) & u.status) break;
        }
        xx = u.x+dir[i][0];
        yy= u.y+dir[i][1];
        if(!ok(xx , yy)) continue;

        //4.机器人朝当前指定的方向走一格
        curst = u.status;
        curT = dp[u.x][u.y][u.status]+1;
        if(curT % p == 0) curst = getSt(curst , 1);
        if(curT < dp[xx][yy][curst]){
            dp[xx][yy][curst] = curT;
            if(!vis[xx][yy][curst]){
                vis[xx][yy][curst]=true;
                q.push(Point(xx , yy , curst));
            }
        }
    }
}

int main()
{
    int T;
    scanf("%d" , &T);
    while(T--)
    {
        scanf("%d%d%d" , &n , &m , &p);
        for(int i=0; i<n; i++)
            scanf("%s", str[i]);
        for(int i=0; i<n; i++)
		{
            for(int j=0; j<m; j++)
			{
                if(str[i][j]=='@') st=Point(i,j,8);
                else if(str[i][j]=='$') en=Point(i,j,8);
            }
        }
        bfs();
        int ans =0x3f3f3f3f;
        for(int i=0 ; i<16 ; i++)
		{
            ans = min(ans, dp[en.x][en.y][i]);
        }
        if(ans !=0x3f3f3f3f) printf("%d\n" ,ans);
        else printf("YouBadbad\n");
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值