ZJU_3048 Continuous Same Game

来源:互联网 发布:淘宝添加客服怎么设置 编辑:程序博客网 时间:2024/06/07 00:34
Continuous Same Game

Time Limit: 1 Second      Memory Limit: 32768 KB

Problem Description

Continuous Same Game is a simple game played on a grid of coloredblocks. Groups of two or more connected (orthogonally, not diagonally)blocks that are the same color may be removed from the board. When agroup of blocks is removed, the blocks above those removed ones falldown 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 fillthe empty columns. Points are scored whenever a group of blocks isremoved. The number of points per block increases as the group becomesbigger. 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 sodifficult to find the optimal scheme. So he always play the game with agreedy strategy: choose the largest group to remove and if there aremore than one largest group with equal number of blocks, choose the onewhich contains the most preceding block ( (x1,y1) is in front of(x2,y2) if and only if (x1<x2 || x1==x2 && y1<y2), wherex stands for the rows from top to bottom and y stands for the columnsfrom 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 mdigits ranging from 1 to 5, indicating the color of the block.

Output

For each test case, output a single line containing the total pointhe will get with the greedy strategy, use '0' to represent emptyblocks.

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.


debug没去掉WA了好几次。。。
比较简单的模拟题,看清楚题目的要求
1.每次都找最大的那块消除,如果有多个块数目相同,那么就找哪个块拥有最靠近左上方的点来消除
2.消除完以后要使非0的数字“下沉”,这倒是不难,关键看下一点
3.题目要求如果一个列全部为0,那么这列右边的所有列都移向左一列,这个需要小心,一开始没注意到这个WA了很多次
  1. #include <iostream>
  2. using namespace std;
  3. int mat[20][20],m,n,tcnt,li,lj,mi,mj;
  4. bool flag[20][20];
  5. const int dir[4][2]={1,0,-1,0,0,1,0,-1};
  6. void getln(int x,int y,int k){
  7. int i,tx,ty;
  8. flag[x][y]=true;
  9. if(x<li||(x==li&&y<lj)){li=x;lj=y;}
  10. for(i=0;i<4;i++)
  11.     {
  12.         tx=x+dir[i][0];
  13.         ty=y+dir[i][1];
  14.         if(tx<0||ty<0||tx>=n||ty>=m||flag[tx][ty]||mat[tx][ty]!=k)continue;
  15.         tcnt++;
  16.         getln(tx,ty,k);
  17.     }
  18. }
  19. void destroy(int x,int y,int k)
  20. {
  21.     int i,tx,ty;
  22.     mat[x][y]=0;
  23.     for(i=0;i<4;i++)
  24.     {
  25.         tx=x+dir[i][0];
  26.         ty=y+dir[i][1];
  27.         if(tx<0||ty<0||tx>=n||ty>=m||mat[tx][ty]!=k)continue;
  28.         tcnt++;
  29.         destroy(tx,ty,k);
  30.     }
  31. }
  32. void move()
  33. {
  34.     int i,j,k;
  35.     for(i=0;i<m;i++)
  36.         {
  37.             for(j=n-1;j>=0;j--)
  38.                 if(mat[j][i]==0)
  39.                     {
  40.                         for(k=j-1;k>=0;k--)
  41.                             if(mat[k][i]) {swap(mat[k][i],mat[j][i]);break;}
  42.                     }
  43.         }
  44.     for(i=0;i<m;i++)
  45.         {
  46.             for(j=n-1;j>=0;j--)
  47.                 {
  48.                     if(mat[j][i])break;
  49.                 }
  50.             if(j==-1)
  51.                 {
  52.                     for(j=i;j<m-1;j++)
  53.                         for(k=0;k<n;k++)mat[k][j]=mat[k][j+1];
  54.                     m--;
  55.                     i=-1;
  56.                 }
  57.         }
  58. }
  59. int main()
  60. {
  61.     int i,j,mcnt,ans;
  62.     while(cin>>n>>m)
  63.         {
  64.             for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%1d",&mat[i][j]);
  65.             ans=0;
  66.             while(1)
  67.                 {
  68.                     mcnt=1;
  69.                     mi=n,mj=m;
  70.                     memset(flag,false,sizeof(flag));
  71.                     for(i=0;i<n;i++)for(j=0;j<m;j++)
  72.                         if(mat[i][j]&&!flag[i][j])
  73.                         {
  74.                             li=n;lj=m;
  75.                             tcnt=1;
  76.                             getln(i,j,mat[i][j]);
  77.                             if(tcnt>mcnt)mcnt=tcnt,mi=li,mj=lj;
  78.                             else if(tcnt==mcnt&&(li<mi||(li==mi&&lj<mj)))mi=li,mj=lj;
  79.                         }
  80.                     if(mcnt==1)break;
  81.                     tcnt=1;
  82.                     destroy(mi,mj,mat[mi][mj]);
  83.                     move();
  84.                     ans+=tcnt*(tcnt-1);
  85.                 }
  86.             cout<<ans<<endl;
  87.         }
  88.     return 0;
  89. }

原创粉丝点击