#include<stdio.h>intcheck(int ans, int guess){
int ansDecompose[4] = {0}, guessDecompose[4] = {0};
int aCnt[10] = {0}, gCnt[10] = {0};
int orig = guess;
int i;
for (i =0; i <4; i++) {
ansDecompose[i] = ans %10;
aCnt[ans %10]++;
ans /=10;
}
for (i =0; i <4; i++) {
guessDecompose[i] = guess %10;
if (gCnt[guess %10] !=0) {
printf("%d is an invalid guess.\n", orig);
return0;
}
gCnt[guess %10]++;
guess /=10;
}
int a =0, b =0;
for (i =0; i <4; i++) {
if (ansDecompose[i] == guessDecompose[i])
a++;
elseif (aCnt[guessDecompose[i]] >0) {
b++;
}
}
printf("%dA%dB\n", a, b);
return a ==4;
}
voidsolve(){
int ans;
scanf("%d", &ans);
int guess;
int history[9999] = {0};
while (scanf("%d", &guess) ==1) {
// check guessing historyif (history[guess] ==1) {
printf("%d is already guessed.\n", guess);
continue;
}
history[guess] =1;
if (check(ans, guess) ==1)
break;
}
}
intmain(){
int ncase;
scanf("%d", &ncase);
while (ncase--) {
solve();
if (ncase)
printf("\n");
}
return0;
}
#include<stdio.h>voidprint(int inp[4][4]){
// printf("==============\n");int i, j;
for (i =0; i <4; i++)
for (j =0; j <4; j++)
printf("%d%c", inp[i][j], j ==3?'\n':' ');
// printf("==============\n");}
intcheckSame(int inp[4][4]){
int i, j, k;
int hasSame =0;
for (i =0; i <4; i++) {
for (j =0; j <4; j++) {
if (inp[i][j] ==0)
continue;
for (k = j +1; k <4; k++) {
if (inp[i][k] ==0)
continue;
if (inp[i][j] == inp[i][k]) {
hasSame =1;
} else {
j = k -1;
break;
}
}
}
}
return hasSame;
}
voidrotate90Left(int inp[4][4]){
int i, j, tmp[4][4];
for (i =0; i <4; i++)
for (j =0; j <4; j++) {
tmp[i][j] = inp[j][4-1- i];
}
for (i =0; i <4; i++)
for (j =0; j <4; j++)
inp[i][j] = tmp[i][j];
}
// 0, no message// 1, has 64// 2, no moveintmove(int inp[4][4]){
// printf("Before\n");// print(inp);int i, j, k;
int has0 =0;
int has64 =0;
for (i =0; i <4; i++) {
for (j =0; j <4; j++) {
if (inp[i][j] ==0)
continue;
for (k = j +1; k <4; k++) {
if (inp[i][k] ==0) // 0, continuecontinue;
if (inp[i][j] == inp[i][k]) { // same, merge inp[i][j] += inp[i][k];
inp[i][k] =0;
break;
} else { // not the same, skipbreak;
}
}
}
}
for (i =0; i <4; i++) {
int lastZero =-1;
for (j =0; j <4; j++) {
if (inp[i][j] ==64)
has64 =1;
if (inp[i][j] ==0)
has0 =1;
if (inp[i][j] !=0&& lastZero !=-1) {
inp[i][lastZero] = inp[i][j]; // move inp[i][j] =0; // fill zero lastZero = j; // crucialcontinue;
}
if (inp[i][j] ==0&&
lastZero ==-1) // update last when no previous zero is present lastZero = j;
}
}
// printf("after\n");// print(inp);if (has64 ==1)
return1;
elseif (has64 ==0&& has0 ==0) {
int hasSame = checkSame(inp);
if (hasSame ==0) {
rotate90Left(inp);
hasSame = checkSame(inp);
rotate90Left(inp);
rotate90Left(inp);
rotate90Left(inp);
}
return hasSame ==0?2:0;
} elsereturn0;
}
voidswap(int*a, int*b){
int tmp =*a;
*a =*b;
*b = tmp;
}
voidsolve(){
int i, j, inp[4][4];
for (i =0; i <4; i++)
for (j =0; j <4; j++)
scanf("%d", &inp[i][j]);
char dir[10];
scanf("%s", dir);
// a left// s down// d right// w up// merge and moveint ret =0;
if (dir[0] =='a') {
ret = move(inp);
} elseif (dir[0] =='d') {
rotate90Left(inp);
rotate90Left(inp);
ret = move(inp);
rotate90Left(inp);
rotate90Left(inp);
} elseif (dir[0] =='s') {
rotate90Left(inp);
rotate90Left(inp);
rotate90Left(inp);
ret = move(inp);
rotate90Left(inp);
} else { // w rotate90Left(inp);
ret = move(inp);
rotate90Left(inp);
rotate90Left(inp);
rotate90Left(inp);
}
print(inp);
if (ret ==1)
printf("You win\n");
elseif (ret ==2)
printf("Game over\n");
}
intmain(){
int ncase;
scanf("%d", &ncase);
while (ncase--) {
solve();
if (ncase)
printf("\n");
}
return0;
}