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

InputThe 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.OutputOutput 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 <cstdio>
#include <queue>
using namespace std;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
int t,n,m,val,head,tail,tx,ty,d;
char map[101][101];

int vis[101][101];
struct que
{
    int x,y,mv;
    friend bool operator <(que a,que b)
    {
        return a.mv<b.mv;
    }
}cur;
int judge(int x,int y)
{
    if(x<0||y<0||x>=n||y>=m)return 0;
    return 1;
}
int check(int x,int y)
{
    for(int i=0;i<4;i++)
    if(judge(x+dir[i][0],y+dir[i][1])&&map[x+dir[i][0]][y+dir[i][1]]==‘E‘)return 1;
    return 0;
}
int check1(int x,int y)
{
    if(vis[x][y]<0)return 0;
    if(map[x][y]==‘P‘||map[x][y]==‘Y‘)return 0;
    return 1;
}
int main()
{
    priority_queue <que>q;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&val);
        head=tail=0;
        memset(vis,-1,sizeof(vis));
        for(int i=0;i<n;i++)
        scanf("%s",map[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map[i][j]==‘Y‘){
                cur.x=i,cur.y=j;
                cur.mv=val;
                q.push(cur);
                vis[i][j]=val;
                break;}
            }
        }

        while(!q.empty())
        {
            for(int i=0;i<4;i++)
            {
                tx=q.top().x+dir[i][0];
                ty=q.top().y+dir[i][1];
                if(!judge(tx,ty)||map[tx][ty]==‘#‘||map[tx][ty]==‘E‘)continue;
                if(map[tx][ty]==‘T‘)d=q.top().mv-2;
                else if(map[tx][ty]==‘R‘)d=q.top().mv-3;
                else d=q.top().mv-1;
                if(check(tx,ty)&&d>0)d=0;
                if(d>vis[tx][ty])
                {
                    vis[tx][ty]=d;
                    if(d>0)
                    {cur.x=tx;
                    cur.y=ty;
                    cur.mv=d;
                    q.push(cur);}
                }
            }
            q.pop();
        }

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(check1(i,j))putchar(‘*‘);
                else putchar(map[i][j]);
            }
            cout<<endl;
        }
        cout<<endl;
    }
}
时间: 2024-11-01 08:18:41

War Chess bfs+优先队列的相关文章

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, an

Battle City BFS+优先队列

Battle City Many of us had played the game "Battle city" in our childhood, and some people (like me) even often play it on computer now. What we are discussing is a simple edition of this game. Given a map that consists of empty spaces, rivers,

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

hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1392 : War Chess 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Rainbow loves to play kinds of War Chess games. There are many famous War Chess games such as "Biography of Cao Cao", "Anecdotes of Wagang Mountain", etc. In this problem, let's c

[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 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 2102 A计划 详细题解 (BFS+优先队列)

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

poj 1724 ROADS (bfs+优先队列)

题目链接 题意:在有费用k限制的条件下,求从1到n的最短距离,如果最短距离相同求费用最小的,边为有向边,其中可能有 多个相同的源点和目标点,但是距离和费用不同. 分析:用bfs和邻接表来把每一个边搜一下,因为用了优先队列,所以先到n的一定是最小的 . 1 #include <iostream> 2 #include <cstring> 3 #include <cstdlib> 4 #include <cmath> 5 #include <cstdio&