結果

問題 No.355 数当てゲーム(2)
ユーザー bal4u
提出日時 2019-06-23 09:19:34
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 30,336 KB
実行使用メモリ 25,452 KB
平均クエリ数 21.48
最終ジャッジ日時 2024-07-17 02:17:05
合計ジャッジ時間 4,133 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 52
権限があれば一括ダウンロードができます

ソースコード

diff #

// yukicoder: No.355 数当てゲーム(2)
// 2019.6.23 bal4u

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define gc() getchar()
#define pc(c) putchar(c)
char a[15], f[10];
char ans[15];
int  k;

int ask(char *x) {
	int i;
	pc(x[0]); for (i = 1; i < 4; i++) pc(' '), pc(x[i]);
	pc('\n'), fflush(stdout);
	if ((i = gc() & 0xf) == 4) exit(0);
	gc(), i += gc() & 0xf, gc();
	return i;
}

void rec(int id) {
	int i;
	if (id == 4) ask(ans);
	else {
		for (i = 0; i < 4; i++) if (!f[i]) {
			f[i] = 1, ans[id] = a[i];
			rec(id + 1);
			f[i] = 0;
		}
	}
}

void check(char *x) {
	int i, j, c, t0, t, f;
	char eq[5];
	
	t0 = ask(x);
	if (t0 == 4) {
		for (i = 0; i < 4; i++) ans[x[i] & 0xf] = 1;
		k = 4;
		return;
	}
	f = 0, j = 0;
	for (i = 0; i < 4; i++) {
		c = x[i], x[i] = x[4];
		t = ask(x);
		if (t < t0) k++, ans[c & 0xf] = 1;
		else if (t > t0) ans[x[4] & 0xf] = 1, f = 1;
		else {
			if (f) ans[c & 0xf] = 1;
			else eq[j++] = c;
		}
		x[i] = c;
	}
	if (f) for (i = 0; i < j; i++) ans[eq[i] & 0xf] = 1, k++;
}

int main()
{
	int i, j;
	
	strcpy(a, "01234"), check(a);
	if (k < 4) strcpy(a, "56789"), check(a);
	j = 0; for (i = 0; i < 10; i++) if (ans[i]) a[j++] = i + '0';
	rec(0);
	return 0;
}
0