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. SFML从入门到放弃(3) 视角和碰撞检测

    SFML从入门到放弃(3) 视角和碰撞检测 视角 window.draw();所画出的对象是在世界坐标的绝对位置. 视角可以选定在窗口中显示世界坐标下的的哪一个区域. sf::View init_vi ...

  2. LCS2 - Longest Common Substring II(spoj1812)(sam(后缀自动机)+多串LCS)

    A string is finite sequence of characters over a non-empty finite set \(\sum\). In this problem, \(\ ...

  3. NPM install 中:-save 、 -save-dev 和 没有--save的情况

    原文地址:https://www.cnblogs.com/limitcode/p/7906447.html npm install moduleName 命令 . 安装模块到项目node_module ...

  4. EL表达式中的11个隐式对象

    EL表达式中定义了11个隐式对象,使用这些隐式对象可以很方便地读取到Cookie.HTTP请求消息头字段.请求参数.Web应用程序中的初始化参数的信息,EL表达式中的隐式对象具体如下: 隐式对象 作用 ...

  5. day 44 django 学习入门

    前情提要: 终于学到了Django  ...古川小姐姐好流b .....7天学完.....脑壳疼..为了出了这个小火箭.. 详细参考官网. https://www.django.cn/ 中文网站 一: ...

  6. 【LeetCode】390. 消除游戏

    题目 给定一个从1 到 n 排序的整数列表. 首先,从左到右,从第一个数字开始,每隔一个数字进行删除,直到列表的末尾. 第二步,在剩下的数字中,从右到左,从倒数第一个数字开始,每隔一个数字进行删除,直 ...

  7. 【GDKOI2017】 两个胖子萌萌哒 小学奥数题

    题目大意:给你一个$n\times m$的网格,你要在这个网格上画三角形. 三角形的顶点只能在网格的整点上,且至少有一条边平行于$x$或$y$轴,且三角形面积为整数.问你能画多少个不同的三角形. 两个 ...

  8. 【NOIP2013】货车运输 最大生成树+倍增

    题目大意:给你一张n个点m条边的图,有q次询问,每次让你找出一条从x至y的路径,使得路径上经过的边的最小值最大,输出这个最大的最小值. 显然,经过的路径必然在这张图的最大生成树上. 我们求出这个图的最 ...

  9. 如何正确的加载和执行 JavaScript 代码

    无论当前 JavaScript 代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JavaScript 执行过程耗时越久,浏览器等待响应用户输入的时间就越长.浏览器在下载和执行 ...

  10. MySQL索引(六)

    一.什么是索引 索引就像一本书的目录一样,如果在字段上建立索引,那么以索引为列的查询条件时可以加快查询的速度,这个就是MySQL优化的重要部分 二.创建主键索引 整个表的每一条记录的主键值在表内都是唯 ...