1、题目类型:模拟、贪心。
2、解题思路:(1)根据输入的map[][]从左往右逐列、从下往上逐列BFS寻找并标记最大块;(2)对最大块进行删除操作,并更新map[][];(3)知道map[][]中节点删除完毕或者无法再删除,则输出总得分。
3、注意事项:注意运用scanf()获得输入字符串、cin.getline() WA了好些次。
4、实现方法:
#include < iostream >
using namespace std;
int dir[ 4 ][ 2 ] = {
{ 1 , 0 },{ 0 , 1 },{ 0 , - 1 },{ - 1 , 0 }
};
typedef struct Node
{
int x,y;
}queue;
queue que[ 155 ];
char map[ 12 ][ 16 ];
int belong[ 12 ][ 16 ],spaceline[ 16 ];
int index,m;
// 从Node节点BFS标记块,返回块的大小
int BFS( int x, int y)
{
int i,cnt = 1 ,front = 0 ,rear = 0 ;
Node tmp,t;
index ++ ;
tmp.x = x;
tmp.y = y;
belong[x][y] = index;
que[rear ++ ] = tmp;
while (front != rear)
{
tmp = que[front];
front ++ ;
for (i = 0 ;i < 4 ;i ++ )
{
t.x = tmp.x + dir[i][ 0 ];
t.y = tmp.y + dir[i][ 1 ];
if ( t.x >= 10 || t.x < 0 || t.y >= 15 || t.y < 0 || belong[t.x][t.y] || map[t.x][t.y] == ' ' )
continue ;
if ( map[t.x][t.y] == map[tmp.x][tmp.y])
{
belong[t.x][t.y] = index;
cnt ++ ;
que[rear ++ ] = t;
}
}
}
return cnt;
}
// 删除块中各个Node
void Del( int p)
{
int i,j;
for (i = 0 ;i < 10 ;i ++ )
for (j = 0 ;j < 15 ;j ++ )
if (belong[i][j] == p)
{
map[i][j] = ' ' ;
}
}
// 更新map[][]便于下一次遍历
void Change()
{
int i,j,pos,k = 0 ;
bool flag1,flag2;
memset(spaceline, - 1 , sizeof (spaceline));
for (j = 0 ;j < 15 ;j ++ )
{
flag1 = 0 ;
flag2 = 0 ;
for (i = 0 ;i < 10 ;i ++ )
{
if ( ! flag1 && map[i][j] == ' ' )
{
pos = i;flag1 = 1 ;
}
if (map[i][j] != ' ' )
{
flag2 = 1 ;
}
}
if (flag1 && flag2)
{
for (i = pos + 1 ;i < 10 ;i ++ )
{
if (map[i][j] != ' ' )
{
map[pos ++ ][j] = map[i][j];
map[i][j] = ' ' ;
}
}
}
if ( ! flag2 )
{
spaceline[k ++ ] = j;
}
}
if (k)
{
int t = spaceline[ 0 ],p = 1 ;
for (i = spaceline[ 0 ] + 1 ;i < 15 ;i ++ )
{
if (i == spaceline[p])
{
p ++ ;
continue ;
}
for (j = 0 ;j < 10 ;j ++ )
{
map[j][t] = map[j][i];
map[j][i] = ' ' ;
}
t ++ ;
}
}
}
void Solve()
{
int i,j,x,y;
int cost = 0 ,n = 0 ,sum = 0 ;
char ch;
while (cost < 10 * 15 - 1 )
{
n ++ ;
memset(belong, 0 , sizeof (belong));
m = 0 ;
index = 0 ;
for (i = 0 ;i < 15 ;i ++ )
{
for (j = 0 ;j < 10 ;j ++ )
{
if (map[j][i] != ' ' && ! belong[j][i])
{
int t = BFS(j,i);
if (t > m)
{
ch = map[j][i];
m = t;x = j;y = i;
}
}
}
}
if (m < 2 )
break ;
cost += m;
i = (m - 2 ) * (m - 2 );
sum += i;
cout << " Move " << n << " at ( " << x + 1 << ' , ' << y + 1 << " ): removed " << m << " balls of color " << ch << " , got " << i << " points. " << endl;
Del(belong[x][y]);
Change();
}
if (cost == 150 )
sum += 1000 ;
cout << " Final score: " << sum << " , with " << 150 - cost << " balls remaining. " << endl << endl;
}
int main()
{
int c,i,k;
scanf( " %d\n " , & c);
for (k = 1 ;k <= c;k ++ )
{
for (i = 1 ;i <= 10 ;i ++ )
{
scanf( " %s " , & map[ 10 - i]);
}
cout << " Game " << k << ' : ' << endl << endl;;
Solve();
}
return 0 ;
}