【悬 1 关】A* 算法 90 pts 求助
查看原帖
【悬 1 关】A* 算法 90 pts 求助
737242
linch吃瓜猫楼主2025/2/4 22:31

rt,90pts WA on #6。

#include<bits/stdc++.h>
using namespace std;
int T;
int g[6][6]={
	{0,0,0,0,0,0},
	{0,1,1,1,1,1},
	{0,0,1,1,1,1},
	{0,0,0,0,1,1},
	{0,0,0,0,0,1},
	{0,0,0,0,0,0},
};
struct State{
	bool w[6][6];
	int x,y,dis,val;
	int get_hash(){
		int ret=0;
		for(int i=1;i<=5;i++){
			for(int j=1;j<=5;j++){
				if(i!=x || j!=y) ret=(ret<<1)|w[i][j];
			}
		}
		ret=ret*25+x*5+y;
		return ret;
	}
	int estimate(){
		int ret=0;
		for(int i=1;i<=5;i++){
			for(int j=1;j<=5;j++){
				if(i!=x || j!=y){
					ret+=(w[i][j]!=g[i][j]);
				}
				else if(i!=3 || j!=3){
					ret++;
				}
			}
		}
		return ret;
	}
	bool operator <(const State &xx) const{
		return val<xx.val;
	}
	bool operator >(const State &xx) const{
		return val>xx.val;
	}
};

int dx[8]={1,-1,2,-2,1,-1,2,-2};
int dy[8]={2,-2,1,-1,-2,2,-1,1};
void BFS(State st){
	priority_queue<State,vector<State>,greater<State>>q;
	map<int,int>mp;
	q.push(st);
	while(!q.empty()){
		State u=q.top();
		q.pop();
		if(u.estimate()==0){
			cout<<u.dis<<"\n";
			return;
		}
		if(u.dis>=15){
			cout<<"-1\n";
			return;
		}
		for(int i=0;i<8;i++){
			int xx=u.x+dx[i];
			int yy=u.y+dy[i];
			if(xx>=1 && xx<=5 && yy>=1 && yy<=5){
				State v=u;
				v.dis=u.dis+1;
				v.x=xx,v.y=yy;
				v.val=v.dis+v.estimate();
				swap(v.w[u.x][u.y],v.w[xx][yy]);
				int h=v.get_hash();
				if(!mp.count(h)){
					mp[h]=true;
					q.push(v);
				}
			}
		}
	}
	cout<<"-1\n";
}

int main(){
	cin>>T;
	while(T--){
		State s;
		for(int i=1;i<=5;i++){
			for(int j=1;j<=5;j++){
				char ch;
				cin>>ch;
				if(ch=='*'){
					s.x=i,s.y=j;
					s.w[i][j]=0;
				}
				else s.w[i][j]=ch-'0';
			}
		}
		s.val=s.estimate();
		s.dis=0;
		BFS(s);
	}
	return 0;
}

是这一组数据有错:

input:

1
10111
00111
*1111
00001
00000

output:

8

我的程序输出 10。求大佬帮调/kel

2025/2/4 22:31
加载中...