(dfs) hdu 2258

Continuous Same Game (1)

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

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

Recommend

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<queue>
#include<vector>
using namespace std;
int n,m,map[24][24],k,s,tx,ty,ans,vis[24][24];
int dic[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
void dfs(int x,int y,int ss)
{
    int xx,yy;
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        xx=x+dic[i][0],yy=y+dic[i][1];
        if(xx<1||yy<1||xx>n||yy>m||vis[xx][yy])
            continue;
        if(map[xx][yy]==ss)
        {
            s++;
            dfs(xx,yy,ss);
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        ans=0;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%1d",&map[i][j]);
        while(1)
        {
            k=0;
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                {
                    if(map[i][j])
                    {
                        s=1;
                        memset(vis,0,sizeof(vis));
                        dfs(i,j,map[i][j]);
                        if(s>k)
                        {
                            k=s;
                            tx=i,ty=j;
                        }
                    }
                }
            }
            if(k<1) break;
            ans+=k*(k-1);
            //printf("%d\n",ans);
            memset(vis,0,sizeof(vis));
            dfs(tx,ty,map[tx][ty]);
            for(int i=1;i<=n;i++)
            {
                for(int j=1;j<=m;j++)
                    if(vis[i][j])
                        map[i][j]=0;
            }
            for(int j=1;j<=m;j++)
            {
                for(int i=1;i<=n;i++)
                {
                    if(map[i][j]==0)
                    {
                        for(int pos=i;pos>1;pos--)
                        {
                            map[pos][j]=map[pos-1][j];
                        }
                        map[1][j]=0;
                    }
                }
            }
            for(int j=1;j<=m;j++)
            {
                if(map[n][j]==0)
                {
                    for(int pos=j;pos>1;pos--)
                    {
                        for(int i=1;i<=n;i++)
                            map[i][pos]=map[i][pos-1];
                    }
                    for(int i=1;i<=n;i++)
                        map[i][1]=0;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2025-01-04 21:09:23

(dfs) hdu 2258的相关文章

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 remov

D - DFS HDU - 2660

D - DFS HDU - 2660 I have N precious stones, and plan to use K of them to make a necklace for my mother, but she won't accept a necklace which is too heavy. Given the value and the weight of each precious stone, please help me find out the most valua

变形课(DFS hdu 1181)

变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 19133    Accepted Submission(s): 6892 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形咒语的一个统一规

(水DFS) hdu 5024

D - Wang Xifeng's Little Plot Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 5024 Appoint description:  System Crawler  (2015-04-14) Description <Dream of the Red Chamber>(also <The Sto

DFS HDU 1342

很水的DFS Lotto Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1515    Accepted Submission(s): 752 Problem Description In a Lotto I have ever played, one has to select 6 numbers from the set {1,2

DFS HDU 2614

很水的.但是wa了很多遍,因为写num[1][1]... 改成0之后就过了...虽然不知道为什么... Beat Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 659    Accepted Submission(s): 415 Problem Description Zty is a man that always full of

DFS/hdu 1010 Tempter of the Bone

题意 给出一个n*m的地图,s:起点,d:终点,x:障碍物,.:可以走的格子 给出一个时间t,求问是否可以恰好用t时间从起点走到终点.走一步为一个时间. 注意:走过的格子不能再走 注意2:是在时间t刚好到达,而不是时间t以内!也就是说可以在地图上绕远,反正就要在时间t到达! 分析 很普通的深搜,但是有2点剪枝: 1.在刚读入数据后剪枝:当可以走的格子本来就比时间t还短时,必然无法在t时间走到终点 1 if (m*n-xx<=t) //xx is the number of 'X' 2 { 3 p

(DFS) hdu 2208

唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1137    Accepted Submission(s): 309 Problem Description 唉,小朋友是比较麻烦的.在一个幼儿园里,老师要上一节游戏课,有N个小朋友要玩游戏,做游戏时要用小皮球,但是幼儿园里只有M个小皮球,而且有些小朋友不喜欢和一些小朋

(dfs) hdu 2821

Pusher Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others)Total Submission(s): 956    Accepted Submission(s): 348Special Judge Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R