POJ 1027--The Same Game
来源:互联网 发布:苹果手机怎么抹除数据 编辑:程序博客网 时间:2024/06/16 17:47
题意
这道题比较简单,就是一个消除游戏,求最终能得到的最高分数,并输出每一次消除的分数,颜色,个数,和消除区域最左、最下边的坐标。
分析
因为在出现空列的时候,需要将右边部分合并到左边,所以考虑按列处理,而不是按行。
我们把每一列看作一个字符串,那么所有球构成一个初始长度为10的15个字符串。这里利用了一个string的特性,string在中间某一字符被删除的时候会自动收缩。比如,str = “abcd”,在删除字符‘b’之后,str自动变为”acd”,这样避免了手动去移动字符。
在搜索最大区域的时候,假定最后需要N次来消除,那么我们在搜索的时候把每个字符均加上当前次数n[1, N]在每次消除的时候,所有字符均变为 c(原值) + n(1..N)。因为每次搜索的起点[x,y]有记录,最后删除的时候直接从坐标[x,y]开始去删除邻近的值等于matrix[x,y]的小球就行。
搜索和删除最大区域均用了一个函数int search(int pos, int col, char key, bool erase),pos为当前搜索列的下标(从下到上),也就是字符串的下标。col为当前所有列的数目,也就是字符串数目。key为搜索或删除目标,通过erase确定是删除还是搜索。
因为是深度搜索,所以删除的时候,先标记要删除的字符,最后遍历一次删除即可。
PS:注意输出格式。
代码如下:
Memory: 220K Time: 500MS Length: 93LINES
#include <iostream>#include <string>using namespace std;const unsigned int max_rows = 10;const unsigned int max_cols = 15;unsigned int cols;string matrix[max_cols];int search(int pos, int col, char key, bool erase){ int left, right; for (left = pos; left >= 0; --left) { if (matrix[col][left] == key) matrix[col][left] = erase ? '0' : matrix[col][left] + 1; else break; } ++left; for (right = pos + 1; right < matrix[col].length(); ++right) { if (matrix[col][right] == key) matrix[col][right] = erase ? '0' : matrix[col][right] + 1; else break; } int sum = right - left; for (int i = left; i < right; ++i) { if (col < cols - 1 && matrix[col + 1].length() > i && matrix[col + 1][i] == key) sum += search(i, col + 1, key, erase); if (col > 0 && matrix[col - 1].length() > i && matrix[col - 1][i] == key) sum += search(i, col - 1, key, erase); } return sum;}bool check(char c) { return (c == 'R' || c == 'G' || c == 'B'); }int main(){ int count = 0; cin >> count; for (int i = 0; i < count; ++i) { char c; short remaind = max_rows * max_cols; int final_score = 0; for (int j = 0; j < max_cols; ++j) matrix[j].clear(); for (int j = 0; j < max_rows; ++j) for (int k = 0; k < max_cols && cin >> c && check(c); matrix[k] = c + matrix[k], ++k); cout << "Game " << i + 1 << ":" << endl << endl; cols = max_cols; for (int step = 1, maxsum = 2; maxsum > 1; ++step, remaind -= maxsum) { maxsum = 0; int j, k; int x = -1, y = -1; int zone = 0; for (j = 0; j < cols; ++j) for (k = 0; k < matrix[j].length(); ++k) { if (check(matrix[j][k] - step + 1)) { int res = search(k, j, matrix[j][k], false); if (res > maxsum) { maxsum = res; x = j; y = k; } } } if (maxsum < 2) break; int score = (maxsum - 2)*(maxsum - 2); final_score += score; cout << "Move " << step << " at (" << y + 1 << "," << x + 1 << "): removed " << maxsum; cout << " balls of color " << char(matrix[x][y] - step) << ", got " << score << " points." << endl; search(y, x, matrix[x][y], true); for (j = 0; j < cols; ++j) { for (string::size_type pos; (pos = matrix[j].find('0')) != string::npos; matrix[j].erase(pos, 1)); if (matrix[j].length() == 0) { for (int k = j; k < cols - 1; ++k) matrix[k] = matrix[k + 1]; --j; --cols; } } } cout << "Final score: " << final_score + (remaind == 0 ? 1000 : 0) << ", with " << remaind << " balls remaining." << endl << endl; } return 0;}
阅读全文
0 0
- poj 1027 The Same Game
- POJ 1027 The Same Game
- POJ 1027 The Same Game
- Poj 1027 The Same Game
- poj 1027 The Same Game
- POJ 1027--The Same Game
- POJ 1027The Same Game
- The Same Game POJ
- POJ 1027 The Same Game 模拟题
- POJ 1027 The Same Game BFS
- poj 1027 The Same Game(大模拟)
- poj 1027 The Same Game 模拟
- POJ 1027 The Same Game 大暴消
- POJ 1027 The Same Game 笔记 未完成
- poj 1027.The Same Game
- POJ 1027 The Same Game棋盘游戏(模拟)
- POJ1027--The Same Game
- poj1027 The Same Game
- Web 存储机制
- 操作系统每日笔记(一):导论(一)
- Ubuntu下安装python3.4
- IntelliJ IDEA 2017.1.4 License Server
- 单片机时钟程序(9)lcd1602.h
- POJ 1027--The Same Game
- 【数据压缩】MPEG音频编码
- 快速排序
- 反射(3)——为什么用反射?!!
- 7.4---SSH之Hibernate初始篇
- C++并发编程框架Theron(1)——Actor模型介绍
- C—HelloWorld
- Linux系统编程——进程间通信:共享内存
- HTML5之WebSQL