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
| #include <bits/stdc++.h> #define ios ios::sync_with_stdio(0),cin.tie(0); using namespace std; int n,col[150],dx[8] = {-1,0,1,1,1,0,-1,-1}, dy[8] = {-1,-1,-1,0,1,1,1,0},board[1005][1005]; bool visit[1005][1005][5];
int encode(char a){ col['Y'] = 1;col['B'] = 2;col['G'] = 3;col['R'] = 4; col['O'] = 5;col['P'] = 6;col['D'] = 7; return col[a]; } struct node{ int x,y,time,color; };
int main(){ ios; int t;cin>>t; while(t--){ cin>>n; queue<node> qq; memset(board, 0, sizeof(board)); memset(visit, 0, sizeof(visit)); char a; for(int i=0;i<3;i++){ int b,c;cin>>a>>b>>c;b++;c++; qq.push((node){b,c,1,encode(a)}); board[b][c] = encode(a); } cin>>a; int target = encode(a),cur_sum = 0,ans = 0,time = 1; if(a=='B'||a=='Y'||a=='R')cur_sum++; if(a=='D'){ cout<<n*n<<endl; continue; } while(!qq.empty()){ while(!qq.empty() && qq.front().time==time){ int nx = qq.front().x,ny = qq.front().y; for(int i=0;i<8;i++){ int x = nx+dx[i],y = ny+dy[i]; if(x<=0 ||x>n||y<=0||y>n||board[x][y]==7|| visit[x][y][qq.front().color])continue; qq.push((node){x,y,time+1,qq.front().color}); int new_color = board[x][y]|qq.front().color; if(new_color!=board[x][y]){ if(new_color==target)cur_sum++; else if(board[x][y]==target)cur_sum--; } board[x][y]|=qq.front().color; visit[x][y][qq.front().color] = 1; } qq.pop(); } time++; if(ans>cur_sum)break; ans = max(ans, cur_sum); } cout<<ans<<endl; } }
|