P1126 机器人搬重物 ( bfs

#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using PII = pair<int,int>;
using ll = long long;
using ull = unsigned long long;
int a[60][60];
int vis[60][60][5];
int n,m;
int op[] = {1,2,3,4,5};
int rx[] = {2,0,3,1};
int lx[] = {1,3,0,2};
// 0        N
//1 2      w e
// 3        s
int res = 0x3f3f3f3f;
int cx[] = {0,1,1,0};
int cy[] = {0,0,1,1};
struct rob{
    int x,y,to;
};
int sx,sy,ex,ey;
char sto;
int check(int x,int y){
    if(x < 1 || x > n-1 || y < 1 || y > m-1)return 0;

    for(int i=0;i<4;i++){
        int xx = x + cx[i];
        int yy = y + cy[i];
        if(a[xx][yy] == 1) return 0;
    }
    return 1;
}


int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
        }
    cin>>sx>>sy>>ex>>ey>>sto;
    int a;
    if(sto == 'N') a = 0;
    else if (sto == 'S') a = 3;
    else if(sto == 'W') a = 1;
    else if(sto == 'E')a = 2;
    queue<rob> q;
    q.push({sx,sy,a});
    vis[sx][sy][a] = 1;
    while(q.size()){
        auto u = q.front();
        q.pop();
        if(u.x == ex && u.y == ey){
            //break;
            cout<<vis[u.x][u.y][u.to]-1;
            return 0;
        }
        int fail = 0;
        for(int i=0;i<5;i++){
            int x = u.x;
            int y = u.y;
            int to = u.to;
            if(i < 3 && fail == 0){
                switch (to) {
                    case 0: x-=op[i];break;
                    case 1: y-=op[i];break;
                    case 2: y+=op[i];break;
                    case 3: x+=op[i];break;
                }
                if(check(x,y) == 0) {
                    fail = 1;
                    continue;
                }
                if(!vis[x][y][to]){
                    q.push({x,y,to});
                    vis[x][y][to] = vis[u.x][u.y][u.to] + 1;
                }

            }else if(i==3){
                to = rx[u.to];
                if(!vis[x][y][to]){
                    q.push({x,y,to});
                    vis[x][y][to] = vis[u.x][u.y][u.to] + 1;
                }

            }else if(i==4){
                to = lx[u.to];
                if(!vis[x][y][to]){
                    q.push({x,y,to});
                    vis[x][y][to] = vis[u.x][u.y][u.to] + 1;
                }

            }

        }
    }
 
    cout<<-1;
    return 0;

}

大模拟,甩写一百行

有一个特殊情况就是如果走一步撞墙了,那么后续走2,3步都否决掉,直接转向

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值