ZJU_3048 Continuous Same Game
来源:互联网 发布:淘宝添加客服怎么设置 编辑:程序博客网 时间:2024/06/07 00:34
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 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;
- }
- ZJU_3048 Continuous Same Game
- hdu2258 Continuous Same Game (1)
- HDOJ-2258 Continuous Same Game (1) dfs
- hdoj 2258 Continuous Same Game (1) (DFS+模拟)
- ZOJ 3048 (HDU 2258) Continuous Same Game (1)
- HDU-2258-Continuous Same Game (1)(DFS+模拟)
- HDU-2259-Continuous Same Game (2)(BFS+DFS+模拟)
- HDU 2258 Continuous Same Game (1) (dfs+模拟)
- GAME Idea(continuous updating)
- POJ1027--The Same Game
- 【模拟】POJ_1027_The Same Game
- poj1027 The Same Game
- The Same Game
- The Same Game POJ
- poj 1027 The Same Game
- POJ 1027 The Same Game
- poj 1027.The Same Game
- POJ 1027 The Same Game
- 神州数码组织级项目管理发展历程(转)
- 关于JTree的简单示例的补充,处理TreeSelectionEvent事件!
- session祥解
- 关于GIS大学生涯规划的思考
- 神州数码最佳实践之一:向项目管理要效益
- ZJU_3048 Continuous Same Game
- 神州数码最佳实践之二:项目成功与盈利的关键因素——项目估算
- ==从零开始==
- Junit测试实例1
- 神州数码最佳实践之三:以项目为中心的结构设置
- MFC标准dll形式
- 神州数码最佳实践之四:项目的人员考核和激励
- 项目型组织的组织结构设置变迁史
- 纪念日