1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
|
#include <stdio.h>
void print(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");
}
int checkSame(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;
}
void rotate90Left(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 move
int move(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, continue
continue;
if (inp[i][j] == inp[i][k]) { // same, merge
inp[i][j] += inp[i][k];
inp[i][k] = 0;
break;
} else { // not the same, skip
break;
}
}
}
}
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; // crucial
continue;
}
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)
return 1;
else if (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;
} else
return 0;
}
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void solve()
{
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 move
int ret = 0;
if (dir[0] == 'a') {
ret = move(inp);
} else if (dir[0] == 'd') {
rotate90Left(inp);
rotate90Left(inp);
ret = move(inp);
rotate90Left(inp);
rotate90Left(inp);
} else if (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");
else if (ret == 2)
printf("Game over\n");
}
int main()
{
int ncase;
scanf("%d", &ncase);
while (ncase--) {
solve();
if (ncase)
printf("\n");
}
return 0;
}
|