题意:
给出一个n*m的图,‘.’可以走,‘#’不能走,图中有字母‘a’~‘z’,每个时间每个字母会向四联通的四个格子扩散,如果两个字母相遇,那个格子会变成‘*’,问最后的图长什么样。
思路:
从每个字母开始搜,记录一下就好了。
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<string>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
#include<climits>
#include<queue>
using namespace std;
int n,m;
char maze[505][505];
int vis[505][505];
int a[]= {1,-1,0,0};
int b[]= {0,0,1,-1};
struct Point
{
int x,y,r;
char ans;
Point(int xx,int yy,char anss,int rr)
{
x=xx;
y=yy;
ans=anss;
r=rr;
}
};
void bfs()
{
queue<Point> q;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if(maze[i][j] >= 'a' && maze[i][j] <= 'z')
q.push(Point(i,j,maze[i][j],0));
while(!q.empty())
{
Point t = q.front();
q.pop();
int x = t.x;
int y = t.y;
int r = t.r;
char ans = t.ans;
if(maze[x][y] == '*') continue;
for(int i=0; i<4; i++)
{
if(x + a[i] <= n && x + a[i] >= 1 && y + b[i] >= 1 && y + b[i] <= m)
{
if(maze[x + a[i]][y + b[i]] == '.')
{
vis[x + a[i]][y + b[i]] = r + 1;
maze[x + a[i]][y + b[i]] = ans;
q.push(Point(x + a[i],y + b[i],ans,r+1));
}
else
{
if(maze[x + a[i]][y + b[i]] >= 'a' || maze[x + a[i]][y + b[i]] <= 'z')
{
if(vis[x + a[i]][y + b[i]] )
{
if(vis[x + a[i]][y + b[i]] == r + 1 && ans != maze[x + a[i]][y + b[i]])
{
maze[x + a[i]][y + b[i]] = '*';
}
}
}
}
}
}
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%s",maze[i] + 1);
memset(vis,0,sizeof(vis));
bfs();
for(int i=1; i<=n; i++)
printf("%s\n",maze[i]+1);
cout<<endl;
}
return 0;
}