题目来源: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. ffmpeg捕捉摄像头发送rtmp

    打印 DirectShow 支持的设备列表(true 可用1替换): ffmpeg -list_devices true -f dshow -i dummy 本计算机打印出的信息如下:[dshow @ ...

  2. Bootstrap CSS教程

    Bootstrap 教程 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更加 ...

  3. yeoman,grunt,bower

    Yeoman主要有三部分组成:yo(脚手架工具).grunt(构建工具).bower(包管理器).这三个工具是分别独立开发的,但是需要配合使用,来实现我们高效的工作流模式. http://www.cn ...

  4. bzoj 1001 狼抓兔子 —— 平面图最小割(最短路)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 平面图最小割可以转化成最短路问题: 建图时看清楚题目的 input ... 代码如下: ...

  5. C# 中常用LInq操作

    static void Main(string[] args) { , , , , , , }; , , , , , , }; , , , , , , , , , , , }; // 交集 var f ...

  6. 杂项-JS:artTemplate.js

    ylbtech-杂项-JS:artTemplate.js artTemplate 是新一代 javascript 模板引擎,它采用预编译方式让性能有了质的飞跃,并且充分利用 javascript 引擎 ...

  7. uboot的relocation原理详细分析

    转自:http://blog.csdn.net/skyflying2012/article/details/37660265 最近在一直在做uboot的移植工作,uboot中有很多值得学习的东西,之前 ...

  8. java基础知识(10)---包

    包:定义包用package关键字. 1:对类文件进行分类管理. 2:给类文件提供多层名称空间. 如果生成的包不在当前目录下,需要最好执行classpath,将包所在父目录定义到classpath变量中 ...

  9. linux日常管理-系统进程查看工具-ps

    查看系统有那些进程 命令有ps aux 和命令 ps -elf USER  哪个用户使用了这个进程 PID  进程的id %CPU 占用CPU的百分比 %MEM 占用内存的百分比 VSZ 虚拟内存的大 ...

  10. struts2 json返回试验

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-/ ...