題目來源:http://poj.org/problem?id=1027ios
題目大意:ide
題目說的就是如今蠻流行的手機小遊戲popstar,求用貪心方法能獲得多少分。測試
小球有三種顏色:R/G/B。橫向、縱向相連的同色小球能夠同時被消掉。消掉一些小球后,首先空格上面的小球會下落,填補空白,而後若是中間出現空的列,則空列右側的小球左移,填補空列。spa
當m個球被消掉時,獎分(m-2)^2,若全部球都被消掉,獎分1000.貪心策略是每次選擇消去連在一塊兒數目最多的球。當有多個時,找最左最下的球所在的那一羣。3d
輸入:第一行爲測試用例數。每一個用例由一系列字符串組成,表示初始時小球的分佈。調試
輸出:每步選擇的小球,每步消掉的小球數,獲得的分數,以及最後的得分和剩餘小球數。code
具體形式見Sampleblog
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 Outputci
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 }