UVALive 4425 Another Brick in the Wall 暴力

C - Another Brick in the Wall
Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF

After years as a brick-layer, you've been called upon to analyze the instability of brick walls. The instability of a wall can be approximated by the maximum damage to a wall in case of taking one brick out. A brick will fall if all bricks that are directly underneath it are removed. Note that if the space underneath a brick is partially empty, it does not fall. You are given the description of all bricks in a wall, and must determine the instability of the wall as described in the following sections.

Input

There are multiple test cases in the input. Each test case consists of a single line, ``M N " (1M, N100) where M and N indicate the height and width (in units), respectively, of the input wall.


Each of the next M lines is a string of N digits which specifies a row in the wall. Each brick in a row is represented by a substring of the row (like s ) such that every digit in s is the same, which is equal to the length of s too. For example, 333 and 22 are two bricks of length 3 and 2 respectively, but 111 specifies three bricks of length one. A 0 in a row means there is no brick in that place of wall. Note that the height of each brick is one. The input terminates with a line containing ` 0 0'. You may assume that the input is correct. This means:

 

  1. There is no brick such that the length of the brick does not conform to the digits in the brick (like 222 in the row 12221).
  2. No brick can fall initially.

Output

For each test case, write a single line containing maximum sum of the bricks' lengths that will fall if we take one brick out (including that brick).

Sample Input

4 5 

33322 

22333 

33322 

22333 

4 6 

122333 

444422 

111111 

333333 

3 3 

022 

220 

111 

0 0

Sample Output

5 

8 

4
#include <cstdio>

#include <cmath>

#include <cstring>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <set>

#include <vector>

#include <sstream>

#include <queue>

#include <typeinfo>

#include <fstream>

typedef long long ll;

using namespace std;

//freopen("D.in","r",stdin);

//freopen("D.out","w",stdout);

#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)

#define maxn 100001

const int inf=0x7fffffff;   //无限大

int dp[110][110];

int g[110][110];

int q[110][110];

int main()

{

    int n,m;

    string s[110];

    while(cin>>n>>m)

    {

        if(n==0&&m==0)

            break;

        memset(dp,0,sizeof(dp));

        memset(q,0,sizeof(q));

        memset(g,0,sizeof(g));

        for(int i=0;i<n;i++)

        {

            cin>>s[i];

            for(int j=0;j<m;j++)

            {

                if(s[i][j]=='0')

                {

                    g[i][j]=-1;

                }

                else

                {

                    g[i][j]=(int)(s[i][j]-'0');

                    j=j+(int)(s[i][j]-'0')-1;

                }

            }

        }





        for(int i=0;i<n;i++)

        {

            for(int j=0;j<m;j++)

            {

                if(i==0)

                {

                    dp[i][j]=(int)(s[i][j]-'0');

                }

                else

                {

                    if(g[i][j]==-1)

                        dp[i][j]=0;

                    else if(g[i][j]==0)

                        dp[i][j]=dp[i][j-1];

                    else

                    {

                        int ii=i-1;

                        int sum=0;

                        int op=g[i][j];

                        dp[i][j]+=g[i][j];



                        for(int k=0;k<m;k++)

                        {

                            if(j<=k&&k<j+op)

                                q[i][k]=-1;

                            else

                                q[i][k]=g[i][k];

                        }





                        while(1)

                        {

                            sum=0;



                            for(int k=0;k<m;k++)

                            {

                                if(q[ii][k]==-1)

                                    continue;

                                q[ii][k]=g[ii][k];

                                for(int jj=0;jj<g[ii][k];jj++)

                                {

                                    if(q[ii+1][k+jj]!=-1)

                                        break;

                                    if(jj==g[ii][k]-1)

                                    {

                                        for(int mm=0;mm<g[ii][k];mm++)

                                        {

                                            q[ii][k+mm]=-1;

                                            sum++;

                                        }

                                    }

                                }

                            }

                            dp[i][j]+=sum;

                            if(sum==0)

                                break;

                            ii--;

                            if(ii<0)

                                break;

                        }

                        memset(q,0,sizeof(q));

                    }

                }

            }

        }

        int ans=0;

        for(int i=0;i<n;i++)

        {

            for(int j=0;j<m;j++)

            {

                ans=max(ans,dp[i][j]);

            }

        }

        cout<<ans<<endl;

    }

    return 0;

}

 


你可能感兴趣的:(live)