HDU-2258-Continuous Same Game (1)(DFS)
Problem Description
Continuous Same Game is a simple game played on a grid of colored blocks. Groups of two or more connected (orthogonally, not diagonally) blocks that are the same color may be removed from the board. When a group of blocks is removed,
the blocks above those removed ones fall down into the empty space. When an entire column of blocks is removed, all the columns to the right of that column shift to the left to fill the empty columns. Points are scored whenever a group of blocks is removed.
The number of points per block increases as the group becomes bigger. When N blocks are removed, N*(N-1) points are scored.
LL was interested in this game at one time, but he found it is so difficult to find the optimal scheme. So he always play the game with a greedy strategy: choose the largest group to remove and if there are more than one largest group with equal number of blocks,
choose the one which contains the most preceding block ( (x1,y1) is in front of (x2,y2) if and only if (x1
Input
Each test case begins with two integers n,m ( 5<=n,m<=20 ), which is the size of the board. Then n lines follow, each contains m characters, indicating the color of the block. There are 5 colors, and each with equal probability.
Output
For each test case, output a single line containing the total point he will get with the greedy strategy.
Sample Input
5 5
35552
31154
33222
21134
12314
Sample Output
32
Hint
35552 00552 00002 00002 00000 00000
31154 05154 05104 00004 00002 00000
33222 01222 01222 00122 00104 00100
21134 21134 21134 25234 25234 25230
12314 12314 12314 12314 12314 12312
The total point is 12+6+6+2+6=32.
思路:DFS找最大的聯通塊消去。注意向左移動的時候連續兩列都為空的情況。
#include
int n,m,total,nxt[4][2]={{0,-1},{-1,0},{1,0},{0,1}};
bool vis[20][20];
char d[20][21];
void dfs(int x,int y,int num)
{
for(int i=0;i<4;i++)
{
x+=nxt[i][0];
y+=nxt[i][1];
if(x>=0 && x=0 && y=0 && x=0 && y'0' && !vis[i][j])
{
vis[i][j]=1;
total=1;
dfs(i,j,d[i][j]);
if(total>mx)
{
mx=total;
x=i;
y=j;
}
}
}
}
remain-=mx;
ans+=mx*(mx-1);
tran(x,y,d[x][y]);
d[x][y]='0';
for(i=n-1;i>=0;i--)//向下移動
{
for(j=0;j=0;k--)
{
if(d[k][j]>'0')
{
d[i][j]=d[k][j];
d[k][j]='0';
break;
}
}
}
}
}
t=m-1;
while(t--)//向左移動,注意連續兩列都為空的情況
{
for(j=0;j'0') break;
if(i==n)
{
for(i=0;i