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 ...
随机推荐
- Agc011_C Squared Graph
传送门 题目大意 给定$n$个点$m$条边的简单图(无重边无自环),将有序点对$\{a,b\}$作为新的点,新产生的$n^2$个点中对于两个点,$\{a,b\},\{x,y\}$,当且仅当原图中存在边 ...
- 1014 Waiting in Line (30)(30 分)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...
- jQuery DataTables 使用手册(精简版)
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/5182940.html 前排提醒,这个插件能不用就不用,那么多好的插件等着你,为什么要用它呢?就算用easyui的 ...
- 使用IronPython集成Python和.NET
本文由 伯乐在线 - 艾凌风 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:www.informit.com.欢迎加入翻译组. 从两个优秀的世界各取所需,更高效的复用代码.想想就醉了,. ...
- Java中Calendar/SimpleDateFormat/Date常用方法总结
//获取当前时刻yyyy-MM-dd HH:mm:ss Calendar calendar = Calendar.getInstance(); SimpleDateFormat sdf = new S ...
- POJ2559:Largest Rectangle in a Histogram
浅谈栈:https://www.cnblogs.com/AKMer/p/10278222.html 题目传送门:http://poj.org/problem?id=2559 贪心的想,最大的子矩阵顶部 ...
- bzoj 1257 余数之和 —— 数论分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1257 \( \sum\limits_{i=1}^{n}k\%i = \sum\limits_ ...
- Ajax知识点整理
一.javascript原生Ajax 1.简介 Ajax是Asynchronous JavaScript+XML(异步JavaScript和XML)的缩写. 该名称诞生于XML还是数据传输首选格式的时 ...
- 《Kubernetes权威指南第2版》学习(三)RC学习
1 RC文件介绍: kind: ReplicationController,表示是一个RC: spec.selector: RC的Pod标签(Label)选择器,监控和管理拥有这些标签的Pod实例, ...
- 三 Flask web开发快速入门
1:会话: from flask import Flask, url_for, request, render_template, session from werkzeug.utils import ...