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

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

时间: 2024-08-04 22:10:03

URAL 1715. Another Ball Killer (大模拟)的相关文章

AC日记——神奇的幻方 洛谷 P2615(大模拟)

题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,……,N*N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方: 首先将1写在第一行的中间. 之后,按如下方式从小到大依次填写每个数K(K=2,3,…,N*N): 1.若(K−1)在第一行但不在最后一列,则将K填在最后一行,(K−1)所在列的右一列: 2.若(K−1)在最后一列但不在第一行,则将K填在第一列,(K−1)所在行的上一行: 3.若(K−1)在第一行最后一列,则将K填在(K−1)

POJ 1835 大模拟

宇航员 #include<iostream> #include<cstdio> #include<string> #include<cstring> #define maxn 10010 using namespace std; int a[7],temp[7]; char str[10]; void solve(int str2[],int str3[]) { if(strcmp(str,"forward")==0)//方向不变 { s

大模拟祭

考试的前一天晚上我还在和$letong$说,我以后晚上再颓就去打模拟,然后就考了个大模拟 我比较$sb$,各种情况全分开了,没怎么压行,打了$1000$行整,$30$多$k$,妈妈再也不怕我打不出来猪国杀了 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 using namespace std; 5 int n,len,Std; 6 int b[5]; 7 char s[20],ss[20];

URAL 2002. Test Task(登陆模拟 map )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=2002 2002. Test Task Time limit: 0.5 second Memory limit: 64 MB It was an ordinary grim October morning. The sky was covered by heavy gray clouds. It was a little rainy. The rain drops fell on the win

2017&quot;百度之星&quot;程序设计大赛 - 复赛1001&amp;&amp;HDU 6144 Arithmetic of Bomb【java大模拟】

Arithmetic of Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 129    Accepted Submission(s): 94 Problem Description 众所周知,度度熊非常喜欢数字. 它最近在学习小学算术,第一次发现这个世界上居然存在两位数,三位数……甚至N位数! 但是这回的算术题可并不简单,由于

UVALive 4222 /HDU 2961 Dance 大模拟

Dance Problem Description For a dance to be proper in the Altered Culture of Machinema, it must abide by the following rules: 1. A dip can only appear 1 or 2 steps after a jiggle, or before a twirl, as in:* ...jiggle dip...* ...jiggle stomp dip...* .

UVA 1596 Bug Hunt (大模拟 栈)

题意: 输入并模拟执行一段程序,输出第一个bug所在的行. 每行程序有两种可能: 数组定义: 格式为arr[size]. 例如a[10]或者b[5],可用下标分别是0-9和0-4.定义之后所有元素均为未初始化状态. 赋值语句: 格式为arr[index]=value. 或者arr[index] = arr[arr[index]]. 例如a[0]=3或者a[a[0]]=a[1]. 赋值语句和数组定义可能会出现两种bug: 下标index越界: 使用未初始化的变量(index和value都可 能出现

URAL 1404. Easy to Hack! (模拟)

space=1&num=1404">1404. Easy to Hack! Time limit: 1.0 second Memory limit: 64 MB When Vito Maretti writes an important letter he encrypts it. His method is not very reliable but it's enough to make any detective understand nothing in that lett

大模拟

工具人的自觉 尽量在对的前提下加快速度吧 也稍微总结下容易粗心的地方,慢慢补 计蒜客T42401 ($Poker\ Game$,$2019ICPC$南京) 一些调试中发现的错误: 1. 把第一次翻开的community card数量当成$2$(应该为$3$)→ 进而影响第$3,4$人在flop-betting时的正确性 2. 一开始没注意到只剩一人时就胜出 → 需要在每一轮都判断 3. 没注意到发牌可以不全发完 4. 看错第$1$人在flop-betting中call的条件 5. Three o