#6121.孤岛营救

23 篇文章 0 订阅
23 篇文章 0 订阅

不用搞得很麻烦,直接加一维当前手里的钥匙就好了 

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<int,int>;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;

int n,q,m;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
int g[12][12][12][12];
int key[12][12];
int dist[12][12][1<<11];


void bfs()
{
	memset(dist,0x3f,sizeof dist);
	queue<array<int,3>>q;
	
	q.push({1,1,key[1][1]});
	dist[1][1][key[1][1]] = 0;
	
	
	while(q.size()){
		auto t = q.front();
		q.pop();
		
		int x = t[0],y = t[1],keys = t[2];
		//cout<<x<<" "<<y<<"??\n";
		if(x==n&&y==m){
			cout<<dist[x][y][keys];return;
		}
		for(int i=0;i<4;i++){
			int temx = dx[i]+x,temy = dy[i]+y;
			if(temx<1||temx>n||temy<1||temy>m)continue;
			if(!g[x][y][temx][temy])continue;
			int state = 0;
			if(g[x][y][temx][temy]!=-1)state = (1<<g[x][y][temx][temy]);
		
			
			
			if((keys|state)!=keys)continue;
			
			state = (keys|key[temx][temy]);
			
			
			if(dist[temx][temy][state]>dist[x][y][keys]+1){
				dist[temx][temy][state] = dist[x][y][keys]+1;
				q.push({temx,temy,state});
			}
			
			
		}
		
	}
	
	cout<<-1;return;
	
	
	
	
	
	
}


void solve()
{
	cin>>n>>m>>q;
	cin>>q;
	
	
	memset(g,-1,sizeof g);
	
	
	while(q--){
		int a,b,c,d,e;cin>>a>>b>>c>>d>>e;
		g[a][b][c][d] = e;
		g[c][d][a][b] = e;
	}
	cin>>q;
	while(q--){
		int a,b,c;cin>>a>>b>>c;
		key[a][b]|=(1<<c);
	}
	
	bfs();
	

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值