(luogu)P4011 孤岛营救问题

硬是没看出来网络流和SPFA算法的写法
正解:SPFA(最短路)+分层图
我的乱搞算法:BFS+二进制状态压缩
考试的时候脑糊了,毕竟调了一上午,自闭了
巨坑:同一个单元格可能有多把钥匙 XSL
蒟蒻的乱搞代码

#include<bits/stdc++.h>
#define re register int
using namespace std;
int n,m,p,k,s;
int MAP[11][11][11][11],key[11][11],vis[11][11][(1<<11)];
struct node
{
    int a,b,tot,k;
};
const int dx[5]={0,1,-1,0,0},dy[5]={0,0,0,1,-1};
queue<node>q;
int read()
{
    int ans = 0;
    char w = ' ', ch = getchar();
    while(ch < '0' || ch > '9')
    {
        w = ch;
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9')
    {
        ans = (ans << 3) + (ans << 1);
        ans += ch - '0';
        ch = getchar();
    }
    return (w == '-' ? -ans : ans);
}
bool check(int a1,int a2)
{
    return (a1>=1 && a1<=n && a2>=1 && a2<=m);
}
int BFS()
{
    q.push(node{1,1,0,key[1][1]});
    vis[1][1][key[1][1]]=1;
    while(!q.empty())
    {
        node now=q.front();
        q.pop();
        if(now.a==n && now.b==m)
        {
            return now.tot;
        }
        for(re i=1;i<=4;i++)
        {
            int f1=now.a+dx[i];
            int f2=now.b+dy[i];
            if(check(f1,f2))
            {
                if(MAP[now.a][now.b][f1][f2]==-1)
                {
                    continue;
                }
                if(MAP[now.a][now.b][f1][f2]!=0)
                {
                    if((now.k&(1<<MAP[now.a][now.b][f1][f2]-1))==0)
                    {
                        continue;
                    }
                }
                int tt=(now.k|key[f1][f2]);
                if(!vis[f1][f2][tt])
                {
                    vis[f1][f2][tt]=1;
                    q.push(node{f1,f2,now.tot+1,tt});
                }
            }
        }
    }
    return -1;
}
int main()
{
    int a1,a2,b1,b2,g;
    n=read();m=read();p=read();k=read();
    for(re i=1;i<=k;i++)
    {
        a1=read();a2=read();b1=read();b2=read();g=read();
        if(g==0) MAP[a1][a2][b1][b2]=MAP[b1][b2][a1][a2]=-1;
        else MAP[a1][a2][b1][b2]=MAP[b1][b2][a1][a2]=g;
    }
    s=read();
    for(re i=1;i<=s;i++)
    {
        a1=read();a2=read();g=read();
        key[a1][a2]|=(1<<g-1);
    }
    cout<<BFS();
    return 0;
}

转载于:https://www.cnblogs.com/wzztabaorz/articles/11409482.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值