为什么本地答案和测试点一样但提交就wa
查看原帖
为什么本地答案和测试点一样但提交就wa
920589
tianye1010楼主2025/7/19 10:57
#include<bits/stdc++.h>
# define fr(i,a,b) for(int i=a;i<=b;i++)
# define fr_(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
using ull = unsigned long long;
typedef long long ll;
typedef unordered_map<string,bool> uap;
struct st {
	ll x,cnt;
	string s;
};
typedef queue<st> que;
const ll N=1e3+5;
char a[N];
que q;
uap bm;
ll n;
ll d[10][4]={2,4,0,0,1,3,5,0,2,6,0,0,1,5,7,0,2,4,6,8,3,5,9,0,4,8,0,0,5,7,9,0,6,8,0,0};
int main() {
	cin>>a+1;
	ll n=strlen(a+1);
	string x="";
	ll x0;
	for(int i=1;i<=n;i++){
		x+=a[i];
		if(a[i]=='0'){
			x0=i;
		}
	}
	q.push({x0-1,0,x});
	while(q.size()){
		st t=q.front();
		if(t.s=="123804765"){
			cout<<t.cnt;
			return 0;
		}
		q.pop();
		//cout<<t.x<<':';
		for(int i=0;d[t.x][i]!=0&&i<4;i++){
			string f=t.s;
			//cout<<d[t.x][i]<<' ';
			f[t.x]=t.s[d[t.x][i]-1];
			f[d[t.x][i]-1]='0';
			if(bm[f]==0){
				//cout<<f<<endl;
				q.push({d[t.x][i]-1,t.cnt+1,f});
				bm[f]=1;
			}else{
				//cout<<"******************"<<endl;
			}
		}
		//cout<<endl;
	}
	return 0;
}

数据: 603712458 正确答案: 23 本地答案: 23 提交答案: 21 qtqt

2025/7/19 10:57
加载中...