HDU3345 War Chess (BFS)

War Chess

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


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 rou
nd, 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 

简单的bfs尽可能的理清思路,简化步骤和代码


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

int mv, n, m;
const int maxn = 105;

char mp[maxn][maxn];
char map[maxn][maxn];
int book[maxn][maxn];

struct node
{
    int x, y;
    int mv;
}st, ed;

int dis[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

bool isable(int x, int y)
{
    if(x < 0 || x >= n || y < 0 || y >= m) return 0;
    else return 1;
}

bool islive(node a)
{
    node b;
    for(int i = 0; i < 4; i++)
    {
        b.x = a.x + dis[i][0];
        b.y = a.y + dis[i][1];
        if(!isable(b.x, b.y)) continue;
        if(map[b.x][b.y] == 'E') return 0;
    }
    
    return 1;
}

void bfs()
{
    queue<node>q;
    
    memset(book, -1, sizeof(book));
    q.push(st);
    book[st.x][st.y] = mv;
    while(!q.empty())
    {
        st = q.front();
        q.pop();
        if(st.mv <= 0) continue;
        for(int i = 0; i < 4; i++)
        {
            ed.x = st.x + dis[i][0];
            ed.y = st.y + dis[i][1];
            if(isable(ed.x, ed.y) == 0 || mp[ed.x][ed.y] == 'E' || mp[ed.x][ed.y] == '#' ) continue;
            
            if(map[ed.x][ed.y] == '.') ed.mv = st.mv - 1;
            else if(map[ed.x][ed.y] == 'T') ed.mv = st.mv - 2;
            else if(map[ed.x][ed.y] == 'R') ed.mv = st.mv - 3;
            else if(mp[ed.x][ed.y] == 'P') ed.mv = st.mv - 1;
            
            if(ed.mv <= book[ed.x][ed.y]) continue;
            
            if(islive(ed)) 
            {
                book[ed.x][ed.y] = ed.mv;
                q.push(ed);
            }
            if(mp[ed.x][ed.y] != 'P') mp[ed.x][ed.y] = '*';
        }
    }
    return ;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d %d", &n, &m, &mv);
        for(int i = 0; i < n; i++)
        {
            getchar();
            for(int j = 0; j < m; j++)
            {
                scanf("%c", &map[i][j]);
                mp[i][j] = map[i][j];
                if(mp[i][j] == 'Y')
                {
                    st.x = i; st.y = j;
                    st.mv = mv;
                }
            }
        }
        
        bfs();
        
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < m; j++)
            {
                printf("%c", mp[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/yinghui_yht/article/details/80297500