HDU

War chess is hh's favorite game: 
In this game, there is an N * M battle map, and every player has his own Moving Val (MV). In each round, every player can move in four directions as long as he has enough MV. To simplify the problem, you are given your position and asked to output which grids you can arrive. 
 
In the map: 
'Y' is your current position (there is one and only one Y in the given map). 
'.' is a normal grid. It costs you 1 MV to enter in this gird. 
'T' is a tree. It costs you 2 MV to enter in this gird. 
'R' is a river. It costs you 3 MV to enter in this gird. 
'#' is an obstacle. You can never enter in this gird. 
'E's are your enemies. You cannot move across your enemy, because once you enter the grids which are adjacent with 'E', you will lose all your MV. Here “adjacent” means two grids share a common edge. 
'P's are your partners. You can move across your partner, but you cannot stay in the same grid with him final, because there can only be one person in one grid.You can assume the Ps must stand on '.' . so ,it also costs you 1 MV to enter this grid.

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+。。写出来还蛮有成就感的。。 

Status Accepted
Time 15ms
Memory 1620kB
Length 3170
Lang G++
Submitted
Shared
RemoteRunId 21228675

  

#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 ",a[i]); } printf(" "); } return 0; }
原文地址:https://www.cnblogs.com/yzm10/p/7216135.html