题目链接

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. python appium 操作app

    下面是一些Python脚本中操作app的用法: 检查app安装情况(返回true/false), driver.is_app_installed(package_name) 安装app driver. ...

  2. 前端开发【第3篇:JavaScript序】

    JavaScript历史 聊聊JavaScript的诞生 JavaScirpt鼻祖:Bremdan Eich(布兰登·艾奇),JavaScript的诞生于浏览器的鼻祖网景公司(Netscape),发布 ...

  3. JAVA 文件编译执行与虚拟机(JVM)简单介绍

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytpo3 java程序的内存分配 JAVA 文件编译执行与虚拟机(JVM)介绍 ...

  4. 8.中断按键驱动程序之poll机制

    本节继续在上一节中断按键程序里改进,添加poll机制. 那么我们为什么还需要poll机制呢.之前的测试程序是这样: ) { read(fd, &key_val, ); printf(" ...

  5. [UWP]使用Writeable​Bitmap创建HSV色轮

    1. HSV 1.1 HSV的定义 HSV都是一种将RGB色彩模型中的点在圆柱坐标系中的表示法,这种表示法试图做到比RGB基于笛卡尔坐标系的几何结构更加直观.HSV即色相.饱和度.明度(英语:Hue, ...

  6. [2017BUAA软工]第0次个人作业

    第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 我觉得我选择计算机系完全是误打误撞吧.当时我的分数上北航是没问题的,所以填专业时就是机械,电气,自动化,计算机等 ...

  7. PCB Design_经验之谈

    所谓覆铜,就是将PCB上闲置的空间作为基准面,然后用固体铜填充,这些铜区又称为灌铜.敷铜的意义在于,减小地线阻抗,提高抗干扰能力:降低压降,提高电源效率:与地线相连,还可以减小环路面积.也出于让PCB ...

  8. 部署自己的GitLab

    先说明一下自己的想法:无论怎么样,个人确实先不想升级到centos7上面,因为我觉得centos6还是比较占用资源少的,而且作为生产环境,centos6完全够用了. 实验测试环境: * centos ...

  9. 2017春季 JMU 1414软工助教 链接汇总

    助教自我介绍 学生博客链接和coding链接 [1414软工助教]团队博客汇总 助教总结 评分 个人作业1:四则运算控制台 结对项目1:GUI 个人作业2:案例分析 结对项目2:单元测试 团队作业1: ...

  10. 【2017集美大学1412软工实践_助教博客】团队作业10——项目复审与事后分析(Beta版本)

    写在前面的话 转眼轰轰烈烈本学期的软工实践就结束了,这个过程中想必在熬夜敲代码,激烈讨论中留下诸多回忆的同时,也收获了不少.恭喜所有团队完成了本阶段冲刺,此外,由于大家的贡献分给的都很平均,将个人贡献 ...