简单bfs
代码:
#include <iostream>
#include<string.h>
#include <queue>
using namespace std;
struct ttt{
int r,c;
int step;
};
char map1[15][15];
int walked[15][15];
int dr[4]={0,1,0,-1};
int dc[4]={1,0,-1,0};
int main(){
//freopen("in.txt","r",stdin);
int i,j,k,l,f1,f2,f3,t1,t2,t3;
int n,m;
ttt u,v;
queue<ttt>qq;
while(scanf("%d%d",&n,&m)==2){
scanf("%d%d",&n,&m);
while(!qq.empty())qq.pop();
memset(walked,0,sizeof(walked));
memset(map1,0,sizeof(map1));
u.step=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++){
scanf("%c",&map1[i][j]);
if(map1[i][j]=='\n'){
j--;
continue;
}
if(map1[i][j]=='*'){
//cout << i << " " << j <<endl;
u.r=i;
u.c=j;
qq.push(u);
}
}
while(!qq.empty()){
u=qq.front();qq.pop();
if(u.step<m){
for(i=0;i<4;i++){
v.c=u.c+dc[i];
v.r=u.r+dr[i];
if(map1[v.r][v.c]=='.'){
map1[v.r][v.c]='*';
v.step=u.step+1;
qq.push(v);
}
}
}
}
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
cout << map1[i][j];
cout <<endl;
}
cout << endl;
}
return 0;
}