hdu 3345 War Chess (bfs+优先队列)

War Chess

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1732    Accepted Submission(s): 416

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ǎ崽

bfs+优先队列,刚开始没有优化,果断超时,第二次竟然因为优先级符号TLE!!(该记得的东西真得记牢)

使用mark数组记录该点MV值大小,初始化为零,搜索时只有当从某个点到达当前点使MV变大时才把该点值更新;入队时判断该点MV值是否大于零,大于则入队。

具体看代码:

#include"stdio.h"
#include"string.h"
#include"queue"
#include"vector"
#include"algorithm"
using namespace std;
#define N 105
#define max(a,b) (a>b?a:b)
int mark[N][N],n,m,v;
int dir[4][2]={0,1,0,-1,-1,0,1,0};
char str[N][N];
struct node
{
    int x,y,d;
    friend bool operator<(node a,node b)
    {
        return a.d<b.d;
    }
};
bool judge(int x,int y)
{
    if(x>=0&&x<n&&y>=0&&y<m)
        return true;
    return false;
}
bool ok(int x,int y)    //判断和(x,y)相邻的是否是敌人
{
    int i,u,v;
    for(i=0;i<4;i++)
    {
        u=dir[i][0]+x;
        v=dir[i][1]+y;
        if(judge(u,v))
        {
            if(str[u][v]=='E')
                return true; //是敌人
        }
    }
    return false;
}
void bfs(int x,int y)
{
    int i,t;
    priority_queue<node>q;
    node cur,next;
    cur.x=x;cur.y=y;cur.d=v;
    q.push(cur);
    memset(mark,-1,sizeof(mark));
    mark[x][y]=v;
    while(!q.empty())
    {
        cur=q.top();
        q.pop();
        for(i=0;i<4;i++)
        {
            next.x=x=dir[i][0]+cur.x;
            next.y=y=dir[i][1]+cur.y;
            if(judge(x,y))
            {
                if(str[x][y]=='.'||str[x][y]=='P')
                    t=cur.d-1;
                else if(str[x][y]=='T')
                    t=cur.d-2;
                else if(str[x][y]=='R')
                    t=cur.d-3;
                else
                    t=-1;
                if(ok(x,y)&&t>0)
                    t=0;        //战斗力减为0
                if(t>0&&t>mark[x][y])
                {
                    next.d=t;
                    q.push(next);
                }
                mark[x][y]=max(mark[x][y],t);
            }
        }
    }
}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&n,&m,&v);
        for(i=0;i<n;i++)
        {
            scanf("%s",str[i]);
        }
        for(i=0;i<n;i++)           //寻找起始位置
        {
            for(j=0;j<m;j++)
            {
                if(str[i][j]=='Y')
                {
                    bfs(i,j);
                    break;
                }
            }
            if(j<m)
                break;
        }
        for(i=0;i<n;i++)      //输出答案
        {
            for(j=0;j<m;j++)
            {
                if(mark[i][j]>=0)
                {
                    if(str[i][j]!='P'&&str[i][j]!='Y')
                        printf("*");
                    else
                        printf("%c",str[i][j]);
                }
                else
                    printf("%c",str[i][j]);
            }
            puts("");
        }
        puts("");
    }
    return 0;
}

hdu 3345 War Chess (bfs+优先队列),布布扣,bubuko.com

时间: 2024-10-19 14:35:32

hdu 3345 War Chess (bfs+优先队列)的相关文章

hdu 3345 War Chess

War Chess Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 5   Accepted Submission(s) : 3 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description War chess is hh's favorite ga

War Chess bfs+优先队列

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 you

HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

题目地址:HDU 1428 先用BFS+优先队列求出所有点到机房的最短距离,然后用记忆化搜索去搜. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #incl

hdu 1242 Rescue (BFS+优先队列)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1242 这道题目我是用BFS+优先队列做的.听说只用bfs会超时. 因为这道题有多个营救者,所以我们从被营救者开始bfs,找到最近的营救者就是最短时间. 先定义一个结构体,存放坐标x和y,还有到达当前点(x,y)消耗的时间. struct node { int x,y; int time; friend bool operator < (const node &a,const node &

hdu 5040 Instrusive【BFS+优先队列】

2014北京网络赛09题,hdu 5040 这次网络赛真是惨,也怪做题策略没想好,当时切完签到题之类的水题之后,马上就去看06青蛙那题去了.结果被那只死青蛙给坑惨了T_T...搞了四小时没搞出来...跪给那只青蛙了...本来当时是准备要做这道题的,题目描述也是好蛋疼,有人说这题不如直接去看Clarification,不看题目了,这也说明这题题目描述确实不清晰,虽然没这么夸张,题目还是得看的. 重新看这道题,不是很难,最短时间的话,那当然想到BFS,纯BFS的话,只是最短步数,所以我们需要+优先队

ZOJ 1649 &amp;&amp; HDU 1242 Rescue (BFS + 优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

[ACM] hdu 1242 Rescue (BFS+优先队列)

Rescue Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is:

hdu 1242 Rescue(bfs+优先队列)

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approach Angel. We assume

hdu 2102 A计划 详细题解 (BFS+优先队列)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2102 这道题属于BFS+优先队列 开始看到四分之一的AC率感觉有点吓人,后来一做感觉就是模板改了点东西而已,一遍就AC了,不过在主函数和全局变量里面都定义了n和m导致我白白浪费了debug的时间.果然全局变量得小心用啊. 跟模板一样的,定义一个结构体,只不过多加了个参数,就是迷宫的层数,我用0代表第一层,1代表第二层,这在数组里面会体现的. struct node { int index;//层数