#include <stdio.h>
#include <memory.h>
#include <queue>
struct node {int x, y;};
int w, h, cnt, v[1010][1010]={}, dx[]={1, -1, 0, 0}, dy[]={0, 0, 1, -1};
char G[1010][1010]={};
struct node S, fire[1000010];
int main() {
int t;
scanf("%d", &t);
for(int i=0; i<t; i++) {
memset(v, -1, sizeof(v));
scanf("%d %d", &w, &h);
for(int i=1; i<=h; i++) {
for(int j=1; j<=w; j++) {
scanf(" %c", &G[i][j]);
if(G[i][j]=='@') S.x=i, S.y=j;
if(G[i][j]=='*') fire[cnt].x=i, fire[cnt].y=j, cnt++;
}
}
for(int i=1; i<=h; i++, puts(""))
for(int j=1; j<=w; j++) {
printf("%c ", G[i][j]);
}
std::queue<node> q;
for(int i=0; i<cnt; i++) {
int nx=fire[i].x, ny=fire[i].y;
q.push({nx, ny});
v[nx][ny]=0;
}
while(!q.empty()) {
node s=q.front();
q.pop();
for(int i=0; i<4; i++) {
int nx=s.x+dx[i], ny=s.y+dy[i];
if(G[nx][ny]=='.'&&v[nx][ny]==-1) {
q.push({nx, ny});
v[nx][ny]=v[s.x][s.y]+1;
}
}
}
for(int i=1; i<=h; i++, puts(""))
for(int j=1; j<=w; j++) {
printf("%d ", v[i][j]);
}
}
}
Pour intégrer ce projet à votre site web, copiez le code suivant et collez-le dans le HTML de votre site web :