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) ...
随机推荐
- Java起源
Java历史发展和特点 作为一名合格的程序员,如果不了解一些关于Java语言的起源是有一些不太合适的.下面就介绍一下我所了解的Java起源. 1.Java名字的来源 Java是印度尼西亚爪哇岛的英文名 ...
- Redis单机版和集群版的安装和部署
1.单机版的安装 本次使用redis3.0版本.3.0版本主要增加了redis集群功能. 安装的前提条件: 需要安装gcc:yum install gcc-c++ 1.1 安装redis 1.下载re ...
- 线程高级篇-Lock锁和Condition条件
浅谈Synchronized: synchronized是Java的一个关键字,也就是Java语言内置的特性,如果一个代码块被synchronized修饰了,当一个线程获取了对应的锁,执行代码块时,其 ...
- 201521123036 《Java程序设计》第8周学习总结
本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 HashMap自定义排序 List<Map.Entry<Stri ...
- 201521123122 《java程序设计》第八周实验总结
201521123122 <java程序设计>第八周实验总结 1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 List中指定元素的删除(题目4- ...
- 201521123078《Java程序设计》第七周学习总结
1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boolean contains(Object o) { re ...
- Java: 类继承中 super关键字
super 关键字的作用有两个: 1)在子类中调用超类的构造器,完成实例域参数的初始化,调用构造器的语句只能作为另一个构造器(通常指的是子类构造器)的第一条语句出现, 2)在子类中调用超类的方法,如: ...
- python 输出颜色的与样式的方法
上次遇到这个问题就想写下来,其实当时我也不怎么会,老师说这个东西不需要理解,只需要死记硬背,写的多了就记住了,所以今天搜集了几篇文章,加上自己的理解,写下了这篇python 输出颜色的样式与方法的文章 ...
- java通过JDBC链接SQLServer2012【转载!!!超详细】
http://blog.csdn.net/stewen_001/article/details/19553173/
- 生成验证码JSP【复用代码】
该JSP可以生成验证码.以后用到的时候就方便了. <%@ page language="java" pageEncoding="UTF-8"%> & ...