[UVA][模擬] 758 - The Same Game
The Same Game
The Same Game |
The game named ``Same'' is a single-person game played on a 10 15 board. Each square contains a ball colored red (R), green (G), or blue (B). Two balls belong to the same cluster if they have the same color, and one can be reached from another by following balls of the same color in the four directions up, down, left, and right. At each step of the game, the player chooses a ball whose cluster has at least two balls and removes all balls in the cluster from the board. Then, the board is ``compressed'' in two steps:
- 1.
- Shift the remaining balls in each column down to fill the empty spaces. The order of the balls in each column is preserved.
- 2.
- If a column becomes empty, shift the remaining columns to the left as far as possible. The order of the columns is preserved.
For example, choosing the ball at the bottom left corner in the sub-board below causes:
The objective of the game is to remove every ball from the board, and the game is over when every ball is removed or when every cluster has only one ball. The scoring of each game is as follows. The player starts with a score of 0. When a cluster of m balls is removed, the player's score increases by (m - 2)2. A bonus of 1000 is given if every ball is removed at the end of the game.
You suspect that a good strategy might be to choose the ball that gives the largest possible cluster at
each step, and you want to test this strategy by writing a program to simulate games played using this
strategy. If there are two or more balls to choose from, the program should choose the leftmost ball
giving the largest cluster. If there is still a tie, it should choose the bottommost ball of these leftmost balls.
Input
You will be given a number of games in the input. The first line of input contains a positive integer giving the number of games to follow. The initial arrangement of the balls of each game is given one row at a time, from top to bottom. Each row contains 15 characters, each of which is one of ``R'', ``G'', or ``B'', specifying the colors of the balls in the row from left to right. A blank line precedes each game.
Output
For each game, print the game number, followed by a new line, followed by information about each move, followed by the final score. Each move should be printed in the format:
Move x at (r,c): removed b balls of color C, got s points.
where x is the move number, r and c are the row
number and column number of the chosen ball, respectively. The rows are
numbered from 1 to 10 from the bottom, and columns are numbered from 1
to 15 from the left. b is the number of balls in the cluster removed. C is one of ``R'', ``G'', or ``B'', indicating the color of the balls removed. s is the score for this move. The score does not include the
1000 point bonus if all the balls are removed after the move.
The final score should be reported as follows:
Final score: s, with b balls remaining.
Insert a blank line between the output of each game. Use the plural forms ``balls'' and ``points'' even if the corresponding value is 1.
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.
Miguel A. Revilla
2000-02-09
還以為題目要求我們找一個最佳的策略得到最高分。
但事實上並不是,題目已經給定一個 Greedy 的策略,模擬它就可以了。
遊戲是這個樣子的:
1. 上下左右四個方向若為顏色相同的是同一個區塊
2. 點擊的區塊至少有兩個
3. 點擊之後,先落下,再向左靠齊,並不會補新的
4. 當無法再次點擊時,遊戲結束
5. 如果盤面上沒有任何一顆,則會得到額外的 1000 分
遊戲策略是:
每次找一個最多區塊的,且越靠近左下角越好。
#include <stdio.h>
#include <string.h>
char g[20][20];
int used[20][20];
int combo;
void dfs(int x, int y, char c) {
if(x < 0 || y < 0 || x >= 10 || y >= 15)
return ;
if(used[x][y] || g[x][y] != c)
return ;
used[x][y] = 1;
combo++;
dfs(x+1, y, c);
dfs(x-1, y, c);
dfs(x, y+1, c);
dfs(x, y-1, c);
}
void touch(int x, int y, char c) {
if(x < 0 || y < 0 || x >= 10 || y >= 15)
return ;
if(g[x][y] != c)
return ;
g[x][y] = 0;
combo++;
touch(x+1, y, c);
touch(x-1, y, c);
touch(x, y+1, c);
touch(x, y-1, c);
}
void F5() {
int i, j, k;
for(i = 0; i < 15; i++) {
int idx = 0;
for(j = 0; j < 10; j++) {
if(g[j][i]) {
g[idx++][i] = g[j][i];
}
}
while(idx < 10)
g[idx][i] = 0, idx++;
}
k = 0;
for(j = 0; j < 15; j++) {
for(i = 0; i < 10; i++) {
if(g[i][j])
break;
}
if(i != 10) {//has shift right
for(i = 0; i < 10; i++)
g[i][k] = g[i][j];
k++;
}
}
while(k < 15) {
for(i = 0; i < 10; i++)
g[i][k] = 0;
k++;
}
}
int main() {
int testcase, cases = 0;
int i, j, k;
scanf("%d", &testcase);
while(testcase--) {
for(i = 9; i >= 0; i--)
scanf("%s", &g[i]);
memset(used, 0, sizeof(used));
int step = 0, remaind = 150;
int score = 0;
printf("Game %d:\n\n", ++cases);
while(true) {
memset(used, 0, sizeof(used));
int mxchoose = -1, x, y;
for(j = 0; j < 15; j++) {
for(i = 0; i < 10; i++) {
if(used[i][j] == 0 && g[i][j] != 0) {
combo = 0;
dfs(i, j, g[i][j]);
if(combo >= 2) {
if(combo > mxchoose)
mxchoose = combo, x = i, y = j;
}
}
}
}
if(mxchoose < 0)
break;
remaind -= mxchoose;
printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n",
++step, x+1, y+1, mxchoose, g[x][y], (mxchoose-2)*(mxchoose-2));
score += (mxchoose-2)*(mxchoose-2);
touch(x, y, g[x][y]);
F5();
}
if(remaind == 0)
score += 1000;
printf("Final score: %d, with %d balls remaining.\n", score, remaind);
if(testcase)
puts("");
}
return 0;
}