GemAnd Prince

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 872    Accepted Submission(s): 299

Problem Description
Nowadays princess Claire wants one more guard and posts the ads throughout the kingdom. For her unparalleled beauty, generality, goodness and other virtues, many people gather at the capital and apply for the position. Because princess Claire is very clever, she doesn't want a fool to be her guard. As Claire is clever, she invents a game to test the applicants. The game is described as follows.
The game begins with a rectangular board of n rows and m columns, containing n*m grids. Each grid is filled with a gem and each gem is covered by one color, denoted by a number.(as the following shows).



If a gem has the same color with another one, and shares the same corner or the same border with it, the two are considered to be adjacent. Two adjacent gems are said to be connective. And we define that if A and B are connective, B and C are connective, then A and C are connective, namely the adjacency is transitive. Each time we can choose a gem and pick up all of the gems connected to it, including itself, and get a score equal to the square of the number of the gems we pick this time(but to make the game more challenging, the number of gems to be picked each time must be equal or larger than three).Another rule is that if one gem is picked, all the gems above it(if there is any)fall down to fill its grid,and if there is one column containing no gems at all, all the columns at its right(also if there is any) move left to fill the column. These rules can be shown as follows.

As the picture [a] above,all the gems that has color 1 are connective. After we choose one of them to be picked, all the gems that are connected to it must also be picked together, as the picture [b] shows (here we use 0 to denote the holes generated by the absence of gems).
Then the rest gems fall, as shown in picture [c]. Then the rest gems move left, as shown in picture [d]. Because we picked six gems at this time, our score increases 6*6=36.And furthermore, because we cannot find another gem, which has at least three gems connected to it(including itself),to be picked, the game comes to an end.
Each applicant will face such a board and the one who gets the highest score will have the honor to serve princess Claire. 
Aswmtjdsj also wants to serve for princess Claire. But he realizes that competing with so many people, even among whom there are powerful ACMers, apparently there is little chance to succeed. With the strong desire to be the lucky dog, Aswmtjdsj asks you for help. Can you help make his dream come true?

 
Input
There are no more than 15 test cases, separated by a blank line, end with EOF. Each case has n+1 lines, the first line of a case has three integers n, m, k (1<=n, m<=8, 1<=k<=6). Each of the next n lines contains m integers. The integer at (i+1)th line and jth column denotes the color of the gem at the grid (i, j), where the grid(1, 1) denotes the top left one, while the grid(n, m) is the lower right one. The integer in the grid is among [1, k].
 
Output
For each case you should output the highest score you can get in one single line.
 
Sample Input
3 3 3
1 1 3
1 2 1
1 1 2
 
5 4 3
2 2 3 3 1
1 3 3
3 2
2 2
3 1 1
1
3 1 2 2
 
Sample Output
36
103
 
Source
 
题意:做了几个这样的类型题目。都是根据一个游戏,模拟消除东西,
   题目告诉了你,最多有6种颜色,有什么用?我就想到了一个优化,因为题目要求至少3个才能消除。
   假如我的6种颜色的个数,都小于3,那就不要做了。但是这样的剪枝是很有限的,由于开始代码的搓,
        把标记的0也当成颜色统计,所以起初完全没有达到优化效果。
        其实还有一个优化的地方,就是统计当前状态下所可能达到的最大价值+已有的价值 和 变量MAX(用来保存最大价值的变量)
    进行比较。
 
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
int n,m,ki;
int a[][],MAX;
int color[];
int qq[][];
int to[][]={{,},{,},{-,},{,-},{,},{-,-},{,-},{-,}};
struct node{
int x,y;
};
queue<node>Q;
int getsum()
{
int i,j,ans=;
memset(color,,sizeof(color));
for(i=;i<n;i++)
for(j=;j<m;j++)
if(a[i][j]) color[a[i][j]]++; for(i=;i<=ki;i++)
ans=ans+color[i]*color[i];
return ans;
}
void change()
{
int i,j,k;
memset(qq,,sizeof(qq));
for(i=;i<m;i++){
for(j=n-,k=n-;j>=;j--)
if(a[j][i]!=) qq[k--][i]=a[j][i];
}
memset(a,,sizeof(a));
for(i=,k=;i<m;i++){
for(j=;j<n;j++) if(qq[j][i]!=)break;
if(j==n)continue;
for(j=n-;j>=;j--)
a[j][k]=qq[j][i];
k++;
}
}
int bfs(int x,int y,int num,bool (*hash)[])
{
int i,gs=;
node t,cur;
while(!Q.empty()){
Q.pop();
}
t.x=x;
t.y=y;
Q.push(t);
while(!Q.empty())
{
cur=Q.front();
Q.pop();
for(i=;i<;i++){
t=cur;
t.x=t.x+to[i][];
t.y=t.y+to[i][];
if(t.x>=&&t.x<n && t.y>=&&t.y<m){
if(!hash[t.x][t.y]&&a[t.x][t.y]==num)
{
gs++;
a[t.x][t.y]=;
hash[t.x][t.y]=true;
Q.push(t);
}
}
}
}
return gs;
}
void dfs(int now){ if(getsum()+now<=MAX) return;//第一个剪枝
if(MAX<now) MAX=now;
int i,j,num,lhxl=-;
int g[][];
int color[];
bool hash[][];
memset(hash,false,sizeof(hash));
memcpy(g,a,sizeof(g));
memset(color,,sizeof(color));
for(i=;i<n;i++)
for(j=;j<m;j++)
if(a[i][j]) color[a[i][j]]++;
for(i=;i<=ki;i++) if(color[i]>lhxl) lhxl=color[i];
if(lhxl<) return;//two
for(i=;i<n;i++)
for(j=;j<m;j++)
{
if(!hash[i][j] && g[i][j]!=)
{
memcpy(a,g,sizeof(a));
num = bfs(i,j,g[i][j],hash);
color[g[i][j]]-=num;
if(num<)continue;//three
change();
dfs(now+num*num);
}
}
}
int main()
{
int i,j;
while(scanf("%d %d %d",&n,&m,&ki)>){
for(i=;i<n;i++)
for(j=;j<m;j++)
scanf("%d",&a[i][j]);
MAX=-;
dfs();
printf("%d\n",MAX);
}
return ;
}

hdu 4090的更多相关文章

  1. hdu 4090 GemAnd Prince

    题目大意: 别人说是消消看,至于你玩没玩过.反正我是没玩过的. 就是选择一个钻石,可以消除与它相连的所有钻石.并获得 消除数量*消除数量  的分 思路: 直接暴搜,然后用一个cnt数组表示每一种钻石剩 ...

  2. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)

    Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 256000/1280 ...

  3. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  4. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  5. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  6. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

  8. HDU 1796How many integers can you find(容斥原理)

    How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d ...

  9. hdu 4481 Time travel(高斯求期望)(转)

    (转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...

随机推荐

  1. node 无脑生成小程序二维码图

    RT 新建createwxaqrcode.js: const request = require('request') const fs = require('fs') // eg:生成购物车列表圆形 ...

  2. WebDriverAPI(10)

    操作Frame页面元素 测试网址代码 frameset.html: <html> <head> <title>frameset页面</title> &l ...

  3. AngularJS常用Directives实例

    在第一篇 认识AngularJS 中,我们已经基本了解了AngularJS,对Directive也有了一定了解,本章我们将继续介绍Directive,对其有一个更深入的了解和掌握. 常用的Direct ...

  4. Maven与Hudson集成

    Hudson是一款优秀的持续集成产品,本文阐述Maven于Hudson的集成   Hudson的下载和安装 Hudson有两种安装模式,1:自运行(Hudson内建netty容器),2:放到如tomc ...

  5. (转)python字符串函数

    原文:https://www.cnblogs.com/emanlee/p/3616755.html https://blog.csdn.net/luoyhang003/article/details/ ...

  6. 我与GitHub的第一次——自制音乐文件修改器

    背景: 随机播放,所有的音乐播放器里面现在几乎都有这个功能吧.但是有没有发现,自己的播放器在选择随机播放的时候,经常会听到重复顺序的歌曲呢?反正我是有这样的感觉,无耐自己平时下的歌曲都是“歌手名—歌曲 ...

  7. css 中 stick footer 布局实现

    做项目中,我们在写弹框的时候,不管弹框的内容多或者少,可能需要一些内容需要固定在框底部,比如关闭按钮.stick footer 就是让 footer 元素固定在底部 当内容不足满屏时,footer 紧 ...

  8. Android4.0以上版本比较靠谱的获取手机屏幕分辨率的方法

    /** * 获取虚拟按键栏高度 * * @param context * @return */ public static int getNavigationBarHeight(Context con ...

  9. FoxitReader软件下载并安装(图文详解)

    不多说,直接上干货! FoxitReader官方网址:https://www.foxitsoftware.com/downloads/ 结束 欢迎大家,加入我的微信公众号:大数据躺过的坑        ...

  10. Check类中的incl、union,excl,diff,intersect

    定义一些类,这些类之间有父子关系,如下: class Father{} class Son1 extends Father{} class Son2 extends Father{} class To ...