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 ...
随机推荐
- XCode - vmware虚拟机安装XCode进行iPhone真机调试
1.vmware安装黑苹果,然后在appStore安装XCode http://blog.csdn.net/forgot2015/article/details/51104329 2.真机调试证书等申 ...
- Linux零碎知识
ln -s用法: 创建软连接,命令如下: ln -s / /home/good/linkname ln的链接分软链接和硬链接两种: .软链接就是:“ln –s 源文件 目标文件”,只会在选定的位置上生 ...
- javascript——后台传值map类型转换成json对象
前端需要对后端传过来的值进行解析之后再展示,而后端传过来的值可能是各种类型的,一般情况下要么和后端沟通下让他直接传给我们需要的类型,这个,我一般直接自己转,这次后端传回来一个map类型的对象,我转来转 ...
- 微信小程序redirect 到tab不刷新
// 更新2018/11/20:现在小程序的页面栈长度为10 更正 2018/11/20: 经过一段时间的实践,我发现以前方法存在很多问题,比如 getCurrentPages 方法并不在官方的 AP ...
- 消息队列 MQ 入门理解
功能特性: 应用场景: 消息队列 MQ 可应用于如下几个场景: 分布式事务 在传统的事务处理中,多个系统之间的交互耦合到一个事务中,响应时间长,影响系统可用性.引入分布式事务消息,交易系统和消息队列之 ...
- 企业IM (或业务系统)web api的json格式设计思考(原创)
在企业IM开发中,经常用到和业务系统的数据交换,在中国企业最常见的比如组织架构变更,一般在客户端加密保存了组织架构树(便于快速的查询和树展示),当HR或OA或AD域这些管控企业组织架构的数据发生改变, ...
- js插件实现点击复制内容到粘贴板,兼容IE8
先来看下本次需要导入的文件: 第一个是jquery.js,这个不多说: 第二个是jquery.zclip.js,第三个是zeroClipboard.swf ,这两个文件的下载链接:http://www ...
- Java之集合(二十二)PriorityBlockingQueue
转载请注明源出处:http://www.cnblogs.com/lighten/p/7510799.html 1.前言 本章介绍阻塞队列PriorityBlockingQueue.这是一个无界有序的阻 ...
- Sington单例模式(创建型模式)
一.使用Sington单例模式的动机(Motivation) 在软件系统中,经常有一些特殊的类,必须保证它们只有一个实例,才能保证它的逻辑正确性.以及良好的效率. 大多数类用的是常规的构造器,所以往往 ...
- Android之Activity界面跳转--生命周期方法调用顺序
这本是一个很基础的问题,很惭愧,很久没研究这一块了,已经忘得差不多了.前段时间面试,有面试官问过这个问题.虽然觉得没必要记,要用的时候写个Demo,打个Log就清楚了.但是今天顺手写了个Demo,也就 ...