题目链接

Problem Description
Nowadays princess Claire wants one more guard and posts the ads throughout the kingdom. For her unparalleled beauty, generality, goodness and other virtues, many people gather at the capital and apply for the position. Because princess Claire is very clever, she doesn't want a fool to be her guard. As Claire is clever, she invents a game to test the applicants. The game is described as follows.
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?

 
Input
There are no more than 15 test cases, separated by a blank line, end with EOF. Each case has n+1 lines, the first line of a case has three integers n, m, k (1<=n, m<=8, 1<=k<=6). Each of the next n lines contains m integers. The integer at (i+1)th line and jth column denotes the color of the gem at the grid (i, j), where the grid(1, 1) denotes the top left one, while the grid(n, m) is the lower right one. The integer in the grid is among [1, k].
 
Output
For each case you should output the highest score you can get in one single line.
 
Sample Input
3 3 3
1 1 3
1 2 1
1 1 2
 
5 4 3
2 2 3 3
1 1 3 3
3 2 2 2
3 1 1 1
3 1 2 2
 
Sample Output
36
103
 
题意:有一个n*m的格状棋盘,每个小格上有一个数字,现在有一个游戏规则:取棋盘上的一个方格,与方格相连的且数值相同的方格也会被选取,每次选取小方格获得的分数为获得的方格数的平方值(方格数>=3才能被选取,小于3不能选取该方格),取走方格后,上面的方格都会向下落填补空的方格,然后如果某一列全空,那么右边的方格会左移补充,游戏直至找不到相连的3个以上的方格为止。现在求能获得的最大得分?
 
思路:搜索,注意剪枝,当前已经获得的分数与剩下的方格能获得的最大得分之和小于ans,则return。
 
代码如下:
#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(搜索)的更多相关文章

  1. hdu 4090 GemAnd Prince

    题目大意: 别人说是消消看,至于你玩没玩过.反正我是没玩过的. 就是选择一个钻石,可以消除与它相连的所有钻石.并获得 消除数量*消除数量  的分 思路: 直接暴搜,然后用一个cnt数组表示每一种钻石剩 ...

  2. hdu 4090

    GemAnd Prince Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. HDU 4616 Game (搜索)、(树形dp)

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4616 这道题目数据可能比较弱,搜索都可以AC,但是不敢写,哎…… 搜索AC代码: #include & ...

  4. [HDU 2102] A计划(搜索题,典型dfs or bfs)

    A计划 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  5. hdu 4722(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. #include<iostre ...

  6. hdu 5335 Walk Out (搜索)

    题目链接: hdu 5335 Walk Out 题目描述: 有一个n*m由0 or 1组成的矩形,探险家要从(1,1)走到(n, m),可以向上下左右四个方向走,但是探险家就是不走寻常路,他想让他所走 ...

  7. HDU 1896 Stones --优先队列+搜索

    一直向前搜..做法有点像模拟.但是要用到出队入队,有点像搜索. 代码: #include <iostream> #include <cstdio> #include <c ...

  8. HDU 1978 记忆化搜索(dfs+dp)

    Y - How many ways Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4597 记忆化搜索

    ² 博弈取牌—记忆化搜索 题目描述: 有两副带有数字的牌,(数字>0)两人轮流取,取中了某张牌,自己的分数就加上牌上的数字,但只能从两端取,每人都会用最优的策略使得自己的分数最高.问A先取,他能 ...

  10. hdu 1560 DNA sequence(搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1560 DNA sequence Time Limit: 15000/5000 MS (Java/Others)  ...

随机推荐

  1. [2013-03-14]使用wiki维护产品文档

    word文档作为产品文档的问题: word文档本身的设计是为了打印: word文档的编辑较为繁琐: 作为产品文档的word文档往往长达百页以上,难以维护,且容易分散注意力,不利于查阅: 没有一个简单易 ...

  2. Webpack+Vue+ES6 前端组件化开发mobile-multi-page应用实战总结

    本文版权归博客园和作者吴双本人共同所有 转载和爬虫请注明原文地址 www.cnblogs.com/tdws 一.写在前面 项目上线有一段时间了,一个基于webpack+vue+ES6的手机端多页面应用 ...

  3. java对象与json对象之间的转换

    jar包:import net.sf.json.JSONObject; 1.解析json字符串 将json字符串转换为json对象,然后再解析json对象:.JSONObjectjsonObject ...

  4. javasript校验字符串【正则和其他函数】

    /**javasript校验输入框值只能为数字中文英文和下划线**/function isRegex(s){ var reg=/^[a-zA-Z0-9_\u4e00-\u9fa5]+$/; if (! ...

  5. 启动tomcat错误:Address already in use: JVM_Bind:8081

    解决方法: 1.打开任务管理器,关闭掉javaw进程. 2.修改tomcat端口:tomcat/conf/server.xml

  6. NPIO 导出Execl

    步骤1:导入NOIO.dll    (我导入压缩包中的4.0)

  7. 团队作业8——第二次项目冲刺(Beta阶段)--5.25 5th day

    团队作业8--第二次项目冲刺(Beta阶段)--5.25 fifth day Day five: 会议照片 项目进展 Beta冲刺的第四天,以下是今天具体任务安排: 队员 昨天已完成的任务 今日计划完 ...

  8. 结对作业1----基于flask框架的四则运算生成器

    011.012结对作业 coding地址:https://coding.net/u/nikochan/p/2nd_SE/git 一.作业描述 由于上次作业我没有按时完成,而且庞伊凡同学编程能力超棒,所 ...

  9. 团队作业4——第一次项目冲刺(Alpha版本)7th day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 在计时模式下能够记录用户的用户名和成绩,没有弄登录功能, 将程序定义为单机的 未完成的卡片为登录功能和使用QQ登录. 四.困难 ...

  10. 201521123023《Java程序设计》第五周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 2. 书面作业 Q1.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过 ...