InputThe first line of the inputs is T, which stands for the number of test cases you need to solve. Then T cases follow: Each test case starts with a line contains three numbers N,M and MV (2<= N , M <=100,0<=MV<= 65536) which indicate the size of the map and Y's MV.Then a N*M two-dimensional array follows, which describe the whole map.OutputOutput the N*M map, using '*'s to replace all the grids 'Y' can arrive (except the 'Y' grid itself). Output a blank line after each case.Sample Input
5 3 3 100 ... .E. ..Y 5 6 4 ...... ....PR ..E.PY ...ETT ....TT 2 2 100 .E EY 5 5 2 ..... ..P.. .PYP. ..P.. ..... 3 3 1 .E. EYE ...Sample Output
... .E* .*Y ...*** ..**P* ..E*PY ...E** ....T* .E EY ..*.. .*P*. *PYP* .*P*. ..*.. .E. EYE .*.
bfs扩展四个方向,消耗mv最少优先级最高,加入优先队列,优先扩展mv最高点。保证*扩展到最远边界。
代码150+。。写出来还蛮有成就感的。。
StatusAcceptedTime15msMemory1620kBLength3170LangG++Submitted2017-07-21 00:33:46SharedRemoteRunId21228675#include<stdio.h> #include<string.h> #include<queue> using namespace std;char a[105][105]; int b[105][105]; int t[4][2]={{1,0},{0,1},{-1,0},{0,-1}};struct Node{ int x,y,mv; friend bool operator<(Node a,Node b) { return a.mv<b.mv; } }node;int main() { int t1,n,m,mvp,f,i,j,k,l; priority_queue<Node> q; scanf("%d",&t1); while(t1--){ scanf("%d%d%d",&n,&m,&mvp); for(i=0;i<n;i++){ getchar(); scanf("%s",a[i]); } memset(b,0,sizeof(b)); for(i=0;i<n;i++){ for(j=0;j<m;j++){ if(a[i][j]=='Y'){ b[i][j]=1; node.x=i; node.y=j; node.mv=mvp; q.push(node); while(q.size()){ for(k=0;k<4;k++){ int tx=q.top().x+t[k][0]; int ty=q.top().y+t[k][1]; if(tx<0||ty<0||tx>=n||ty>=m) continue; if(a[tx][ty]=='.'&&b[tx][ty]==0){ if(q.top().mv-1<0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; a[tx][ty]='*'; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; a[tx][ty]='*'; node.x=tx; node.y=ty; node.mv=q.top().mv-1; q.push(node); } else if(a[tx][ty]=='T'&&b[tx][ty]==0){ if(q.top().mv-2<0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; a[tx][ty]='*'; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; a[tx][ty]='*'; node.x=tx; node.y=ty; node.mv=q.top().mv-2; q.push(node); } else if(a[tx][ty]=='R'&&b[tx][ty]==0){ if(q.top().mv-3<0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; a[tx][ty]='*'; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; a[tx][ty]='*'; node.x=tx; node.y=ty; node.mv=q.top().mv-3; q.push(node); } else if(a[tx][ty]=='#'&&b[tx][ty]==0){ b[tx][ty]=1; continue; } else if(a[tx][ty]=='E'&&b[tx][ty]==0){ b[tx][ty]=1; continue; } else if(a[tx][ty]=='P'&&b[tx][ty]==0){ if(q.top().mv-1<=0) continue; f=0; for(l=0;l<4;l++){ int ttx=tx+t[l][0]; int tty=ty+t[l][1]; if(ttx<0||tty<0||ttx>=n||tty>=m) continue; if(a[ttx][tty]=='E'){ b[tx][ty]=1; f=1; continue; } } if(f==1) continue; b[tx][ty]=1; node.x=tx; node.y=ty; node.mv=q.top().mv-1; q.push(node); } } q.pop(); } } } } for(i=0;i<n;i++){ printf("%s\n",a[i]); } printf("\n"); } return 0; }
转载于:https://www.cnblogs.com/yzm10/p/7216135.html
