有点繁琐的模拟题。。硬搞吧,感觉和那个popstar的游戏很像,我妈超爱玩。。核心就是BFS,还有那个shift过程,反正细心吧。发现这种苦力代码题submit的人好少。。是都懒的写么。。我承认我比较水只能写写这种蛋疼的苦力代码题了,就当是对代码能力的一种锻炼吧
#include<cstdio>
#include<deque>
#include<cstring>
using namespace std;
namespace
{
char map[10][15], temp[10][15];
int dir[4][2] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
deque<pair<int, int> > queue;
int bfs(char s[10][15], int r, int c)
{
queue.clear();
queue.push_back(make_pair(r, c));
char ch = s[r][c];
s[r][c] = 0;
int i, j, k, res = 1;
while (!queue.empty())
{
pair<int, int> now = queue.front();
queue.pop_front();
for (k = 0; k < 4; k++)
{
i = now.first + dir[k][0];
j = now.second + dir[k][1];
if (i >= 0 && i < 10 && j >= 0 && j < 15 && s[i][j] == ch)
{
queue.push_back(make_pair(i, j));
s[i][j] = 0;
res++;
}
}
}
return res;
}
void shift()
{
bool empty[15];
int i, j, index;
char column[10];
memset(empty, 0, sizeof(empty));
for (j = 0; j < 15; j++)
{
memset(column, 0, sizeof(column));
index = 0;
for (i = 0; i < 10; i++)
if (map[i][j])
column[index++] = map[i][j];
if (index)
{
for (i = 0; i < index; i++)
map[i][j] = column[i];
for (i = index; i < 10; i++)
map[i][j] = 0;
}
else
empty[j] = true;
}
int col = 0;
for (j = 0; j < 15; j++)
if (!empty[j])
{
if (col != j)
for (i = 0; i < 10; i++)
map[i][col] = map[i][j];
col++;
}
for (j = col; j < 15; j++)
for (i = 0; i < 10; i++)
map[i][j] = 0;
}
int remain()
{
int res = 0;
for (int i = 0; i < 10; i++)
for (int j = 0; j < 15; j++)
if (map[i][j])
res++;
return res;
}
void resolve()
{
int i, j, max, next, ri, rj, count = 0, sum = 0;
while (1)
{
memcpy(temp, map, sizeof(map));
max = 1;
for (j = 0; j < 15; j++)
for (i = 0; i < 10; i++)
if (temp[i][j])
{
next = bfs(temp, i, j);
if (next > max)
{
max = next;
ri = i;
rj = j;
}
}
if (max == 1)
break;
printf(
"Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",
++count, ri + 1, rj + 1, max, map[ri][rj],
(max - 2) * (max - 2));
sum += (max - 2) * (max - 2);
bfs(map, ri, rj);
shift();
}
int rem = remain();
if (!rem)
sum += 1000;
printf("Final score: %d, with %d balls remaining.\n", sum, rem);
}
}
int main()
{
int t, N;
scanf("%d", &N);
for (t = 0; t < N; t++)
{
if (t)
putchar('\n');
int n, i, r, c;
scanf("%d", &n);
getchar();
for (i = 1; i <= n; i++)
{
getchar();
for (r = 9; r >= 0; r--)
{
for (c = 0; c < 15; c++)
map[r][c] = getchar();
getchar();
}
if (i != 1)
putchar('\n');
printf("Game %d:\n\n", i);
resolve();
}
}
return 0;
}