kmjp's blog

競技プログラミング参加記です

yukicoder : No.2848 Birthday Hit and Blow

ちょっと変わった入力範囲。
https://yukicoder.me/problems/no/2848

問題

グレゴリオ歴における月日を表す、異なる数字4つからなる4桁の整数を考える。
マスターマインドの要領でクエリを実行できるとき、これらの整数をクエリ6回以内に特定せよ。

解法

あり得る回の候補に対し、できるだけ解の候補の最大数を最小化できるクエリを投げよう。
候補に対しクエリを10000通りチェックするとTLEするが、高々100通りぐらいのうち、解の候補の最大数を最小化できるものを試せば十分間に合う。

int M[]={31,29,31,30,31,30,31,31,30,31,30,31};
vector<string> bcand,cand;
int T;


map<pair<string,string>,pair<int,int>> memo;
pair<int,int> hit(string a,string b) {
	if(memo.count({a,b})==0) {
		int hit=0,blow=0;
		int x,y;
		FOR(x,4) FOR(y,4) if(a[x]==b[y]) {
			if(x==y) hit++;
			else blow++;
		}
		memo[{a,b}]={hit,blow};
	}
	return memo[{a,b}];
}

string best() {
	int a,b,c,d;
	int num=101010;
	string be;
	int step=0;
	string X="0000";
	map<pair<int,int>,int> M;
	for(X[0]='0';X[0]<'9';X[0]++)
	for(X[1]='0';X[1]<'9';X[1]++)
	for(X[2]='0';X[2]<'9';X[2]++)
	for(X[3]='0';X[3]<'9';X[3]++) {
		if(X[0]==X[1]) continue;
		if(X[0]==X[2]) continue;
		if(X[0]==X[3]) continue;
		if(X[1]==X[2]) continue;
		if(X[1]==X[3]) continue;
		if(X[2]==X[3]) continue;
		step++;
		if(step>=100) break;
		M.clear();
		int ma=0;
		FORR(c,cand) {
			auto p=hit(c,X);
			M[p]++;
			ma=max(ma,M[p]);
			if(ma>=num) break;
		}
		if(ma<num) {
			num=ma;
			be=X;
		}
		
	}
	return be;
}



void solve() {
	int i,j,k,l,r,x,y; string s;
	
	FOR(i,12) {
		FOR(j,M[i]) {
			string S="0000";
			S[0]+=(i+1)/10;
			S[1]+=(i+1)%10;
			S[2]+=(j+1)/10;
			S[3]+=(j+1)%10;
			
			if(S[0]==S[1]) continue;
			if(S[0]==S[2]) continue;
			if(S[0]==S[3]) continue;
			if(S[1]==S[2]) continue;
			if(S[1]==S[3]) continue;
			if(S[2]==S[3]) continue;
			bcand.push_back(S);
		}
	}
	cin>>T;
	while(T--) {
		cand=bcand;
		//cin>>s;
		k=0;
		while(cand.size()>1) {
			k++;
			string X=best();
			
			cout<<"? "<<X<<endl;
			cin>>x>>y;
			/*
			auto p=hit(s,X);
			x=p.first;
			y=p.second;
			*/
			assert(x!=-1);
			vector<string> nex;
			FORR(c,cand) if(hit(X,c)==make_pair(x,y)) nex.push_back(c);
			cand=nex;
		}
		assert(k<=6);
		cout<<"! "<<cand[0]<<endl;
		cin>>x;
		//assert(x==0);
	}
	
	
}

まとめ

地味にTLE対応に手間取った。