对于set和map有一些疑惑(IDA*)
查看原帖
对于set和map有一些疑惑(IDA*)
800543
NirvanaCeleste楼主2024/6/24 17:49
#include <bits/stdc++.h>
using namespace std;
int tx[4] = {1,0,-1,0};
int ty[4] = {0,1,0,-1};
int n,m,p,k,s,ans = -1,max_dep;
bool pd;
int key_G[12][12];
int G[12][12][12][12];
int jc[12];
struct state{
	bool d[12];
	int x,y,arr;
	bool operator < (const state& a) const{
		if(x != a.x) return x < a.x;
		if(y != a.y) return y < a.y;
		if(arr != a.arr) return arr < a.arr;
	}
};
set<state> b;
void scan(){
	memset(G,-1,sizeof(G));
	cin >> n >> m >> p;
	int tx,ty,dx,dy,h;
	cin >> k;
	for(int i=1;i<=p;i++){
		scanf("%d %d %d %d %d",&tx,&ty,&dx,&dy,&h);
		G[tx][ty][dx][dy] = h;
	}
	cin >> s;
	for(int i=1;i<=s;i++){
		scanf("%d %d %d",&tx,&ty,&h);
		key_G[tx][ty] = h;
	}
}
bool check_ok(int x,int y,int nx,int ny,state nxt_now){
	if(nx < 1 || nx > m || ny < 1 || ny > n) return 0;
//	cout<<x<<" "<<y<<" "<<nx<<" "<<ny<<" "<<endl;
	int kye = G[x][y][nx][ny];
//	cout<<kye;
	if(kye == 0) return 0;
	if(!nxt_now.d[kye]) return 0;
	return 1;
}
int jinzhi(state p){
	int parr = 0;
	for(int i=1;i<12;i++){
		if(p.d[i]) parr +=jc[i];
	}
	return parr;
}
void IDA(state now,int dep){
//	for(int i=1;i<12;i++) cout<<now.d[i]<<" ";
//	cout<<endl<<now.x<<" "<<now.y<<endl;
	if(b.count(now)) return;
	if(now.x == n && now.y == m){
		ans = dep;
		pd = 1;
		return;
	}
	if(dep > max_dep) return;
	for(int i=0;i<4;i++){
		int x = now.x,y = now.y;
		int nxtx = x + tx[i],nxty = y + ty[i];
		state nxt_now = now;
		nxt_now.x = nxtx,nxt_now.y = nxty;
		if(key_G[nxtx][nxty] != 0){
			int kye = key_G[nxtx][nxty];
			nxt_now.d[kye] = 1;	
		}
		nxt_now.arr = jinzhi(nxt_now);
		if(check_ok(x,y,nxtx,nxty,nxt_now)){
			b.insert(nxt_now);
			IDA(nxt_now,dep+1);
			b.erase(nxt_now);
			if(pd) return;
		}
	}
	return;
}
int main(){
	scan();
	state temp_dfs;
	for(int i=0;i<12;i++) temp_dfs.d[i] = 0;
	jc[0] = 1;
	for(int i=1;i<12;i++) jc[i] = jc[i - 1] * 2;
	temp_dfs.arr = 0;
	temp_dfs.x = 1,temp_dfs.y = 1;
	for(max_dep = 1;max_dep <= m * n * s;max_dep *= 2){
		if(pd) break;
		IDA(temp_dfs,0);
	}
	cout<<ans;
	return 0;	
}

只会输出-1

2024/6/24 17:49
加载中...