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<x2 || x1==x2 && y1<y2), where x stands for the rows from top to bottom and y stands for the columns from left to right). Now, he want to know how many points he will get. Can you help him?
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 digits ranging from 1 to 5, indicating the color of the block.
Output
For each test case, output a single line containing the total point he will get with the greedy strategy, use '0' to represent empty blocks.
Sample Input
5 5Sample Output
35552
31154
33222
21134
12314
32Hint
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.
debug没去掉WA了好几次。。。
比较简单的模拟题,看清楚题目的要求
1.每次都找最大的那块消除,如果有多个块数目相同,那么就找哪个块拥有最靠近左上方的点来消除
2.消除完以后要使非0的数字“下沉”,这倒是不难,关键看下一点
3.题目要求如果一个列全部为0,那么这列右边的所有列都移向左一列,这个需要小心,一开始没注意到这个WA了很多次
- #include <iostream>
- using namespace std;
- int mat[20][20],m,n,tcnt,li,lj,mi,mj;
- bool flag[20][20];
- const int dir[4][2]={1,0,-1,0,0,1,0,-1};
- void getln(int x,int y,int k){
- int i,tx,ty;
- flag[x][y]=true;
- if(x<li||(x==li&&y<lj)){li=x;lj=y;}
- for(i=0;i<4;i++)
- {
- tx=x+dir[i][0];
- ty=y+dir[i][1];
- if(tx<0||ty<0||tx>=n||ty>=m||flag[tx][ty]||mat[tx][ty]!=k)continue;
- tcnt++;
- getln(tx,ty,k);
- }
- }
- void destroy(int x,int y,int k)
- {
- int i,tx,ty;
- mat[x][y]=0;
- for(i=0;i<4;i++)
- {
- tx=x+dir[i][0];
- ty=y+dir[i][1];
- if(tx<0||ty<0||tx>=n||ty>=m||mat[tx][ty]!=k)continue;
- tcnt++;
- destroy(tx,ty,k);
- }
- }
- void move()
- {
- int i,j,k;
- for(i=0;i<m;i++)
- {
- for(j=n-1;j>=0;j--)
- if(mat[j][i]==0)
- {
- for(k=j-1;k>=0;k--)
- if(mat[k][i]) {swap(mat[k][i],mat[j][i]);break;}
- }
- }
- for(i=0;i<m;i++)
- {
- for(j=n-1;j>=0;j--)
- {
- if(mat[j][i])break;
- }
- if(j==-1)
- {
- for(j=i;j<m-1;j++)
- for(k=0;k<n;k++)mat[k][j]=mat[k][j+1];
- m--;
- i=-1;
- }
- }
- }
- int main()
- {
- int i,j,mcnt,ans;
- while(cin>>n>>m)
- {
- for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%1d",&mat[i][j]);
- ans=0;
- while(1)
- {
- mcnt=1;
- mi=n,mj=m;
- memset(flag,false,sizeof(flag));
- for(i=0;i<n;i++)for(j=0;j<m;j++)
- if(mat[i][j]&&!flag[i][j])
- {
- li=n;lj=m;
- tcnt=1;
- getln(i,j,mat[i][j]);
- if(tcnt>mcnt)mcnt=tcnt,mi=li,mj=lj;
- else if(tcnt==mcnt&&(li<mi||(li==mi&&lj<mj)))mi=li,mj=lj;
- }
- if(mcnt==1)break;
- tcnt=1;
- destroy(mi,mj,mat[mi][mj]);
- move();
- ans+=tcnt*(tcnt-1);
- }
- cout<<ans<<endl;
- }
- return 0;
- }