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. java中的Robot

    项目中最近遇到了一个棘手问题,无法用WebDriver去操作win弹出窗口,经过多番查找,发现了Robot这个奇葩东东,Robot可以模拟鼠标和键盘操作, robot可以实现本地系统输入空包括win弹 ...

  2. (七)DAC0832 数模转换芯片的应用 以及运算放大器的学习 01

    DAC0832是8分辨率的D/A转换集成芯片.与微处理器完全兼容.这个DA芯片以其价格低廉.接口简单.转换控制容易等优点,在单片机应用系统中得到广泛的应用.D/A转换器由8位输入锁存器.8位DAC寄存 ...

  3. Linux内核之旅 链表实现

    #include "stdio.h" #include "stdlib.h" struct list_head{ struct list_head *prev; ...

  4. sql 索引创建

    --格式 --CREATE [索引类型] INDEX 索引名称--ON 表名(列名)--WITH FILLFACTOR = 填充因子值0~100--GO ----------------------- ...

  5. ralink网卡驱动的下载地址集合

    linuxMT7612U11/7/2014v3.0.0.1http://cdn-cw.mediatek.com/Downloads/linux/MT7612U_DPO_LinuxSTA_3.0.0.1 ...

  6. 导出excel部分代码

    public static function header_file($doc,$file,$title,$type='Excel5'){ if(!empty($doc)){ $objWriter = ...

  7. Openstack的镜像属性

    先来看张图: 容易理解的地方我们就不介绍了,我们这里介绍'公有'和'受保护'的 在shell命令中,公有用is-public=True表示,而受保护的用is-protected表示,公有的反面是is- ...

  8. IoC 依赖注入、以及在Spring中的实现

    资源来自网络: 去年火得不行的Spring框架,一般的书籍都会从IoC和AOP开始介绍起,这个IoC概念,个人感觉资料里都写得让人看得有些痛苦,所谓IoC,就是控制反转(Inversion of Co ...

  9. HTTP错误汇总(404、302、200……)

    HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...

  10. linux端口

    1.查看开放的端口 netstat -anp 来查看哪些端口被打开. 注:加参数'-n'会将应用程序转为端口显示,即数字格式的地址,如:nfs->2049, ftp->21,因此可以开启两 ...