#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
struct node{
int x,y,mv;
friend bool operator < (node n1,node n2){
return n1.mv<n2.mv;
}
};
int n,m,mv;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
char graph[105][105]; //由于不需要回溯,所以可以直接在原来的地图上进行标记
bool vis[105][105];
int sx,sy;
bool judge(int x, int y){
for(int i=0;i<4;i++){
int xx = x+dx[i];
int yy = y+dy[i];
if(xx>=0 && xx<n && yy>=0 && yy<m &&graph[xx][yy]=='E'){
return true;
}
}
return false;
}
void bfs(){
priority_queue<node> q;
node cur,next;
vis[sx][sy] = 1;
cur.x = sx;
cur.y = sy;
cur.mv = mv;
q.push(cur);
while(!q.empty()){
cur = q.top();
q.pop();
if(cur.mv <= 0) continue;
// if(judge(cur.x,cur.y) && graph[cur.x][cur.y]!='Y')
// cur.mv = 0;
if(cur.mv==0) continue;
for(int i=0;i<4;i++){
int xx = cur.x+dx[i];
int yy = cur.y+dy[i];
if(xx>=0 && xx<n && yy>=0 && yy<m && !vis[xx][yy] && graph[xx][yy]!='#' && graph[xx][yy]!='*' && graph[xx][yy]!='E'){
next.x = xx;
next.y = yy;
next.mv = cur.mv;
char ch = graph[xx][yy];
if(ch=='.' || ch=='P')
next.mv--;
else if(ch=='T')
next.mv -= 2;
else if(ch=='R')
next.mv -=3;
if(next.mv<0) continue;
if(judge(next.x,next.y)) next.mv = 0;
q.push(next);
vis[next.x][next.y] = 1;
if(graph[next.x][next.y] != 'P')
graph[next.x][next.y] = '*';
// if(graph[xx][yy] == '.'){ //1
// next.mv = cur.mv-1;
// graph[xx][yy] = '*';
// if(next.mv>0){
// vis[xx][yy] = 1;
// q.push(next);
// }
//
// }
// else if(graph[xx][yy] == 'T'){ //2
// next.mv = cur.mv-2;
// graph[xx][yy] = '*';
// if(next.mv>0){
// vis[xx][yy] = 1;
// q.push(next);
// }
// }
// else if(graph[xx][yy] == 'R'){ //3
// next.mv = cur.mv-3;
// graph[xx][yy] = '*';
// if(next.mv>0){
// vis[xx][yy] = 1;
// q.push(next);
// }
// }
// else if(graph[xx][yy] == 'P'){
// next.mv = cur.mv-1;
// if(next.mv>0){
// vis[xx][yy] = 1;
// q.push(next);
// }
// }
}
}
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&m,&mv);
for(int i=0;i<n;i++)
scanf("%s",graph[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(graph[i][j]=='Y'){
sx = i;
sy = j;
}
memset(vis,false,sizeof(vis));
bfs();
for(int i=0;i<n;i++)
printf("%s\n",graph[i]);
printf("\n");
}
return 0;
}
HDU 3345 bfs
最新推荐文章于 2018-07-21 09:51:06 发布