GemAnd Prince
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 872 Accepted Submission(s): 299
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?
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).
![](https://i-blog.csdnimg.cn/blog_migrate/39ec7b72c7827d402326e2b2c2a98feb.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/95f724027541e7ea6cf976e72b9915c2.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/d185e33f598db30ac9febf39a6db4a38.jpeg)
![](https://i-blog.csdnimg.cn/blog_migrate/b96050bcea1be9c8b62eab9c5d060347.jpeg)
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.
![](https://i-blog.csdnimg.cn/blog_migrate/0430062d852352ccf40eb7f38de78574.jpeg)
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
Source
题意:做了几个这样的类型题目。都是根据一个游戏,模拟消除东西,
题目告诉了你,最多有6种颜色,有什么用?我就想到了一个优化,因为题目要求至少3个才能消除。
假如我的6种颜色的个数,都小于3,那就不要做了。但是这样的剪枝是很有限的,由于开始代码的搓,
把标记的0也当成颜色统计,所以起初完全没有达到优化效果。
其实还有一个优化的地方,就是统计当前状态下所可能达到的最大价值+已有的价值 和 变量MAX(用来保存最大价值的变量)
进行比较。
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 #include<queue> 6 using namespace std; 7 int n,m,ki; 8 int a[8][8],MAX; 9 int color[7]; 10 int qq[8][8]; 11 int to[8][2]={{1,0},{0,1},{-1,0},{0,-1},{1,1},{-1,-1},{1,-1},{-1,1}}; 12 struct node{ 13 int x,y; 14 }; 15 queue<node>Q; 16 int getsum() 17 { 18 int i,j,ans=0; 19 memset(color,0,sizeof(color)); 20 for(i=0;i<n;i++) 21 for(j=0;j<m;j++) 22 if(a[i][j]) color[a[i][j]]++; 23 24 for(i=0;i<=ki;i++) 25 ans=ans+color[i]*color[i]; 26 return ans; 27 } 28 void change() 29 { 30 int i,j,k; 31 memset(qq,0,sizeof(qq)); 32 for(i=0;i<m;i++){ 33 for(j=n-1,k=n-1;j>=0;j--) 34 if(a[j][i]!=0) qq[k--][i]=a[j][i]; 35 } 36 memset(a,0,sizeof(a)); 37 for(i=0,k=0;i<m;i++){ 38 for(j=0;j<n;j++) if(qq[j][i]!=0)break; 39 if(j==n)continue; 40 for(j=n-1;j>=0;j--) 41 a[j][k]=qq[j][i]; 42 k++; 43 } 44 } 45 int bfs(int x,int y,int num,bool (*hash)[8]) 46 { 47 int i,gs=0; 48 node t,cur; 49 while(!Q.empty()){ 50 Q.pop(); 51 } 52 t.x=x; 53 t.y=y; 54 Q.push(t); 55 while(!Q.empty()) 56 { 57 cur=Q.front(); 58 Q.pop(); 59 for(i=0;i<8;i++){ 60 t=cur; 61 t.x=t.x+to[i][0]; 62 t.y=t.y+to[i][1]; 63 if(t.x>=0&&t.x<n && t.y>=0&&t.y<m){ 64 if(!hash[t.x][t.y]&&a[t.x][t.y]==num) 65 { 66 gs++; 67 a[t.x][t.y]=0; 68 hash[t.x][t.y]=true; 69 Q.push(t); 70 } 71 } 72 } 73 } 74 return gs; 75 } 76 void dfs(int now){ 77 78 if(getsum()+now<=MAX) return;//第一个剪枝 79 if(MAX<now) MAX=now; 80 int i,j,num,lhxl=-1; 81 int g[8][8]; 82 int color[7]; 83 bool hash[8][8]; 84 memset(hash,false,sizeof(hash)); 85 memcpy(g,a,sizeof(g)); 86 memset(color,0,sizeof(color)); 87 for(i=0;i<n;i++) 88 for(j=0;j<m;j++) 89 if(a[i][j]) color[a[i][j]]++; 90 for(i=1;i<=ki;i++) if(color[i]>lhxl) lhxl=color[i]; 91 if(lhxl<3) return;//two 92 for(i=0;i<n;i++) 93 for(j=0;j<m;j++) 94 { 95 if(!hash[i][j] && g[i][j]!=0) 96 { 97 memcpy(a,g,sizeof(a)); 98 num = bfs(i,j,g[i][j],hash); 99 color[g[i][j]]-=num; 100 if(num<3)continue;//three 101 change(); 102 dfs(now+num*num); 103 } 104 } 105 } 106 int main() 107 { 108 int i,j; 109 while(scanf("%d %d %d",&n,&m,&ki)>0){ 110 for(i=0;i<n;i++) 111 for(j=0;j<m;j++) 112 scanf("%d",&a[i][j]); 113 MAX=-1; 114 dfs(0); 115 printf("%d\n",MAX); 116 } 117 return 0; 118 }