Constraints:
1 <= R, C <= 500
1 <= T <= 5
Sample Input:
5
3 5
#####
a...b
#####
3 4
####
a..b
####
3 3
#c#
a.b
#d#
3 3
#c#
...
a.b
3 5
.....
.#.#.
a...b
Sample Output:
#####
aa*bb
#####
####
aabb
####
#c#
a*b
#d#
#c#
acb
a*b
aa*bb
a#.#b
aa*bb
#include <bits/stdc++.h> #include <cstring> #include <iostream> #include <algorithm> #define foror(i,a,b) for(i=a;i<b;i++) #define foror2(i,a,b) for(i=a;i>b;i--) #define EPS 1e-9 #define PI acos(-1.0) #define INF 1000000000 #define MOD 1000000007 #define mem(a,b) memset((a),b,sizeof(a)) #define TS printf("!!! ") #define lson o<<1, l, m #define rson o<<1|1, m+1, r using ll = long long; using ull= unsigned long long; //std::ios::sync_with_stdio(false); using namespace std; char s[505][505]; int visit[505][505]; int dx[4]={-1,0,0,1}; int dy[4]={0,-1,1,0}; int a,b; int sum; struct point{ int x,y; char ch; int num; }; queue<point> q; bool check(point jqk) { if((jqk.x>=1&&jqk.x<=a)&&(jqk.y>=1&&jqk.y<=b)&&(s[jqk.x][jqk.y]!='#')&&(s[jqk.x][jqk.y]!='*')) return true; return false; } char readchar() { char tmp = 0; char ch; bool read = false; while (ch = getchar()) { if ((ch >='a'&&ch<='z')||ch=='#'||ch=='.') { read = true; tmp = ch; } else if (read) { break; } else { continue; } } return tmp; } void bfs() { while(!q.empty()) { point aim=q.front(); q.pop(); //cout << aim.ch << " "; if(s[aim.x][aim.y]!='*') for(int i=0;i<=3;i++) { point cur; cur.x=aim.x+dx[i]; cur.y=aim.y+dy[i]; cur.ch=aim.ch; cur.num=aim.num+1; if(check(cur)) { if(s[cur.x][cur.y]>='a'&&s[cur.x][cur.y]<='z') { if(s[cur.x][cur.y]==cur.ch) continue; else { if(visit[cur.x][cur.y]!=cur.num) continue; else { s[cur.x][cur.y]='*'; } } } else if(s[cur.x][cur.y]=='.') { s[cur.x][cur.y]=cur.ch; visit[cur.x][cur.y]=cur.num; q.push(cur); } } } } } int main() { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int T; cin >>T; while(T--) { mem(visit,0); char pop; scanf("%d %d",&a,&b); point aim; for(int i=1;i<=a;i++) scanf("%s",s[i]+1); /*for(int i=1;i<=a;i++) printf("%s ",s[i]+1);*/ for(int i=1;i<=a;i++) for(int j=1;j<=b;j++) { pop=s[i][j]; if(pop!='#'&&pop!='.') { aim.x=i; aim.y=j; aim.ch=pop; aim.num=1; q.push(aim); visit[i][j]=1; } } if(!q.empty()) bfs(); for(int i=1;i<=a;i++) printf("%s ",s[i]+1); printf(" "); printf(" "); } //fclose(stdin); //fclose(stdout); return 0; }