ZOJ 3865 Superbot(BFS水题)

比赛的时候题意理解错了,不想多说什么了。。

水题一道,,给你四个按钮,每次你可以选择不动,左右移动按钮,按钮每P秒右移一次,,水啊

#include "stack"
#include "cstdio"
#include "iostream"
#include "cmath"
#include "set"
#include "sstream"
#include "cctype"
#include "string"
#include "cstring"
#include "algorithm"
#include "queue"
#include "map"
using namespace std;

int n,m,k;
char mp[12][12];
int vis[12][12][5][5]; // 记录当前按钮位置 以及移动的次数
int dis[4][2]= {0,-1,0,1,-1,0,1,0};
struct node
{
    int x,y,f,l,t;
};

int judge(node a)
{
    if(a.x<0 || a.y<0 || a.x>=n || a.y>=m) 
        return 0;
    if(vis[a.x][a.y][a.f][a.l]==1) 
        return 0;
    if(mp[a.x][a.y]=='*') 
        return 0;
    return 1;
}

int bfs(int x,int y)
{
    
    memset(vis,0,sizeof(vis));
    node cur,next;
    cur.x=x;
    cur.y=y;
    cur.t=0;
    cur.f=0;
    cur.l=0;
    queue<node>q;
    q.push(cur);
    vis[x][y][0][0]=1;
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(mp[cur.x][cur.y]=='$') 
            return cur.t;
        //一共四种选择,一个个对应下来进行bfs就好了
        next=cur;
        next.t=cur.t+1;
        next.l=(next.t/k)%4;
        if(judge(next))
        {
            vis[next.x][next.y][next.f][next.l]=1;
            q.push(next);
        }
        
        next=cur;
        next.t=cur.t+1;
        next.f=(next.f+1)%4;
        next.l=(next.t/k)%4;
        if(judge(next))
        {
            vis[next.x][next.y][next.f][next.l]=1;
            q.push(next);
        }
        
        next=cur;
        next.t=cur.t+1;
        next.f=(next.f-1+4)%4;
        next.l=(next.t/k)%4;
        if(judge(next))
        {
            vis[next.x][next.y][next.f][next.l]=1;
            q.push(next);
        }
        
        next.x=cur.x+dis[(cur.f-cur.l+4)%4][0];
        next.y=cur.y+dis[(cur.f-cur.l+4)%4][1];
        next.f=cur.f;
        next.t=cur.t+1;
        next.l=(next.t/k)%4;
        if(judge(next))
        {
            vis[next.x][next.y][next.f][next.l]=1;
            q.push(next);
        }
    }
    return -1;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int sx,sy;
        scanf("%d%d%d",&n,&m,&k);
        for(int i=0; i<n; i++) 
            scanf("%s",mp[i]);
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                if(mp[i][j]=='@')
                {
                    sx=i;
                    sy=j;
                }
            }
        }
        int ans=bfs(sx,sy);
        if(ans==-1) 
        puts("YouBadbad");
        else 
        printf("%d\n",ans);
    }
    
    return 0;
    
}




好想哭啊~!@~!#~!~~~

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值