您的位置:首页 > 其它

hdu 4090 GemAnd Prince(dfs)

2015-08-25 10:41 316 查看


GemAnd Prince

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,矩阵上每个点都有一个数字,数字局限是1到k,某个数字相同的连通块(附加的8个方向)若是块数大于3,那么我们就可以选择消掉这个连通块,分数是块数的平方,求最高分数?

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#define INF 100000000
using namespace std;
int n,m,k,cnt,ans;
int dir[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};

int map[8][8];
int maxsum() //理想状态的最优分数(一个剪枝条件:目前分数+剩下部分的最优分数《=已计算出的最高分数则剪枝)
{
int val[10]={0};
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
if (map[i][j])
{
val[map[i][j]]++;
}
}
}
int temp=0;
for (int i=1; i<=k; i++)
{
temp+=val[i]*val[i];
}
return temp;
}

void dfs1(int x,int y,int val,bool vis[][8])//找连通块
{
cnt++;
vis[x][y]=true;
map[x][y]=0;
for (int i=0;i<8; i++)
{
x+=dir[i][0];
y+=dir[i][1];
if(x>=0 && x<n && y>=0 && y<m &&map[x][y]==val && !vis[x][y])
{
dfs1(x,y,val,vis);
}
x-=dir[i][0];
y-=dir[i][1];
}
}

void move()
{
//下移
for (int i=n-1 ; i>=0; i--)
{
for (int j=0; j<m; j++)
{
if(!map[i][j])
{
for (int e=i-1; e>=0; e--)
{
if (map[e][j])
{
map[i][j]=map[e][j];
map[e][j]=0;
break;
}
}
}
}
}
//左移
for (int i=0; i<m; i++)
{
if(map[n-1][i]==0)
{
for (int j=i+1; j<m; j++)
{
if(map[n-1][j])
{
for (int g=0; g<n; g++)
{
map[g][i]=map[g][j];
map[g][j]=0;
}
break;
}
}
}
}
}
void dfs(int value)
{
bool vis[8][8];
int temp[8][8];
memset(vis, false, sizeof(vis));
if(maxsum()+value<=ans)
return;
memcpy(temp, map, sizeof(map));
if(value>ans)
ans=value;
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
{
cnt=0;
memcpy(map, temp, sizeof(temp));
if(map[i][j] && !vis[i][j])
dfs1(i, j,map[i][j] , vis);

if(cnt<3)
continue;
move();
dfs(value+cnt*cnt);
}
}
}
int main()
{
while (scanf("%d%d%d",&n,&m,&k)!=EOF)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<m; j++)
scanf("%d",&map[i][j]);
}
ans=0;
dfs(0);
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: