网络流24题14

状压+bfs

https://loj.ac/problem/6121

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int maxn =12;
vector<int> key[maxn][maxn];
int dir[4][2] = {1,0,-1,0,0,1,0,-1};
int door[maxn][maxn][maxn][maxn];
int vis[1<<15][maxn][maxn];
struct node{
    int x,y,step,state;
};
int getans(int stx,int sty,int n,int m){
    int ststate = 0;
    for(int i=0;i<key[stx][sty].size();i++)
    ststate |= (key[stx][sty][i]==0? 0: (1<<key[stx][sty][i]));
    
    node tmp = (node){stx,sty,0,ststate};
    queue<node> q;
    q.push(tmp);
    while (!q.empty()) {
        node now = q.front();
        q.pop();
        if(now.x==n && now.y==m) return now.step;
        for(int i=0;i<4;i++){
            int nx = now.x+dir[i][0];
            int ny = now.y+dir[i][1];
            if(nx<1 || nx>n || ny<1 || ny>m) continue;
            int nstate = now.state ;
            for(int j=0;j<key[nx][ny].size();j++)
            nstate |= (key[nx][ny][j]==0?0:(1<<key[nx][ny][j]));
            if(door[now.x][now.y][nx][ny]!=-1){
                int kk =door[now.x][now.y][nx][ny];
                if((now.state&(1<<kk))==0) continue;
            }
            if(vis[nstate][nx][ny]==1) continue;
            vis[nstate][nx][ny] = 1;
            q.push((node){nx,ny,now.step+1,nstate});
        }
    }
    return 0x3f3f3f3f;
}
int main(int argc, const char * argv[]) {
    int n,m,p;
    scanf("%d%d%d",&n,&m,&p);
    
    int k;
    scanf("%d",&k);
    memset(door, -1, sizeof(door));
    for(int i=0;i<k;i++){
        int x1,y1,x2,y2,g;
        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&g);
        door[x1][y1][x2][y2] = g;
        door[x2][y2][x1][y1] = g;
    }
    int s;scanf("%d",&s);
    for(int i=0;i<s;i++){
        int x1,y1,g;
        scanf("%d%d%d",&x1,&y1,&g);
        key[x1][y1].push_back(g);
    }
    int diss = getans(1,1,n,m);
    if(diss==0x3f3f3f3f){
        puts("-1");
    }else{
        printf("%d\n",diss);
    }
    return 0;
}

  

转载于:https://www.cnblogs.com/tjucxz/p/8665364.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值