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 ;
}

hdu 2258 优先队列的更多相关文章

  1. hdu 5306 优先队列

    用到优先队列 #include<iostream> #include<string> #include<algorithm> #include<cstdio& ...

  2. HDU 4006 优先队列

    The kth great number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Oth ...

  3. hdu 5818 (优先队列) Joint Stacks

    题目:这里 题意: 两个类似于栈的列表,栈a和栈b,n个操作,push a x表示把数x放进a栈的栈底,pop b 表示将栈b的栈顶元素取出输出,并释放这个栈顶元素,merge a b表示把后面的那个 ...

  4. hdu 4302 优先队列

    进一步学习了优先队列的用法 题意:一只小动物在直线上走,起始位置为零,之后会出现食物,动物要去距离自己最短的食物那,若两边的食物距离相等,则选择之前走的方向的食物 0 x,代表x的位置出现了食物,1代 ...

  5. hdu 4393 优先队列

    用优先队列储存每个人的初始距离和编号,每轮求出最快的人,然后pop掉 一开始想遍历队列的,后来发现队列没办法遍历,汗-_-! 题意,给几个第一秒冲出的距离和以后速度,求每秒后最前面人的编号,求完后最前 ...

  6. HDU 1058 优先队列or堆

    本来应当是一道优先队列或者堆的题 因为每个数都应该是已经得到的数*2 *3 *5 *7而得到的 但是 2*7 大于 3*2 这就必须保证每次取得都是没有拿过的最小的数 但是它主动降低难度在样例里卖了个 ...

  7. hdu 4544 优先队列+贪心

    题意:最近,减肥失败的湫湫为发泄心中郁闷,在玩一个消灭免子的游戏.游戏规则很简单,用箭杀死免子即可.箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di ...

  8. HDU 5700 优先队列(或者multiset) 或 线段树

    题目大意:有n个区间,求k个区间,使得这k个区间相交的区间内数字之和最大.数列的数字均>=0 优先队列思路: 按照左端点sort,然后枚举左端点,假设他被覆盖过k次,然后用优先队列来维护最右端即 ...

  9. hdu 1026(优先队列+路径输出)

    Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

随机推荐

  1. mysql之消息队列

    消息队列:在消息的传输过程中保存消息的容器. 消息队列管理器在将消息从它的源中继到它的目标时充当中间人.队列的主要目的是提供路由并保证消息的传递:如果发送消息时接收者不可用,消息队列会保留消息,直到可 ...

  2. mysql之触发器before和after的区别

    我们先做个测试: 接上篇日志建的商品表g和订单表o和触发器 假设:假设商品表有商品1,数量是10: 我们往订单表插入一条记录: insert into o(gid,much) values(1,20) ...

  3. demo06

    city_data.xml <?xml version="1.0" encoding="utf-8"?> <resources> < ...

  4. php laravel curD

    Laravel PHP Web开发框架 Laravel是一套简洁.优雅的PHP Web开发框架(PHP Web Framework).它可以让你从面条一样杂乱的代码中解脱出来:它可以帮你构建一个完美的 ...

  5. paper 86:行人检测资源(上)综述文献【转载,以后使用】

    行人检测具有极其广泛的应用:智能辅助驾驶,智能监控,行人分析以及智能机器人等领域.从2005年以来行人检测进入了一个快速的发展阶段,但是也存在很多问题还有待解决,主要还是在性能和速度方面还不能达到一个 ...

  6. web工程常见部署方式总结

    作为一个web测试工程师,对测试所属的平台架构,项目部署情况应该是有所了解的,下面在此基础上总结下web项目在各种场景下常用的部署方式: 第一种方法: 开发常用部署方法,直接在myeclipse里部署 ...

  7. yii2购物车实现

    1.商品列表中点击加入购物车,则跳转到购物车列表,效果如图所示: 视图代码goods/list.php中.代码如下: <?php echo Html::a('加入购物车',['cart','id ...

  8. cvLoadImage函数解析 cvLoadImageM()函数

    1.函数原型:IplImage* cvLoadImage( const char* filename, int flags=CV_LOAD_IMAGE_COLOR ); filename :要被读入的 ...

  9. Floyd算法核心代码证明

    Flody  大家都知道这个最终模版: for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i ...

  10. android 项目学习随笔二十一(IM、语音识别、机器人、统计、扫描二维码、条形码)

    语音识别:科大讯飞语音云 http://www.xfyun.cn/ 语音机器人模拟 public class TalkBean { public String text; public boolean ...