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 ...
随机推荐
- OpenCV-Python sift/surf特征匹配与显示
import cv2 import numpy as np def drawMatchesKnn_cv2(img1_gray,kp1,img2_gray,kp2,goodMatch): h1, w1 ...
- HihoCoder1339 Dice Possibility(概率DP+母函数)
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 What is possibility of rolling N dice and the sum of the numb ...
- Qt Quick中的信号与槽
在QML中,在Qt Quick中,要想妥善地处理各种事件,肯定离不开信号与槽,本博的主要内容就是整理Qt 中的信号与槽的内容. 1. 链接QML类型的已知信号 QML中已有类型定义的信号分为两类:一类 ...
- 1103 Integer Factorization (30)(30 分)
The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- bzoj 4771: 七彩树 树链的并+可持久化线段树
题目大意: 给定一颗树,询问树中某个点x的子树中与其距离不超过d的所有点中本质不同的颜色数 强制在线 题解: 一下午终于把这道题叉掉了. 写了三个算法,前两个都是错的,后一个是%的网上大爷们的题解. ...
- codevs 3372 选学霸
3372 选学霸 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 老师想从N名学生中选M人当学霸,但有K对人实力相当,如果 ...
- SLF4j+LOG4j
工作笔记:在myeclipse 中创建一个java project 创建一个 TestSlf4J 类 import org.slf4j.Logger; import org.slf4j.LoggerF ...
- Project Web Server PSI 接口一些常用操作
对Project Web Server进行二次开发,每天都把自己折腾到12点以后才休息,到处都是坑,研究那些烦人的PSI,国内根本查不到PSI相关的资料,对照API文档一点点谷歌资料,全部英文资料,开 ...
- BO 与 VO 的属性拷贝 copyProperties(bo,vo)
NewClass extends OldClass : newClass继承自oldClass,属性大多数相同,但是也有新的扩展. beanUtils.copyProperties(oldClas ...
- Math类简介
Math abs max min 分别是绝对值 最大值,最小值 round 四舍五入 ceil ceil(32.6) 33.0 ceil(32.2) 33.0 返回大于该数值的较大的整数 与之相对 ...