hdu 4090
GemAnd Prince
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 872 Accepted Submission(s): 299
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?
3 2
3 1 1
3 1 2 2
#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的更多相关文章
- hdu 4090 GemAnd Prince
题目大意: 别人说是消消看,至于你玩没玩过.反正我是没玩过的. 就是选择一个钻石,可以消除与它相连的所有钻石.并获得 消除数量*消除数量 的分 思路: 直接暴搜,然后用一个cnt数组表示每一种钻石剩 ...
- HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)
Computer Virus on Planet Pandora Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 256000/1280 ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
随机推荐
- Mac OS 10.12 - 如何能够像在Windows一样切换中英文输入法和大小写键?
最开始,我切换中英文输入法和大小写键是按照下面博客做到的: http://www.cnblogs.com/sunylat/p/6415563.html 但是当我安装完毕搜狗输入法后,切换中英文输入法和 ...
- 简单的卷积神经网络(CNN)的搭建
卷积神经网络(Convolutional Neural Network, CNN)是一种前馈神经网络,它的人工神经元可以响应一部分覆盖范围内的周围单元,对于大型图像处理有出色表现.与普通神经网络非常相 ...
- 近期js
1 var value1 = 0, value2 = 0, value3 = 0; for ( var i = 1; i <= 3; i++) { var i2 = i; (function() ...
- 【bug】安卓浏览器键盘输入改变弹出层的定位
bug描述 在安卓浏览器中,有一个在页面底部的弹出层表单,样式如下: .popup { position: absolute; bottom: 0; } 当在这个弹出层输入内容,键盘自动弹出,弹出层的 ...
- 汽车检测SIFT+BOW+SVM
项目来源于 <opencv 3计算机视觉 python语言实现> 整个执行过程如下: 1)获取一个训练数据集. 2)创建BOW训练器并获得视觉词汇. 3)采用词汇训练SVM. 4)尝试对测 ...
- php if 的实现
简单分析下php中的分支背后的实现 <?php ){ echo "a"; }else{ echo "b"; } 1.语法分析 unticked_state ...
- 【learning】 单调队列与单调栈用法详解
1.单调栈 单调栈是指一个栈内部的元素具有严格单调性的一种数据结构,分为单调递增栈和单调递减栈. 其具有以下两个性质: 1,满足栈底到栈顶的元素具有严格单调性. 2,满足栈的先进后出特性,越靠近栈顶的 ...
- subscripts(下标)
/* subscripts(下标): 访问对象中数据的快捷方式 所谓下标脚本语法就是能够通过, 实例[索引值]来访问实例中的数据 类似于以前我们访问数字和字典, 其实Swift中的数组和字典就是一个结 ...
- linux下安装mysql-5.7.20
1.下载地址 https://downloads.mysql.com/archives/community/ 2.安装步骤 解压: groupadd mysql useradd -r -g mysql ...
- 剑指offer五十二之正则表达式匹配
一.题目 请实现一个函数用来匹配包括'.'和'*'的正则表达式.模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次). 在本题中,匹配是指字符串的所有字符匹配整个模式 ...