hdu2258Continuous Same Game (1) (消消乐)

来源:互联网 发布:Linux命令如何更换壁纸 编辑:程序博客网 时间:2024/04/28 15:49

Continuous Same Game (1)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 568    Accepted Submission(s): 243


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) ). 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 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 53555231154332222113412314
 

Sample Output
32
Hint
35552 00552 00002 00002 00000 0000031154 05154 05104 00004 00002 0000033222 01222 01222 00122 00104 0010021134 21134 21134 25234 25234 2523012314 12314 12314 12314 12314 12312The total point is 12+6+6+2+6=32.
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  1691 2240 2414 2678 2259 
 

Statistic | Submit | Discuss | Note

我只说两个字:好气啊。

消消乐小游戏。写了这个算法可以写个消消乐了。。。

只不过加个数字交换位置。

这道题应该是比较简单的,难的就是细节。

解题思路:

  • 从左上角到右下角dfs深搜遍历可以消除的最多的数字,记录下其坐标A
  • 根据dfs判断A周围的和A相等的数字,并将其消除(置为0)
  • 更新游戏地图
一个不能忽视的细节就是在更新地图时。
我只给大家两个数据:
5 5
00002
00001
00001
00000
00000
5 5
00002
00001
00001
00030
00000
更新地图后的结果:

代码:
#include <stdio.h>#include <string.h>using namespace std;#define INF 0x3fffffffchar map[25][25];int n,m,sum;int res=0;int dir[4][2]={1,0,-1,0,0,1,0,-1};bool vis[25][25];//测试输出 void print(){for(int i=0;i<m;i++){printf("%s\n",map[i]);}}//边界条件 bool limit(int x,int y){if(x<0||y<0||x>=m||y>=n||map[x][y]=='0')return false;return true;}//深搜 搜索符合条件的一组 void dfs(int x,int y,char c){for(int i=0;i<4;i++){int xx=x+dir[i][0];int yy=y+dir[i][1];if(!vis[xx][yy]&&c==map[xx][yy]&&limit(xx,yy)){vis[xx][yy]=true;sum++;dfs(xx,yy,c);}}}//更新游戏 void updateBlocks(){//垂直方向for(int i=m-1;i>0;i--){for(int j=0;j<n;j++){if(map[i][j]=='0'){int x=i;while(x>=0&&map[x][j]=='0') x--;if(x>=0){map[i][j]=map[x][j];map[x][j]='0';}}}} /*** 判断一整列是否都为‘0’* 若是:左移 */for(int j=n-1;j>=0;j--){//若一列的最下为‘0’则一整列都是‘0’ ,左移 if(map[m-1][j]=='0'){for(int k=j;k<n-1;k++){for(int i=0;i<m;i++){if(map[i][k+1]!='0'){map[i][k]=map[i][k+1]; map[i][k+1]='0';}}}} } //printf("-----------运行结果:-----------------\n");//print();}// 把要移除的一组放入队列  void removeBlocks(int x,int y,char c){map[x][y]='0';for(int i=0;i<4;i++){int xx=x+dir[i][0];int yy=y+dir[i][1];if(c==map[xx][yy]&&limit(xx,yy)){removeBlocks(xx,yy,c);}}}//程序开始 void checkGroups(){memset(vis,0,sizeof(vis));int maxsum=-INF;int x,y;for(int i=0;i<m;i++){for(int j=0;j<n;j++){if(!vis[i][j]&&map[i][j]!='0'){vis[i][j]=true;sum=1;dfs(i,j,map[i][j]);if(maxsum<sum){maxsum=sum;x=i;y=j;}}}}if(maxsum!=INF&&maxsum>=2){res+=maxsum*(maxsum-1);removeBlocks(x,y,map[x][y]);updateBlocks();checkGroups();}}int main(){while(~scanf("%d %d",&m,&n)){res=0;memset(map,0,sizeof(map));for(int i=0;i<m;i++){scanf("%s",map[i]);}checkGroups();printf("%d\n",res);}return 0;} /*测试数据 5 500002000010000100000000005 50000200001000010003000000*/




1 0