题目来源: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的更多相关文章

  1. poj分类 很好很有层次感。

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  2. 【转】POJ题目分类推荐 (很好很有层次感)

    OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2739,poj1083,poj2262,poj1503,poj3006,poj2255,poj3094)初期: 一. ...

  3. 【转】ACM训练计划

    [转] POJ推荐50题以及ACM训练方案 -- : 转载自 wade_wang 最终编辑 000lzl POJ 推荐50题 第一类 动态规划(至少6题, 和 必做) 和 (可贪心) (稍难) 第二类 ...

  4. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

  5. (转)POJ题目分类

    初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推. ...

  6. acm常见算法及例题

    转自:http://blog.csdn.net/hengjie2009/article/details/7540135 acm常见算法及例题  初期:一.基本算法:     (1)枚举. (poj17 ...

  7. poj分类

    初期: 一.基本算法:      (1)枚举. (poj1753,poj2965)      (2)贪心(poj1328,poj2109,poj2586)      (3)递归和分治法.      ( ...

  8. 转载 ACM训练计划

    leetcode代码 利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode. ...

  9. ACM算法总结及刷题参考

    参考:http://bbs.byr.cn/#!article/ACM_ICPC/11777 OJ上的一些水题(可用来练手和增加自信)(poj3299,poj2159,poj2739,poj1083,p ...

随机推荐

  1. 省选/NOI刷题Day2

    bzoj2616 放一个车的时候相当于剪掉棋盘的一行,于是就可以转移了,中间状态转移dp套dp,推一下即可 bzoj2878 环套树期望dp 手推一下递推式即可 bzoj3295 树状数组套权值线段树 ...

  2. Lisp的本质(The Nature of Lisp)

    Lisp的本质(The Nature of Lisp) 作者 Slava Akhmechet                             译者 Alec Jang 出处: http://w ...

  3. nodejs 静态文件服务器

    https://cnodejs.org/topic/4f16442ccae1f4aa27001071 http://blog.csdn.net/zhangxin09/article/details/8 ...

  4. org.springframework.web.client.HttpClientErrorException: 400 null

    异常代码: org.springframework.web.client.HttpClientErrorException: 400 null 已解决. 百度了一下400代表无法解析请求. 说明请求是 ...

  5. 问题:C# List;结果:C#中数组、ArrayList和List三者的区别

    C#中数组.ArrayList和List三者的区别 分类: [C#那些事] 2013-03-11 00:03 36533人阅读 评论(23) 收藏 举报 目录(?)[+] 在C#中数组,ArrayLi ...

  6. DES加密/解密

    /// <summary> /// DES加密/解密类. /// </summary> public class DESEncrypt { #region ========加密 ...

  7. tomcat jsp 数字串传值异常问题

    1.在下面的jsp内嵌java代码去除num之前,有某Controller已经有了操作:      request.getSession().setAttribute("num", ...

  8. nodejs中 underscore 包有什么作用

    Underscore一个JavaScript实用库,提供了一整套函数式变成有用的实用功能,但是没有扩展任何JavaScript内置对象.它是这个问题的答案:“如果我在一个空白的HTML页面前坐下, 并 ...

  9. R: data.frame 生成、操作数组。重命名、增、删、改

    ################################################### 问题:生成.操作数据框   18.4.27 怎么生成数据框 data.frame.,,及其相关操 ...

  10. 30、 bowtie和bowtie2使用条件区别及用法

    转载:http://blog.csdn.net/soyabean555999/article/details/62235577 一.转录组还是基因组? map常用的工具有bowtie/bowtie2, ...