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.

按照给定的策略模拟游戏的进行即可,只是调试比较需要耐心和细心。

  1 //////////////////////////////////////////////////////////////////////////
  2 //        POJ1027 The Same Game
  3 //        Memory: 288K        Time: 500MS
  4 //        Language: C++        Result: Accepted
  5 //////////////////////////////////////////////////////////////////////////
  6 
  7 #include <iostream>
  8 #include <stdio.h>
  9 
 10 using namespace std;
 11 
 12 int gameCnt;
 13 char board[11][16];
 14 int record[11][16];
 15 int rows[15];
 16 int maxRow = 0;
 17 int maxCol = 0;
 18 char color = ' ';
 19 int maxCluster = 0;
 20 int score;
 21 
 22 int bfs(int row, int col) {
 23     if (board[row][col] == 0) {
 24         return 0;
 25     }
 26     color = board[row][col];
 27     int cluster = 1;
 28     record[row][col] = 1;
 29     if (board[row + 1][col] != 0 && record[row + 1][col] == 0 && board[row + 1][col] == color) {
 30         cluster += bfs(row + 1, col);
 31     }
 32     if (board[row - 1][col] != 0 && record[row - 1][col] == 0 && board[row - 1][col] == color) {
 33         cluster += bfs(row - 1, col);
 34     }
 35     if (board[row][col + 1] != 0 && record[row][col + 1] == 0 && board[row][col + 1] == color) {
 36         cluster += bfs(row, col + 1);
 37     }
 38     if (board[row][col - 1] != 0 && record[row][col - 1] == 0 && board[row][col - 1] == color) {
 39         cluster += bfs(row, col - 1);
 40     }
 41     return cluster;
 42 }
 43 
 44 void clear(int row, int col) {
 45     board[row][col] = 0;
 46     if (board[row + 1][col] != 0 && board[row + 1][col] == color) {
 47         clear(row + 1, col);
 48     }
 49     if (board[row - 1][col] != 0 && board[row - 1][col] == color) {
 50         clear(row - 1, col);
 51     }
 52     if (board[row][col + 1] != 0 && board[row][col + 1] == color) {
 53         clear(row, col + 1);
 54     }
 55     if (board[row][col - 1] != 0 && board[row][col - 1] == color) {
 56         clear(row, col - 1);
 57     }
 58 }
 59 
 60 void process(int row, int col) {
 61     clear(row, col);
 62     int r = 0, c = 0;
 63     for (c = 0; c < 15; ++c) {
 64         int i, j;
 65         for (i = 0; i < 10 && board[i][c] != 0; ++i) {
 66             continue;
 67         }
 68         for (j = i; j < 10 && board[j][c] == 0; ++j) {
 69             continue;
 70         }
 71         while (i < 10) {
 72             if (j > 10) {
 73                 board[i++][c] = 0;
 74                 continue;
 75             }
 76             if (board[j][c] == 0) {
 77                 ++j;
 78                 continue;
 79             }
 80             board[i++][c] = board[j++][c];
 81         }
 82     }
 83     int i = 0, j = 0;
 84     for (i = 0; i < 15 && board[0][i] != 0; ++i) {
 85         continue;
 86     }
 87     for (j = i; j < 15 && board[0][j] == 0; ++j) {
 88         continue;
 89     }
 90     while (i < 15) {
 91         if (j > 15) {
 92             for (int k = 0; k <= 10; ++k) {
 93                 board[k][i] = 0;
 94             }
 95             ++i;
 96             continue;
 97         }
 98         if (board[0][j] == 0) {
 99             ++j;
100             continue;
101         }
102         for (int k = 0; k <= 10; ++k) {
103             board[k][i] = board[k][j];
104         }
105         ++i;
106         ++j;
107     }
108 }
109 
110 int main() {
111 
112     cin >> gameCnt;
113     for (int gameId = 1; gameId <= gameCnt; ++gameId) {
114         int row, col;
115         memset(record, 0, sizeof(record));
116         color = ' ';
117         maxCluster = 0;
118         for (row = 9; row >= 0; --row) {
119             for (col = 0; col < 15; ++col) {
120                 cin >> board[row][col];
121             }
122         }
123         int move = 1;
124         int score = 0;
125         int remain = 150;
126 
127         cout << "Game " << gameId << ":" << endl << endl;
128         while (true) {
129             maxCluster = 0;
130             memset(record, 0, sizeof(record));
131             for (row = 0, col = 0; board[row][col] != 0; ++col) {
132                 for (row = 0; board[row][col] != 0; ++ row) {
133                     if (record[row][col] != 0) continue;
134                     int cluster = bfs(row, col);
135                     if (cluster > maxCluster) {
136                         maxRow = row;
137                         maxCol = col;
138                         maxCluster = cluster;
139                     }
140                 }
141                 row = 0;
142             }
143             color = board[maxRow][maxCol];
144             if (maxCluster < 2) {
145                 break;
146             }
147             int point = (maxCluster - 2) * (maxCluster - 2);
148             remain -= maxCluster;
149             cout << "Move "<< move << " at (" << maxRow + 1 << ","<< maxCol + 1 
150                 << "): removed " << maxCluster <<" balls of color " << color << ", got " 
151                 << point << " points." << endl;
152             ++move;
153             score += point;
154             process(maxRow, maxCol);
155         }
156         if (remain == 0) {
157             score += 1000;
158         }
159         cout << "Final score: " << score << ", with " << remain << " balls remaining." << endl << endl;
160     }
161 
162     system("pause");
163     return 0;
164 }
View Code
posted @ 2013-07-31 22:32  小菜刷题史  阅读(542)  评论(0编辑  收藏  举报