POJ1027 The Same Game
题目来源:http://poj.org/problem?id=1027
题目大意:
题目说的就是现在蛮流行的手机小游戏popstar,求用贪心方法能得到多少分。
小球有三种颜色:R/G/B。横向、纵向相连的同色小球可以同时被消掉。消掉一些小球后,首先空格上面的小球会下落,填补空白,然后如果中间出现空的列,则空列右侧的小球左移,填补空列。
当m个球被消掉时,奖分(m-2)^2,若所有球都被消掉,奖分1000.贪心策略是每次选择消去连在一起数目最多的球。当有多个时,找最左最下的球所在的那一群。

输入:第一行为测试用例数。每个用例由一系列字符串组成,表示初始时小球的分布。
输出:每步选择的小球,每步消掉的小球数,得到的分数,以及最后的得分和剩余小球数。
具体形式见Sample
Sample Input
3
RGGBBGGRBRRGGBG
RBGRBGRBGRBGRBG
RRRRGBBBRGGRBBB
GGRGBGGBRRGGGBG
GBGGRRRRRBGGRRR
BBBBBBBBBBBBBBB
BBBBBBBBBBBBBBB
RRRRRRRRRRRRRRR
RRRRRRGGGGRRRRR
GGGGGGGGGGGGGGG RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
GGGGGGGGGGGGGGG
GGGGGGGGGGGGGGG
BBBBBBBBBBBBBBB
BBBBBBBBBBBBBBB
RRRRRRRRRRRRRRR
RRRRRRRRRRRRRRR
GGGGGGGGGGGGGGG
GGGGGGGGGGGGGGG RBGRBGRBGRBGRBG
BGRBGRBGRBGRBGR
GRBGRBGRBGRBGRB
RBGRBGRBGRBGRBG
BGRBGRBGRBGRBGR
GRBGRBGRBGRBGRB
RBGRBGRBGRBGRBG
BGRBGRBGRBGRBGR
GRBGRBGRBGRBGRB
RBGRBGRBGRBGRBG
Sample Output
Game 1: Move 1 at (4,1): removed 32 balls of color B, got 900 points.
Move 2 at (2,1): removed 39 balls of color R, got 1369 points.
Move 3 at (1,1): removed 37 balls of color G, got 1225 points.
Move 4 at (3,4): removed 11 balls of color B, got 81 points.
Move 5 at (1,1): removed 8 balls of color R, got 36 points.
Move 6 at (2,1): removed 6 balls of color G, got 16 points.
Move 7 at (1,6): removed 6 balls of color B, got 16 points.
Move 8 at (1,2): removed 5 balls of color R, got 9 points.
Move 9 at (1,2): removed 5 balls of color G, got 9 points.
Final score: 3661, with 1 balls remaining. Game 2: Move 1 at (1,1): removed 30 balls of color G, got 784 points.
Move 2 at (1,1): removed 30 balls of color R, got 784 points.
Move 3 at (1,1): removed 30 balls of color B, got 784 points.
Move 4 at (1,1): removed 30 balls of color G, got 784 points.
Move 5 at (1,1): removed 30 balls of color R, got 784 points.
Final score: 4920, with 0 balls remaining. Game 3: Final score: 0, with 150 balls remaining.
按照给定的策略模拟游戏的进行即可,只是调试比较需要耐心和细心。
//////////////////////////////////////////////////////////////////////////
// POJ1027 The Same Game
// Memory: 288K Time: 500MS
// Language: C++ Result: Accepted
////////////////////////////////////////////////////////////////////////// #include <iostream>
#include <stdio.h> using namespace std; int gameCnt;
char board[][];
int record[][];
int rows[];
int maxRow = ;
int maxCol = ;
char color = ' ';
int maxCluster = ;
int score; int bfs(int row, int col) {
if (board[row][col] == ) {
return ;
}
color = board[row][col];
int cluster = ;
record[row][col] = ;
if (board[row + ][col] != && record[row + ][col] == && board[row + ][col] == color) {
cluster += bfs(row + , col);
}
if (board[row - ][col] != && record[row - ][col] == && board[row - ][col] == color) {
cluster += bfs(row - , col);
}
if (board[row][col + ] != && record[row][col + ] == && board[row][col + ] == color) {
cluster += bfs(row, col + );
}
if (board[row][col - ] != && record[row][col - ] == && board[row][col - ] == color) {
cluster += bfs(row, col - );
}
return cluster;
} void clear(int row, int col) {
board[row][col] = ;
if (board[row + ][col] != && board[row + ][col] == color) {
clear(row + , col);
}
if (board[row - ][col] != && board[row - ][col] == color) {
clear(row - , col);
}
if (board[row][col + ] != && board[row][col + ] == color) {
clear(row, col + );
}
if (board[row][col - ] != && board[row][col - ] == color) {
clear(row, col - );
}
} void process(int row, int col) {
clear(row, col);
int r = , c = ;
for (c = ; c < ; ++c) {
int i, j;
for (i = ; i < && board[i][c] != ; ++i) {
continue;
}
for (j = i; j < && board[j][c] == ; ++j) {
continue;
}
while (i < ) {
if (j > ) {
board[i++][c] = ;
continue;
}
if (board[j][c] == ) {
++j;
continue;
}
board[i++][c] = board[j++][c];
}
}
int i = , j = ;
for (i = ; i < && board[][i] != ; ++i) {
continue;
}
for (j = i; j < && board[][j] == ; ++j) {
continue;
}
while (i < ) {
if (j > ) {
for (int k = ; k <= ; ++k) {
board[k][i] = ;
}
++i;
continue;
}
if (board[][j] == ) {
++j;
continue;
}
for (int k = ; k <= ; ++k) {
board[k][i] = board[k][j];
}
++i;
++j;
}
} int main() { cin >> gameCnt;
for (int gameId = ; gameId <= gameCnt; ++gameId) {
int row, col;
memset(record, , sizeof(record));
color = ' ';
maxCluster = ;
for (row = ; row >= ; --row) {
for (col = ; col < ; ++col) {
cin >> board[row][col];
}
}
int move = ;
int score = ;
int remain = ; cout << "Game " << gameId << ":" << endl << endl;
while (true) {
maxCluster = ;
memset(record, , sizeof(record));
for (row = , col = ; board[row][col] != ; ++col) {
for (row = ; board[row][col] != ; ++ row) {
if (record[row][col] != ) continue;
int cluster = bfs(row, col);
if (cluster > maxCluster) {
maxRow = row;
maxCol = col;
maxCluster = cluster;
}
}
row = ;
}
color = board[maxRow][maxCol];
if (maxCluster < ) {
break;
}
int point = (maxCluster - ) * (maxCluster - );
remain -= maxCluster;
cout << "Move "<< move << " at (" << maxRow + << ","<< maxCol +
<< "): removed " << maxCluster <<" balls of color " << color << ", got "
<< point << " points." << endl;
++move;
score += point;
process(maxRow, maxCol);
}
if (remain == ) {
score += ;
}
cout << "Final score: " << score << ", with " << remain << " balls remaining." << endl << endl;
} system("pause");
return ;
}
POJ1027 The Same Game的更多相关文章
- poj分类 很好很有层次感。
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 【转】POJ题目分类推荐 (很好很有层次感)
OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...
- 【转】ACM训练计划
[转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
- (转)POJ题目分类
初期:一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. (4)递推. ...
- acm常见算法及例题
转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题 初期:一.基本算法: (1)枚举. (poj17 ...
- poj分类
初期: 一.基本算法: (1)枚举. (poj1753,poj2965) (2)贪心(poj1328,poj2109,poj2586) (3)递归和分治法. ( ...
- 转载 ACM训练计划
leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...
- ACM算法总结及刷题参考
参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,p ...
随机推荐
- UVA12163 游戏
题目大意 现在有两个人在一个n个结点的有向图上玩一个双人游戏,保证图中无环和自圈.游戏的规则如下:1.初始的时候$i$号点有一个正权值$value_i$2.两名玩家依次操作,每个玩家在当前回合可以选择 ...
- 每天一个linux命令(13):more命令
版权声明更新:2017-05-17博主:LuckyAlan联系:liuwenvip163@163.com声明:吃水不忘挖井人,转载请注明出处! 1 文章介绍 本文介绍了Linux下面的mv命令. 2. ...
- 【LeetCode】084. Largest Rectangle in Histogram
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
- Nested loops、Hash join、Sort merge join(三种连接类型原理、使用要点)
nested loop 嵌套循环(原理):oracle从较小结果集(驱动表.也可以被称为outer)中读取一行,然后和较大结果集(被侦查表,也可以叫做inner)中的所有数据逐条进行比较(也是等值连接 ...
- gulp之压缩图片
//先全局安装gulp:npm install -g gulp //然后在项目根目录中安装gulp依赖:npm install --save-dev gulp //http://www.gulpjs. ...
- Exchange邮箱设置,android手机和mac book
假设 用户名:abc 密码:123 公司名是:qq 一 android手机: 1 输入地址:abc@qq.com 2 密码:123 3 协议:EXCHANGE 点击下一步 用户名:abc 域名:qqc ...
- BZOJ1972:[SDOI2010]猪国杀
我对模拟的理解:https://www.cnblogs.com/AKMer/p/9064018.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem ...
- bzoj 4823 & 洛谷 P3756 老C的方块 —— 最小割
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4823 https://www.luogu.org/problemnew/show/P3756 ...
- 学习过程的记录:实验室电脑上的jdk环境变量
亲爱的,先区分安装路径和软件的存放路径好不好呢? 1.变量名:JAVA_HOME 变量值:D:\Program Files\Java\jdk1.7.0_21 2. 编辑 Path(粘贴到最后) %JA ...
- 基于C语言的ssdb笔记 ----hashmap的简单实例
ssdb支持 zset, map/hash, list, kv 数据结构,同redis差不多.下面是关于ssdb hsahmap的使用笔记 1.ssdb hashmap的命令 1.hset name ...