hdu 3345 war chess 水题 bfs+优先队列

两发编译错误 原因是bfs判断下一个点的时候用了一个变量名叫next

改成next1就ac了 但是心里不爽

气球说他之前告诉我了 然鹅我想了想好像是这么回事

哎呀快下课了写完博客就走

这道题嘛 求得是个最优解吧 也不算是最优解,但是看上去每条路有个优先级,既然是这样,那理所当然用bfs+优先队列啦

(可惜蓝桥报的java啊 留下几天把这些用java敲一遍啊哭哭哭哭嘤嘤嘤)(31msac)

War Chess

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


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
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 .*.
 
Author
shǎ崽
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:   3346  3347  3344  3343  3349
 
 
 
 
 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>

using namespace std;

struct node{
    int x,y,blood;
    friend bool operator<(node n1,node n2){
        return n1.blood < n2.blood;
    }
}cur,nextt;

const int maxn = 110; 
int dr[4][2] = {1,0,0,1,-1,0,0,-1};
char map[maxn][maxn];
int a[maxn][maxn];
int m,n;

int judge_ok(int x,int y){
    if(x < 0||y < 0||x >= n||y >= m){
        return 0;
    }
    if(a[x][y] == -1)
        return -1;
    return 1;
}

int meet_E(int x,int y){
    for(int i = 0;i < 4;i++){
        int cx = x + dr[i][0];
        int cy = y + dr[i][1];
        if(judge_ok != 0){
            if(map[cx][cy] == 'E')
                return 1;
        }
    }
    return 0;
}

void dye(int x,int y){
    if(map[x][y] != 'P')
        map[x][y] = '*';
    return;
    
}

void bfs(int sx,int sy,int maxb){
    int curb = maxb;
    priority_queue<node> q;
    cur.x = sx;
    cur.y = sy;
    cur.blood = maxb;
    q.push(cur);
    int i;
    while(!q.empty()){
        cur = q.top();
        q.pop();
        for(i = 0;i < 4;i++){
            nextt.x = cur.x + dr[i][0];
            nextt.y = cur.y + dr[i][1];
            if(judge_ok(nextt.x,nextt.y) == 1){
                nextt.blood = cur.blood - a[nextt.x][nextt.y];
                if(nextt.blood < 0) continue;
                a[nextt.x][nextt.y] = -1;
                dye(nextt.x,nextt.y);
                if(meet_E(nextt.x,nextt.y) == 0&&nextt.blood > 0)
                    q.push(nextt);
            }
        }
    } 
}

int main(){
    int t,b,sx,sy,i,j;
    
    while(scanf("%d",&t) != EOF){
        while(t--){
            memset(map,0,sizeof(map));
            memset(a,-1,sizeof(a));
            
            scanf("%d%d%d",&n,&m,&b);//input
            for(i = 0;i < n;i++){
                scanf("%s",map[i]);
                for(j = 0;j < m;j++){
                    if(map[i][j] == '.'||map[i][j] == 'P')
                        a[i][j] = 1;
                    else if(map[i][j] == 'Y'){
                        a[i][j] = -1;
                        sx = i;sy = j;
                    }
                    else if(map[i][j] == 'T'){
                        a[i][j] = 2;
                    }
                    else if(map[i][j] == 'R'){
                        a[i][j] = 3;
                    }
                    else if(map[i][j] == 'E'||map[i][j] == '#'){
                        a[i][j] = -1;
                    }
                }
            }
            bfs(sx,sy,b);
            for(i = 0;i < n;i++){
                for(j = 0;j < m;j++){
                    printf("%c",map[i][j]);
                }
                printf("\n");
            }
            printf("\n");
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xuyanqd/p/9035405.html