zoukankan      html  css  js  c++  java
  • Continuous Same Game (1) 简单的模拟dfs() 很好过

    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.
    ***************************************************************************************************************************
    ***************************************************************************************************************************
      1 #include<iostream>
      2 #include<string>
      3 #include<cstring>
      4 #include<cmath>
      5 #include<algorithm>
      6 #include<cstdio>
      7 using namespace std;
      8 
      9 char g[24][24] = {0};
     10 bool use[24][24] = {0};
     11 int dx, dy, n, m;
     12 int zx[] = {-1, 0, 1, 0}, zy[] = {0, 1, 0, -1};
     13 
     14 int find(int x, int y, char c)//找最大的区域
     15 {
     16  int sum;
     17  int k;
     18  use[x][y] = true;
     19  sum = 1;
     20  for (k = 0; k < 4; ++k)
     21  {
     22   if(g[x + zx[k]][y + zy[k]] == c && !use[x + zx[k]][y + zy[k]])
     23   {
     24    sum += find(x + zx[k], y + zy[k], c);
     25   }
     26  }
     27  return sum;
     28 }
     29 
     30 void del(int x, int y, char c)//删除最大的块
     31 {
     32  int k;
     33  g[x][y] = '0';
     34  for (k = 0; k < 4; ++k)
     35  {
     36   if(g[x + zx[k]][y + zy[k]] == c)
     37   {
     38    del(x + zx[k], y + zy[k], c);
     39   }
     40  }
     41 }
     42 void out();
     43 void move()//矩阵之间元素的移动
     44 {
     45  int i, j, k, last = m;;
     46 // out();
     47  int x1, x2;
     48  for (j = 1; j <= last; ++j)
     49  {
     50   for (x1 = 1; x1 <= n && g[x1][j] == '0'; ++x1);
     51   if (x1 > n)
     52   {
     53    for (k = j; k < last; ++k)
     54    {
     55     for (i = 1; i <= n; ++i)
     56     {
     57      g[i][k] = g[i][k + 1];
     58     }
     59    }
     60    for (i = 1; i <= n; ++i)
     61    {
     62     g[i][last] = '0';
     63    }
     64    last--;
     65    j--;
     66    continue;
     67   }
     68   x2 = x1 + 1;
     69   while(x2 <= n)
     70   {
     71    if (g[x2][j] == '0')
     72    {
     73     for (i = x2; i > x1; --i)
     74     {
     75      g[i][j] = g[i - 1][j];
     76     }
     77     g[x1][j] = '0';
     78     x1++;
     79    }
     80    ++x2;
     81   }
     82  }
     83 
     84 }
     85 void solve()
     86 {
     87  int i, j, ans = 0;
     88  int maxArea = 0;
     89  while(true)
     90  {
     91   memset(use, false, sizeof(use));
     92   maxArea = 0;
     93   for (i = 1; i <= n; ++i)
     94   {
     95    for (j = 1; j <= m; ++j)
     96    {
     97     if (!use[i][j] && g[i][j] != '0')
     98     {
     99      int t = find(i, j, g[i][j]);
    100      if (t > maxArea)
    101      {
    102       maxArea = t;
    103       dx = i;
    104       dy = j;
    105      }
    106     }
    107    }
    108   }
    109   if (maxArea <= 1)
    110    break;
    111   ans += maxArea * (maxArea - 1);
    112  // printf("%d
    ", maxArea * (maxArea - 1));
    113   del(dx, dy, g[dx][dy]);
    114   move();
    115  }
    116  printf("%d
    ", ans);
    117 }
    118 
    119 bool input()
    120 {
    121  if (scanf("%d%d", &n, &m) == EOF)
    122   return false;
    123  memset(g, '0', sizeof(g));
    124  int i;
    125  for (i = 1; i <= n; ++i)
    126   scanf("%s", g[i] + 1);
    127  return true;
    128 }
    129 
    130 int main()
    131 {
    132  while(input())
    133  {
    134   solve();
    135  }
    136  return 0;
    137 }
    View Code
  • 相关阅读:
    JavaScript+IndexedDB实现留言板:客户端存储数据
    怎么限制Google自动调整字体大小
    《互联网时代》告诉我的互联网简史(二)
    《互联网时代》告诉我的互联网简史(一)
    CSS换行:word-wrap、word-break和text-wrap区别
    php中的字符串和正则表达式
    php数组使用小结
    问题:关于一个贴友的js留言板的实现
    问题:关于坛友的一个定时重复显示和隐藏div的实现
    使用union 外加count
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3428264.html
Copyright © 2011-2022 走看看