HDU-2258-Continuous Same Game (1)(DFS)

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.

思路:DFS找最大的联通块消去。注意向左移动的时候连续两列都为空的情况。

#include <stdio.h>

int n,m,total,nxt[4][2]={{0,-1},{-1,0},{1,0},{0,1}};
bool vis[20][20];
char d[20][21];

void dfs(int x,int y,int num)
{
    for(int i=0;i<4;i++)
    {
        x+=nxt[i][0];
        y+=nxt[i][1];

        if(x>=0 && x<n && y>=0 && y<m && !vis[x][y] && d[x][y]==num)
        {
            vis[x][y]=1;

            total++;

            dfs(x,y,num);
        }

        x-=nxt[i][0];
        y-=nxt[i][1];
    }
}

void tran(int x,int y,int num)
{
    for(int i=0;i<4;i++)
    {
        x+=nxt[i][0];
        y+=nxt[i][1];

        if(x>=0 && x<n && y>=0 && y<m && d[x][y]==num)
        {
            d[x][y]='0';

            tran(x,y,num);
        }

        x-=nxt[i][0];
        y-=nxt[i][1];
    }
}

int main()
{
    int i,j,k,mx,x,y,ans,remain,t;

    while(~scanf("%d%d",&n,&m))
    {
        for(i=0;i<n;i++) scanf("%s",d[i]);

        ans=0;

        remain=n*m;

        while(remain)
        {
            mx=0;

            for(i=0;i<n;i++) for(j=0;j<m;j++) vis[i][j]=0;

            for(i=0;i<n;i++)
            {
                for(j=0;j<m;j++)
                {
                    if(d[i][j]>'0' && !vis[i][j])
                    {
                        vis[i][j]=1;

                        total=1;

                        dfs(i,j,d[i][j]);

                        if(total>mx)
                        {
                            mx=total;

                            x=i;
                            y=j;
                        }
                    }
                }
            }

            remain-=mx;

            ans+=mx*(mx-1);

            tran(x,y,d[x][y]);

            d[x][y]='0';

            for(i=n-1;i>=0;i--)//向下移动
            {
                for(j=0;j<m;j++)
                {
                    if(d[i][j]=='0')
                    {
                        for(k=i-1;k>=0;k--)
                        {
                            if(d[k][j]>'0')
                            {
                                d[i][j]=d[k][j];
                                d[k][j]='0';

                                break;
                            }
                        }
                    }
                }
            }

            t=m-1;
            while(t--)//向左移动,注意连续两列都为空的情况
            {
                for(j=0;j<m-1;j++)
                {
                    for(i=0;i<n;i++) if(d[i][j]>'0') break;

                    if(i==n)
                    {
                        for(i=0;i<n;i++)
                        {
                            d[i][j]=d[i][j+1];

                            d[i][j+1]='0';
                        }
                    }
                }
            }
        }

        printf("%d\n",ans);
    }
}

HDU-2258-Continuous Same Game (1)(DFS),布布扣,bubuko.com

时间: 2024-11-10 14:06:11

HDU-2258-Continuous Same Game (1)(DFS)的相关文章

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

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

HDU 1016 Prime Ring Problem --- 经典DFS

思路:第一个数填1,以后每个数判断该数和前一个数想加是否为素数,是则填,然后标记,近一步递归求解. 然后记得回溯,继续判断下一个和前一个数之和为素数的数. /* HDU 1016 Prime Ring Problem --- 经典DFS */ #include <cstdio> #include <cstring> int n; bool primer[45], visit[25]; //primer打素数表,visit标记是否访问 int a[25]; //数组a存储放置的数 /

hdu 1241 Oil Deposits (一次dfs搞定有某有)

1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 char map[105][105]; 7 8 int dir[8][2]={0, 1, 1, 0, -1, 0, 0, -1, 1, 1, 1, -1, -1, 1, -1, -1}; 9 int n, m; 10 int ans; 11 12

hdu 1016 Prime Ring Problem (简单DFS)

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25700    Accepted Submission(s): 11453 Problem Description A ring is compose of n circles as shown in diagram. Put natural numb

HDU 1010 Tempter of the Bone dfs+剪枝

给你一个迷宫一个起点和一个终点,问你能否走T步刚好到达终点,不能重复走,并且只有4个方向 显然这是一个dfs,虽然N最大只有7,但是裸的dfs复杂度还是太高了,因此要进行一些剪枝 1.如果T比图上所有的可走点还要大,肯定是不可行的.这个可以避免dfs整张图. 2.奇偶剪枝,有性质当前点(x,y)到目标点(tx,ty)的所有路径的长度的奇偶性一定和|x-tx|+|y-ty|一样. #include <cstdio> #include <iostream> #include <c

hdu 1016 Prime Ring Problem (dfs)

一切见注释. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; bool vis[22]; int n; int ans[22]; int top; bool isprime(int x)//判断素数 { for(int i=2;i<x;i++) if(x%i==0)return false; return

hdu 1518 Square(深搜dfs)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1518 --------------------------------------------------------------------------------------------------------------------------------------------

HDU 2553 N皇后问题 (搜索DFS)

N皇后问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7743    Accepted Submission(s): 3481 Problem Description 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在与棋盘边框成45角的斜线上. 你的任务是,对于给定的N,求

HDU 2209 翻纸牌游戏(dfs)

翻纸牌游戏 Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2180    Accepted Submission(s): 787 Problem Description 有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌.但是麻烦的是,每当你翻一张