HDU 3345 war chess(bfs + 优先队列)

War Chess

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


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 .*.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>
#define LL long long 
using namespace std;
const int MAXN = 100 + 10;
int n, m, mv;
struct Node
{
    int x, y, mv;
    Node (int xx, int yy, int zz) : x(xx), y(yy), mv(zz){}
    bool friend operator < (const Node& a, const Node& b)
    {
        return a.mv < b.mv;
    }
};
int dir[][2] = { {-1, 0}, {0, 1}, {1, 0}, {0, -1} };
int sx, sy;
int vis[MAXN][MAXN];
char mp[MAXN][MAXN];
bool can(int x, int y)
{
    if(x >= 2 && mp[x-1][y] == 'E') return true;
    if(x < n && mp[x+1][y] == 'E') return true;
    if(y >= 2 && mp[x][y-1] == 'E') return true;
    if(y < m && mp[x][y+1] == 'E') return true;
    return false; 
}
void bfs()
{
    memset(vis, 0 ,sizeof(vis));
    priority_queue<Node> Q;
    Q.push(Node(sx, sy,mv));
    vis[sx][sy] = 1;
    while(!Q.empty())
    {
        Node now = Q.top();
        Q.pop();
        if(now.mv <= 0) continue;
        for(int i=0;i<4;i++)
        {
            Node pre = now;
            pre.x += dir[i][0];
            pre.y += dir[i][1];
            int x = pre.x, y = pre.y;
            if(x < 1 || x > n || y < 1 || y > m) continue;
            if(vis[x][y]) continue;
            if(mp[x][y] == 'E' || mp[x][y] == '#') continue;
            if(mp[x][y] == 'T') pre.mv -= 2;
            else if(mp[x][y] == '.' || mp[x][y] == 'P') pre.mv -= 1;
            else if(mp[x][y] == 'R') pre.mv -= 3;
            if(pre.mv < 0) continue;
			if(can(x, y)) pre.mv = 0;
            vis[pre.x][pre.y] = 1;
            Q.push(pre);
            if(mp[x][y] != 'P') mp[x][y] = '*';
            //cout << x << ' '<< y << ' '<<mb<< endl;
        }
    }
}
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d%d", &n, &m, &mv);
        for(int i=1;i<=n;i++)
        {
            scanf("%s", mp[i] + 1);
            for(int j=1;j<=m;j++)
            {
                if(mp[i][j] == 'Y')
                {
                    sx = i;;
                    sy = j;
                }
            }
        }
        bfs();
        for(int i=1;i<=n;i++)
            printf("%s\n", mp[i] + 1);
        printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值