URAL 1715. Another ball Killer (big simulation)

Source: Internet
Author: User

1715. Another ball killertime 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 would win. However, in reality, the jury members prefer to play computer games, giving a all control of the contest to heartless Machines. Another ball Killer are one of the favorite games of the jury. Its rules is very simple:
  1. The game is played by one person on a rectangular field of size n x m. At the initial moment, each cell of the field contains a ball of one of the five colors:blue, green, red, white, or yellow.
  2. At each move, the player chooses some figure (a connected group of both or more balls of the same color; Balls is Called connected if their cells has a common side) and removes it from the field. After that, the balls that were above the removed balls fall. If a column without the balls appears, then all the columns in its right is shifted to the left.
    The image below shows how the field changes after the removal of the largest figure.
    The player is awarded K x (k . 1) points for he move, where K is the size of the removed figur E, i.e. the number of balls in it.
  3. The game was finished when there was 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 are at least one figure:03 while there are 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 is several ways to remove the figure in lines and, one should choose the largest figure containing the bot Tommost Ball (if there is several such figures, then one should choose among them the figure that contains the leftmost O F 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 would be awarded if this color is chosen as the main one. Your task is to write such a program. Inputthe first line contains the dimensions of the fieldNandm(1≤N,m≤50). Each of the followingNLines containsm 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 is given in the order from the top row to the bottom row. Outputoutput one line for each color present in the Field:first output the letter denoting the color and then a colon, a spa CE, and the number of points the chairman of the jury would get if he chooses this color as the main one. The colors must is considered in the following order:blue, green, red, white, yellow. Sample
input Output
3 6WWWGBGWBGGGBGGGGBB
b:74g:92w:74
problem Author:Folklore (prepared by Eugene Krokhalev)
problem Source:NEERC, Eastern Subregional contest


Test instructions: N*m lattice has up to 5 colors of the lattice, the same color of K (k>=2) a lattice can be eliminated, the score k* (k-1), each time a main color, the first to eliminate the largest block, if there are more than one block of the same size, first to eliminate the bottom, on the left; , and then the block of the main color is eliminated. Each time a block is removed, the whole is moved downward, to the left, such as the picture. Outputs the score under each of the main colors.

Idea: Egg pain big simulation, knocked for two hours, the head to blow up, fortunately 1 a grateful, directly on the code, their code to read the =-=

Code:

#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 1000000009const int maxn = 55;int a[2 6];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;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

URAL 1715. Another ball Killer (big simulation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.