码迷,mamicode.com
首页 > 其他好文 > 详细

(dfs) hdu 2258

时间:2015-03-20 23:41:12      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

  



(dfs) hdu 2258

标签:

原文地址:http://www.cnblogs.com/a972290869/p/4354824.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!