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

URAL 1715. Another Ball Killer (大模拟)

时间:2015-07-29 21:26:35      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:ural 1715   another ball killer   大模拟   

1715. Another Ball Killer

Time limit: 2.0 second
Memory limit: 64 MB
Contestants often wonder what jury members do during a contest. Someone thinks that they spend all the contest fixing bugs in tests. Others say that the jury members watch the contest excitedly and even bet on who will win. However, in reality, the jury members prefer to play computer games, giving a complete control of the contest to heartless machines.
Another Ball Killer is one of the favorite games of the jury. Its rules are very simple:
  1. The game is played by one person on a rectangular field of size n × m. At the initial moment, each cell of the field contains a ball of one of five colors: blue, green, red, white, or yellow.
  2. At each move, the player chooses some figure (a connected group of two or more balls of the same color; balls are called connected if their cells have a common side) and removes it from the field. After that the balls that were above the removed balls fall down. If a column without the balls appears, then all the columns on its right are shifted to the left. 
    The image below shows how the field changes after the removal of the largest figure. 
    The player is awarded k × (k ? 1) points for his move, where k is the size of the removed figure, i.e. the number of balls in it.
  3. The game is finished when there are no figures left on the field. The goal is to get as many points as possible by the end of the game.
技术分享
Lazy jury members play Another Ball Killer using the following algorithm:
01  Choose the color of one of the balls in the field as the main color.
02  While there is at least one figure:
03      While there is at least one figure of a color different from the main color:
04          Remove the largest figure of a color different from the main color.
05      If there is a figure of the main color:
06          Remove the largest figure of the main color.
If there are several ways to remove the figure in lines 04 and 06, one should choose the largest figure containing the bottommost ball (if there are several such figures, then one should choose among them the figure that contains the leftmost of such balls).
Chairman is the laziest person in the jury. He doesn‘t even think about which color he should choose as the main one. By pressing one key, he launches a program that calculates for every color present in the field the number of points that will be awarded if this color is chosen as the main one. Your task is to write such a program.

Input

The first line contains the dimensions of the field n and m (1 ≤ nm ≤ 50). Each of the followingn lines contains m letters denoting the color of the ball in the corresponding cell of the field (B for blue, G for green, R for red, W for white, and Y for yellow). The rows of the playing field are given in the order from the top row to the bottom row.

Output

Output one line for each color present in the field: first output the letter denoting the color, then a colon, a space, and the number of points the chairman of the jury will get if he chooses this color as the main one. The colors must be considered in the following order: blue, green, red, white, yellow.

Sample

input output
3 6
WWWGBG
WBGGGB
GGGGBB
B: 74
G: 92
W: 74
Problem Author: folklore (prepared by Eugene Krokhalev)
Problem Source: NEERC 2009, Eastern subregional contest


题意:n*m的格子上有至多5种颜色的格子,同一颜色的 k (k>=2)个格子连成一块可以相消,得分k*(k-1),每次规定一主颜色,每次先消最大的块,若存在多个相同大小的块,先消靠底部的,靠左边的;先消与主颜色不同的块,再消主颜色的块。每次消完一个块整体都向下挪,向左挪,如题图。输出每种主颜色下的得分。

思路:蛋疼大模拟,敲了两个小时,脑袋都要炸了,幸好1A技术分享感激涕零,直接上代码,自己的代码写完就看不懂了=-=

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

#define INF 0x3f3f3f3f
#define mod 1000000009

const int maxn = 55;

int a[26];
int n,m,cnt;
char mp[maxn][maxn],MP[maxn][maxn];
bool vis[maxn][maxn];
bool ok1[maxn][maxn],ok2[maxn][maxn];
int dir[4][2]={1,0,0,1,-1,0,0,-1};

void show()
{
    for (int i=0;i<n;i++)
        printf("%s\n",mp[i]);
}

bool isok(int x,int y)
{
    if (x>=0&&x<n&&y>=0&&y<m) return true;
    return false;
}

int dis_up(int x,int y)
{
    int ans=0;
    while (isok(x,y)&&mp[x][y]=='.')
    {
        ans++;
        x--;
    }
    return ans;
}

void Change(int x,int y)
{
     for(int i=x;i>=0;i--)
        if(i==0) mp[i][y]='.';
     else
        mp[i][y]=mp[i-1][y];
}

void Move(int y)
{
    int i,j;
    for(i=0;i<n;i++)
        if(mp[i][y]=='.')
           Change(i,y);
}

void right_to_left(int y)
{
    for (int i=0;i<n;i++)
    {
        for (int j=y;j<m-1;j++)
        {
            mp[i][j]=mp[i][j+1];
        }
        mp[i][m-1]='.';
    }
}

void dfs(int x,int y,int letter)
{
    ok1[x][y]=true;
    vis[x][y]=true;
    cnt++;
    for (int i=0;i<4;i++)
    {
        int dx=dir[i][0]+x;
        int dy=dir[i][1]+y;
        if (isok(dx,dy)&&!vis[dx][dy]&&mp[dx][dy]==letter)
            dfs(dx,dy,letter);
    }
    return ;
}

int ffd(int color,int wq)
{
    memset(vis,false,sizeof(vis));
    int number=0;
    for (int i=n-1;i>=0;i--)
    {
        for (int j=0;j<m;j++)
        {
            if (mp[i][j]=='.') continue;
            if (vis[i][j]) continue;
            if (wq==1&&mp[i][j]=='A'+color) continue;
            else if (wq==0&&mp[i][j]!='A'+color) continue;
            cnt=0;
            memset(ok1,false,sizeof(ok1));
            dfs(i,j,mp[i][j]);
            if (cnt<2) continue;
            if (cnt>number)
            {
                number=cnt;
                memcpy(ok2,ok1,sizeof(ok1));
            }
        }
    }
    return number;
}

void change()
{
    for (int i=0;i<n;i++)
    {
        for (int j=0;j<m;j++)
            if (ok2[i][j])
            mp[i][j]='.';
    }
//    printf("******\n");
//    show();
//    printf("******\n\n");
    for (int j=0;j<m;j++)
    {
        Move(j);
    }
    for (int j=m-1;j>=0;j--)
    {
        int x=dis_up(n-1,j);
        if (x==n)
            right_to_left(j);
    }

}

int get_point(int color)
{
    int sum=0;
    while (1)
    {
        int ss;
        while ((ss=ffd(color,1))>=2)
        {
//            printf("ss=%d\n",ss);
//            cas++;
            sum+=(ss-1)*ss;
            change();
//            show();
//            printf("==================\n");
        }
        if ((ss=ffd(color,0))>=2)
        {
            sum+=(ss-1)*ss;
            change();
//            show();
//            printf("++++++++++++++++++++\n");
        }
        if (ffd(color,1)<2&&ffd(color,0)<2) break;
    }
    return sum;
}

void solve()
{
    for (int i=0;i<26;i++)
    {
        if (a[i])
        {
            memcpy(mp,MP,sizeof(MP));
            int sum=get_point(i);
            printf("%c: %d\n",'A'+i,sum);
        }
    }
}

int main()
{
    int i,j;
    while (~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        for (i=0;i<n;i++)
        {
            scanf("%s",mp[i]);
            for (j=0;j<m;j++)
                a[mp[i][j]-'A']=1;
        }
        memcpy(MP,mp,sizeof(mp));
        solve();
    }
    return 0;
}



版权声明:本文为博主原创文章,未经博主允许不得转载。

URAL 1715. Another Ball Killer (大模拟)

标签:ural 1715   another ball killer   大模拟   

原文地址:http://blog.csdn.net/u014422052/article/details/47132407

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