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. 封装mysqli类

    类: <?phpheader('content-type:text/html;charset=utf-8');/*掌握满足单例模式的必要条件(1)私有的构造方法-为了防止在类外使用new关键字实 ...

  2. HDU 1728 逃离迷宫(BFS)

    Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...

  3. 夺命雷公狗---DEDECMS----9dedecms单标签

    我们这一节课开始将dedecms的标签了,dedecms里面的标签分好多个的,我们先来看下他的标签长得啥样的先: 随便点击一个修改即可见到标签了: 这里面上面的大文本框里面有标签的用法下面有参数的说明 ...

  4. PAT乙级 1019. 数字黑洞 (20)

    1019. 数字黑洞 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定任一个各位数字不完全相同的4位 ...

  5. Windows7(x64)下Oracle10g安装

    安装环境:Windows7 (64位版本) + Oracle10g 问题描述1:无法启动安装程序,程序提示“程序异常终止.发生内部错误....” 解决过程:按网上说法加6.1版本参数,按xp兼容模式启 ...

  6. Xml游标

    Mainactivity package com.exmple.xmlstream; import java.util.ArrayList; import java.util.List; import ...

  7. jquery中的each用法以及js中的each方法实现实例

    each()方法能使DOM循环结构简洁,不容易出错.each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组.多维数组.DOM, JSON 等等在javaScript开发过程中使用$ ...

  8. Mongodb 笔记06 副本集的组成、从应用程序连接副本集、管理

    副本集的组成 1. 同步:MongoDB的复制功能是使用操作日志oplog实现的,操作日志包含了主节点的每一次写操作.oplog是主节点的local数据库中的一个固定集合.备份节点通过查询整个集合就可 ...

  9. 怎样使用AutoLayOut为UIScrollView添加约束

    1.在ViewController中拖入1个UIScrollView,并为其添加约束 约束为上下左右四边与superview对齐 2.在scrollview中,拖入1个UIView,为了便于区分将其设 ...

  10. 慎用MySQL replace语句

    语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [PARTITION (partition_name,...)] [(col_name,... ...