1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| #include <cstdio> #include <vector> #include <cstring> #include <utility> #define N 20 using namespace std; typedef pair<int , int> pr; vector<pr> v[N * N]; int n , m , a[N][N] , b[N][N] , c[N][N] , tx[N] , ty , vis[N][N]; char str[N]; inline int ctoi(char c) { return c == 'R' ? 1 : c == 'G' ? 2 : 3; } inline char itoc(int a) { return a == 1 ? 'R' : a == 2 ? 'G' : 'B'; } void floodfill(int x , int y , int k) { v[k].push_back(pr(x , y)) , vis[x][y] = 1; if(x > 1 && a[x - 1][y] == a[x][y] && !vis[x - 1][y]) floodfill(x - 1 , y , k); if(x < n && a[x + 1][y] == a[x][y] && !vis[x + 1][y]) floodfill(x + 1 , y , k); if(y > 1 && a[x][y - 1] == a[x][y] && !vis[x][y - 1]) floodfill(x , y - 1 , k); if(y < m && a[x][y + 1] == a[x][y] && !vis[x][y + 1]) floodfill(x , y + 1 , k); } int main() { int T , cc; scanf("%d" , &T); for(cc = 1 ; cc <= T ; cc ++ ) { int i , j , cnt , sum = 0 , step; vector<pr>::iterator it; scanf("%d%d" , &n , &m) , cnt = n * m; for(i = n ; i >= 1 ; i -- ) { scanf("%s" , str + 1); for(j = 1 ; j <= m ; j ++ ) a[i][j] = ctoi(str[j]); } printf("Game %d:\n\n" , cc); for(step = 1 ; cnt ; step ++ ) { memset(vis , 0 , sizeof(vis)); int mx = 0 , pi , id = 0; for(j = 1 ; j <= m ; j ++ ) { for(i = 1 ; i <= n ; i ++ ) { if(a[i][j] && !vis[i][j]) { id ++ , v[id].clear() , floodfill(i , j , id); if(mx < (int)v[id].size()) mx = v[id].size() , pi = id; } } } if(mx < 2) break; printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n" , step , v[pi][0].first , v[pi][0].second , mx , itoc(a[v[pi][0].first][v[pi][0].second]) , (mx - 2) * (mx - 2)); sum += (mx - 2) * (mx - 2) , cnt -= mx; for(it = v[pi].begin() ; it != v[pi].end() ; it ++ ) a[it->first][it->second] = 0; memset(b , 0 , sizeof(b)) , memset(tx , 0 , sizeof(tx)) , memset(c , 0 , sizeof(c)) , ty = 0; for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j <= m ; j ++ ) if(a[i][j] != 0) b[++tx[j]][j] = a[i][j]; for(j = 1 ; j <= m ; j ++ ) { if(tx[j] != 0) { ty ++ ; for(i = 1 ; i <= n ; i ++ ) c[i][ty] = b[i][j]; } } for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j <= m ; j ++ ) a[i][j] = c[i][j]; } if(!cnt) sum += 1000; printf("Final score: %d, with %d balls remaining.\n\n" , sum , cnt); } return 0; }
|