hdu3345War Chess(bfs,条件很多)

来源:互联网 发布:清涧李家塔镇政府网络 编辑:程序博客网 时间:2024/04/30 12:40

War Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1609    Accepted Submission(s): 378


Problem Description
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.
 

Input
The 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.
 

Output
Output 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
53 3 100....E...Y5 6 4..........PR..E.PY...ETT....TT2 2 100.EEY5 5 2.......P...PYP...P.......3 3 1.E.EYE...
 

Sample Output
....E*.*Y...***..**P*..E*PY...E**....T*.EEY..*...*P*.*PYP*.*P*...*...E.EYE.*.
 
#include <iostream>#include <cstring>#include <queue>using namespace std;struct node{    int x,y,mv;}Tnode[1000015];int r,c,mv,t;int dir[4][2]={1,0,0,1,-1,0,0,-1};char mat[105][105]; int vis[105][105];bool isE(const int &x, const int &y){    int newx,newy;    for(int i=0; i<4; i++){        newx = x + dir[i][0];        newy = y + dir[i][1];        if(newx<0 || newx>r-1 || newy<0 ||newy>c-1) continue;        if(mat[newx][newy]=='E') return true;    }    return false;}int getMv(int mv, int x, int y){    if(mat[x][y]=='R') mv-=3;    else if(mat[x][y]=='T') mv-=2;    else mv-=1;    if(mv>0 && isE(x,y)) mv=0;    return mv;}void bfs(node s){    memset(vis,-1,sizeof(vis));    Tnode[0]=s;    int start=0, end = 1;    node head,next;    vis[s.x][s.y] = s.mv;    while(start<end){        head = Tnode[start++];        if(head.mv<1) continue;        for(int i=0; i<4; i++){            next.x = head.x + dir[i][0];            next.y = head.y + dir[i][1];            if(next.x<0 || next.x>r-1 || next.y<0 || next.y>c-1) continue;            if(mat[next.x][next.y]=='Y' || mat[next.x][next.y]=='#' || mat[next.x][next.y]=='E')                continue;            int tepmv = getMv(head.mv, next.x, next.y);            if(tepmv>vis[next.x][next.y]){                next.mv = tepmv;                vis[next.x][next.y] = tepmv;                if(mat[next.x][next.y]!='P') mat[next.x][next.y]='*';                Tnode[end++] = next;            }        }    }}int main(){    node s;    cin>>t;    while(t--){        cin>>r>>c>>mv;        memset(mat,'\0',sizeof(mat));        for(int i=0; i<r; i++)            for(int j=0; j<c; j++){                cin>>mat[i][j];                if(mat[i][j]=='Y'){                    s.x = i;                    s.y = j;                    s.mv = mv;                }            }        bfs(s);        for(int i=0; i<r; i++)            cout<<mat[i]<<endl;        cout<<endl;    }    return 0;}



0 0
原创粉丝点击