没有什么技术含量的BFS,因为在数量相同的情况下要优先选择左边的,其次是下面的。
所以找的时候要从最左边的一列开始,每一列从下面开始。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
struct N
{
int x,y,clu;
}re;
struct Q
{
int r,c;
}t1,t2;
queue<Q> q;
char cb[15][20];
bool MarkVisit[15][20];
int bfs(int r,int c)
{
int jr[] = {-1, 0, 1, 0};
int jc[] = { 0 ,1, 0,-1};
int i,ans = 1;
t1.r = r;
t1.c = c;
q.push(t1);
MarkVisit[r][c] = true;
while(q.empty() == false)
{
t1 = q.front();
q.pop();
for(i = 0;i < 4; ++i)
{
t2.r = t1.r + jr[i];
t2.c = t1.c + jc[i];
if(1 <= t2.r && t2.r <= 10 && 1 <= t1.c && t1.c <= 15 && MarkVisit[t2.r][t2.c] == false && cb[t2.r][t2.c] == cb[t1.r][t1.c])
{
q.push(t2);
MarkVisit[t2.r][t2.c] = true;
ans ++;
}
}
}
return ans;
}
void delcb(int r,int c)
{
memset(MarkVisit,false,sizeof(MarkVisit));
int jr[] = {-1, 0, 1, 0};
int jc[] = { 0 ,1, 0,-1};
int i,j,k;
t1.r = r;
t1.c = c;
q.push(t1);
MarkVisit[r][c] = true;
while(q.empty() == false)
{
t1 = q.front();
q.pop();
for(i = 0;i < 4; ++i)
{
t2.r = t1.r + jr[i];
t2.c = t1.c + jc[i];
if(1 <= t2.r && t2.r <= 10 && 1 <= t1.c && t1.c <= 15 && MarkVisit[t2.r][t2.c] == false && cb[t2.r][t2.c] == cb[t1.r][t1.c])
{
q.push(t2);
MarkVisit[t2.r][t2.c] = true;
}
}
}
for(i = 1;i <= 10; ++i)
{
for(j = 1;j <= 15; ++j)
{
if(MarkVisit[i][j])
cb[i][j] = 'a';
}
}
for(i = 1;i <= 15; ++i)
{
for(k = 0;k <= 10; ++k)
{
for(j = 1;j <= 10; ++j)
{
if(cb[j][i] == 'a')
{
if(j != 10)
{
cb[j][i] = cb[j+1][i];
cb[j+1][i] = 'a';
}
}
}
}
}
for(k = 1 ; k <= 15; ++k)
{
for(i = 1;i <= 15; ++i)
{
for(j = 1;j <= 10; ++j)
{
if(cb[j][i] != 'a')
break;
}
if(j == 11)
{
if(i != 15)
{
for(j = 1;j <= 10; ++j)
{
cb[j][i] = cb[j][i+1];
cb[j][i+1] = 'a';
}
}
}
}
}
}
int main()
{
int T,icase = 0;
int i,j,ans,step;
int sumdel,getpoint,del;
scanf("%d",&T);
while(T--)
{
step = 0;
sumdel = 0;
getpoint = 0;
del = 0;
for(i = 10;i >= 1 ; --i)
cin>>(cb[i]+1);
printf("Game %d:\n\n",++icase);
while(1)
{
re.x = -1;
re.y = -1;
re.clu = -1;
memset(MarkVisit,false,sizeof(MarkVisit));
for(j = 1;j <= 15; ++j)
{
for(i = 1;i <= 10; ++i)
{
if(MarkVisit[i][j] == false && cb[i][j] != 'a')
{
ans = bfs(i,j);
if(ans > re.clu)
{
re.x = i;
re.y = j;
re.clu = ans;
}
else if(ans == re.clu)
{
if(j < re.y)
{
re.x = i;
re.y = j;
re.clu = ans;
}
else if(j == re.y)
{
if(i < re.x)
{
re.x = i;
re.y = j;
re.clu = ans;
}
}
}
}
}
}
if(re.clu < 2)
break;
getpoint += (re.clu-2)*(re.clu-2);
sumdel += re.clu;
printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",++step,re.x,re.y,re.clu,cb[re.x][re.y],(re.clu-2)*(re.clu-2));
delcb(re.x,re.y);
}
if(sumdel == 150)
{
getpoint += 1000;
}
printf("Final score: %d, with %d balls remaining.\n\n",getpoint,150-sumdel);
}
return 0;
}