首页 > 代码库 > ZOJ 3048 (HDU 2258) Continuous Same Game (1)
ZOJ 3048 (HDU 2258) Continuous Same Game (1)
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
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 5 35552 31154 33222 21134 12314Sample Output
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.
好恶心的一道模拟题
题意:给你一个方格,有不同颜色,消除颜色会有下移和左移(如果出现空格)
做了一,感觉不会做二了,上挫码=。=,dfs搜联通块。
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> using namespace std; int mp[22][22]; int visit[22][22]; int dr[4][2]={{-1,0},{1,0},{0,-1},{0,1}}; int n,m,s; void dfs(int x,int y,int sum)//dfs搜索联通区域 { visit[x][y]=1; for(int i=0;i<4;i++) { int xx=x+dr[i][0]; int yy=y+dr[i][1]; if(!visit[xx][yy]&&mp[xx][yy]==sum) { s++; dfs(xx,yy,sum); } } return ; } int main() { int sx,sy; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) scanf("%1d",&mp[i][j]); } int ans=0; while(1) { int k=0; for(int i=1;i<=n;i++)//选择最大的联通块 { for(int j=1;j<=m;j++) { if(mp[i][j]) { memset(visit,0,sizeof(visit)); s=1; dfs(i,j,mp[i][j]); if(s>k) { sx=i; sy=j; k=s; } } } } if(k<1) break;//不要写成k<=1=。= ans+=k*(k-1); memset(visit,0,sizeof(visit));//对联通块进行标志 dfs(sx,sy,mp[sx][sy]); for(int i=1;i<=n;i++)//联通块消0 { for(int j=1;j<=m;j++) { if(visit[i][j]) mp[i][j]=0; } } for(int j=1;j<=m;j++)//竖向消0 { for(int i=1;i<=n;i++) { if(mp[i][j]==0) { for(int k=i;k>1;k--) mp[k][j]=mp[k-1][j]; mp[1][j]=0; } } } for(int j=1;j<=m;j++)//向左移动 { if(mp[n][j]==0) { for(int k=j;k>1;k--) { for(int l=1;l<=n;l++) mp[l][k]=mp[l][k-1]; } for(int i=1;i<=n;i++) mp[i][1]=0; } } } printf("%d\n",ans); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。