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) ...
随机推荐
- [2013-03-14]使用wiki维护产品文档
word文档作为产品文档的问题: word文档本身的设计是为了打印: word文档的编辑较为繁琐: 作为产品文档的word文档往往长达百页以上,难以维护,且容易分散注意力,不利于查阅: 没有一个简单易 ...
- Webpack+Vue+ES6 前端组件化开发mobile-multi-page应用实战总结
本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.写在前面 项目上线有一段时间了,一个基于webpack+vue+ES6的手机端多页面应用 ...
- java对象与json对象之间的转换
jar包:import net.sf.json.JSONObject; 1.解析json字符串 将json字符串转换为json对象,然后再解析json对象:.JSONObjectjsonObject ...
- javasript校验字符串【正则和其他函数】
/**javasript校验输入框值只能为数字中文英文和下划线**/function isRegex(s){ var reg=/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/; if (! ...
- 启动tomcat错误:Address already in use: JVM_Bind:8081
解决方法: 1.打开任务管理器,关闭掉javaw进程. 2.修改tomcat端口:tomcat/conf/server.xml
- NPIO 导出Execl
步骤1:导入NOIO.dll (我导入压缩包中的4.0)
- 团队作业8——第二次项目冲刺(Beta阶段)--5.25 5th day
团队作业8--第二次项目冲刺(Beta阶段)--5.25 fifth day Day five: 会议照片 项目进展 Beta冲刺的第四天,以下是今天具体任务安排: 队员 昨天已完成的任务 今日计划完 ...
- 结对作业1----基于flask框架的四则运算生成器
011.012结对作业 coding地址:https://coding.net/u/nikochan/p/2nd_SE/git 一.作业描述 由于上次作业我没有按时完成,而且庞伊凡同学编程能力超棒,所 ...
- 团队作业4——第一次项目冲刺(Alpha版本)7th day
一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 在计时模式下能够记录用户的用户名和成绩,没有弄登录功能, 将程序定义为单机的 未完成的卡片为登录功能和使用QQ登录. 四.困难 ...
- 201521123023《Java程序设计》第五周学习总结
1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过 ...