hdu 2258 优先队列

Continuous Same Game (1)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 410    Accepted Submission(s): 143

Problem Description
Continuous Same Game is a simple game played on a grid of colored blocks. Groups of two or more connected (orthogonally, not diagonally) blocks that are the same color may be removed from the board. When a group of blocks is removed, the blocks above those removed ones fall down into the empty space. When an entire column of blocks is removed, all the columns to the right of that column shift to the left to fill the empty columns. Points are scored whenever a group of blocks is removed. The number of points per block increases as the group becomes bigger. When N blocks are removed, N*(N-1) points are scored.

LL was interested in this game at one time, but he found it is so difficult to find the optimal scheme. So he always play the game with a greedy strategy: choose the largest group to remove and if there are more than one largest group with equal number of blocks, choose the one which contains the most preceding block ( (x1,y1) is in front of (x2,y2) if and only if (x1<x2 || x1==x2 && y1<y2) ). Now, he want to know how many points he will get. Can you help him?

 
Input
Each test case begins with two integers n,m ( 5<=n,m<=20 ), which is the size of the board. Then n lines follow, each contains m characters, indicating the color of the block. There are 5 colors, and each with equal probability.
 
Output
For each test case, output a single line containing the total point he will get with the greedy strategy. 
 
Sample Input
5 5
35552
31154
33222
21134
12314
 
Sample Output
32
 
优先队列,取反,记住!!
做了hdu3090,这道题就很有思路的。
 
题意:题目要求每一次按照贪心的步骤来做,所以只要根据贪心的规则来模拟这个过程就好了。
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std; int n,m,MAX;
char tom[][];
int a[][];
int to[][]={{,},{,},{,-},{-,}};
bool hash[][];
bool use[][];
struct node
{
friend bool operator< (node n1,node n2)
{
if(n1.val>n2.val)return false;//no return true;
else if(n1.val==n2.val)
{
if(n1.x<n2.x)return false;
else if(n1.x==n2.x && n1.y<n2.y) return false;
}
return true;
}
int x,y,val;
};
struct st
{
int x,y;
};
priority_queue<node>Q; int bfs(int x,int y,int num)
{
int i,cout=;
queue<st>S;
st t,cur;
t.x=x;
t.y=y;
hash[x][y]=true;
S.push(t); while(!S.empty())
{
cur=S.front();
S.pop();
for(i=;i<;i++)
{
t=cur;
t.x=t.x+to[i][];
t.y=t.y+to[i][];
if(t.x>=&&t.x<n &&t.y>=&&t.y<m && !hash[t.x][t.y] && a[t.x][t.y]==num){
hash[t.x][t.y]=true;
cout++;
S.push(t);
}
}
}
return cout;
}
void change(node &t)
{
int i,j,k;
int qq[][];
memset(qq,,sizeof(qq));
memset(hash,false,sizeof(hash));
k = bfs(t.x,t.y,a[t.x][t.y]);
for(i=;i<n;i++)
for(j=;j<m;j++)
if(hash[i][j]) a[i][j]=;
for(i=;i<m;i++){
for(j=n-,k=n-;j>=;j--)
if(a[j][i]) qq[k--][i] = a[j][i];
}
memset(a,,sizeof(a));
for(i=,k=;i<m;i++){
for(j=;j<n;j++) if(qq[j][i]!=)break;
if(j==n)continue;
for(j=n-;j>=;j--)
a[j][k]=qq[j][i];
k++;
}
}
void dfs(int now)
{
int i,j,val;
bool flag=false;
node t;
while(!Q.empty()){
Q.pop();
}
if(now>MAX) MAX = now;
memset(hash,false,sizeof(hash));
for(i=;i<n;i++){
for(j=;j<m;j++){
if(!hash[i][j] && a[i][j]!=)
{
val = bfs(i,j,a[i][j]);
if(val == ) continue;
t.x=i;
t.y=j;
t.val=val*(val-);
flag=true;
Q.push(t);
}
}
}
if(flag==false) return;
t=Q.top();
change(t);
dfs(now+t.val);
}
int main()
{
int i,j;
while(scanf("%d%d",&n,&m)>)
{
for(i=;i<n;i++)
scanf("%s",tom[i]);
for(i=;i<n;i++)
for(j=;j<m;j++)
a[i][j]=tom[i][j]-'';
MAX=-;
dfs();
printf("%d\n",MAX);
}
return ;
}
上一篇:Struts1文件上传、单文件、多文件上传【Struts1】


下一篇:iOS开发之创建颜色渐变视图View