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 ...
随机推荐
- OVS 内核KEY值提取及匹配流表代码分析
原文链接:http://ry0117.com/2016/12/24/OVS内核KEY值提取及匹配流表代码分析/ 当开启OVS后,创建datapath类型为system的网桥并他添加相关接口,OVS网桥 ...
- 干货 | 自适应大邻域搜索(Adaptive Large Neighborhood Search)入门到精通超详细解析-概念篇
01 首先来区分几个概念 关于neighborhood serach,这里有好多种衍生和变种出来的胡里花俏的算法.大家在上网搜索的过程中可能看到什么Large Neighborhood Serach, ...
- 【OpenCV3】cvRound()、cvFloor()、cvCeil()函数详解
函数cvRound().cvFloor().cvCeil()都是按照一种舍入方式将浮点型数据转换为整型数据. cvRound():返回跟参数最接近的整数值,即四舍五入: cvFloor() :返回不 ...
- 剑指offer五十一之构建乘积数组
一.题目 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...
- Spring Security构建Rest服务-0900-rememberMe记住我
Spring security记住我基本原理: 登录的时候,请求发送给过滤器UsernamePasswordAuthenticationFilter,当该过滤器认证成功后,会调用RememberMeS ...
- [转]ASP.NET Core 十种方式扩展你的 Views
http://www.cnblogs.com/savorboard/p/aspnetcore-views.html
- static变量生命周期小研究
A页面:定义一个普通类,类包含一个静态变量a,值为:111111111111111111 B页面:修改a的值为:22222222222222 C页面:用来查看a的值 1.在vs2008中,先打开B,再 ...
- 【数组】Rotate Image
题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...
- android listview实现点击某个item后使其显示在屏幕顶端
在该listview的点击事件中加入一下代码即可 listView.setSelectionFromTop(position, 0);
- Office自动生成目录步骤(非常实用)(图文详解)
不多说,直接上干货! 结束 欢迎大家,加入我的微信公众号:大数据躺过的坑 人工智能躺过的坑 同时,大家可以关注我的个人博客: http://www.cnblogs.co ...