hdu 4090--GemAnd Prince(搜索)
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?
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
//#include <windows.h>
using namespace std;
struct Node
{
int a[][];
};
int n,m,k;
int dx[]={,,,,,-,-,-};
int dy[]={,,-,,-,,,-};
int ans; /**void print(Node s)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
cout<<s.a[i][j]<<" ";
cout<<endl;
}
}*/
int get(Node s)
{
int r[];
for(int i=;i<=k;i++) r[i]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
r[s.a[i][j]]++;
}
int ans=;
for(int i=;i<=k;i++) ans+=r[i]*r[i];
return ans;
}
int dfs(Node &t,int x,int y,int d,Node &p)
{
int num=;
t.a[x][y]=;
p.a[x][y]=;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(nx<||nx>n||ny<||ny>m||t.a[nx][ny]!=d) continue;
num+=dfs(t,nx,ny,d,p);
}
return num;
}
void change1(Node &t)
{
for(int i=;i<=m;i++)
{
int pos=n;
for(int j=n;j>=;j--)
{
if(t.a[j][i]==) continue;
t.a[pos][i]=t.a[j][i];
if(pos!=j) t.a[j][i]=;
pos--;
}
}
}
void change2(Node &t)
{
int pos=;
for(int i=;i<=m;i++)
{
int f=;
for(int j=;j<=n;j++)
if(t.a[j][i]!=) { f=; break; }
if(f)
{
for(int j=;j<=n;j++)
{
t.a[j][pos]=t.a[j][i];
if(pos!=i) t.a[j][i]=;
}
pos++;
}
}
}
void solve(Node &s,int sum)
{
if(sum+get(s)<=ans) return ;
Node t=s;
Node p=s;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(t.a[i][j]==||p.a[i][j]==) continue;
int tmp=dfs(t,i,j,t.a[i][j],p);
if(tmp>=)
{
change1(t);
change2(t);
//Sleep(12000);
ans=max(ans,sum+tmp*tmp);
solve(t,sum+tmp*tmp);
}
t=s;
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
Node s;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
scanf("%d",&s.a[i][j]);
ans=;
solve(s,);
printf("%d\n",ans);
}
return ;
}
/**
3 3 5
0 0 3
0 2 0
0 0 2
*/
hdu 4090--GemAnd Prince(搜索)的更多相关文章
- hdu 4090 GemAnd Prince
题目大意: 别人说是消消看,至于你玩没玩过.反正我是没玩过的. 就是选择一个钻石,可以消除与它相连的所有钻石.并获得 消除数量*消除数量 的分 思路: 直接暴搜,然后用一个cnt数组表示每一种钻石剩 ...
- hdu 4090
GemAnd Prince Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 4616 Game (搜索)、(树形dp)
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4616 这道题目数据可能比较弱,搜索都可以AC,但是不敢写,哎…… 搜索AC代码: #include & ...
- [HDU 2102] A计划(搜索题,典型dfs or bfs)
A计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 4722(记忆化搜索)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...
- hdu 5335 Walk Out (搜索)
题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...
- HDU 1896 Stones --优先队列+搜索
一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...
- HDU 1978 记忆化搜索(dfs+dp)
Y - How many ways Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4597 记忆化搜索
² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...
- hdu 1560 DNA sequence(搜索)
http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others) ...
随机推荐
- jquery 的 each 方法中 return 的坑
jquery 的 each 方法中 return 的坑 Chapter 0 在项目中使用 jquery 的 each 方法时想在 each 的循环中返回一个布尔类型的值于是掉进一个坑中... Chap ...
- Python学习笔记5
1.关于global声明变量的错误例子 I ran across this warning: #!/usr/bin/env python2.3 VAR = 'xxx' if __name__ == ' ...
- [2014-09-21]如何在 Asp.net Mvc 开发过程中更好的使用Enum
场景描述 在web开发过程中,有时候需要根据Enum类型生成下拉菜单: 有时候在输出枚举类型的时候,又希望输出对应的更具描述性的字符串. 喜欢直接用中文的请无视本文 不多说,直接看代码. 以下代码借鉴 ...
- [2012-06-21]结合find的awk
源起:http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=3754423&page=1&extra=#pid221729 ...
- 九天学会Java,第五天,函数定义函数调用
变量和数据类型,赋值和输出 算术运算 选择结构 循环结构 函数定义,函数调用 变量作用域 栈,程序运行的基石 面向对象 异常处理 语言提供的公用包 什么是函数,为什么有函数,大家可能有这样的疑问. 举 ...
- NOSQL EYE开源
简介 NOSQL数据库监控工具,目前实现了对Redis.MongoDB的监控功能. 功能列表 演示地址 http://106.14.181.95:7004 登录用户名.密码:admin/admin ...
- hdu2222 Keywords Search(AC自动机初步)
题目大意: 给出多个模式串和一个主串,求多少个模式串在主串中出现过. 传送门 这是一道AC自动机的模板题. 在学习AC自动机之前,首先要学习WA自动机.TLE自动机和MLE自动机(雾 AC自动机是一种 ...
- Varnsih调用多台后端主机
author:JevonWei 版权声明:原创作品 Varnsih调用多个后端主机 环境 Varnish 192.168.198.139 图片服务端 192.168.198.120 程序服务端 192 ...
- C#构造函数、操作符重载以及自定义类型转换
构造器 构造器(构造函数)是将类型的实例初始化的特殊方法.构造器可分为实例构造器和类型构造器,本节将详细介绍有关内容. 实例构造器 顾名思义,实例构造器的作用就是对类型的实例进行初始化.如果类没有显示 ...
- 201521123050 《Java程序设计》第8周学习总结
1. 本周学习总结 2. 书面作业 1.List中指定元素的删除(题目4-1) 1.1 实验总结 在删除元素时,要注意后续元素位置的前移 2.统计文字中的单词数量并按出现次数排序(题目5-3) 2.1 ...