码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3345 War Chess

时间:2016-08-06 19:00:45      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

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

自己写的spfa代码:
注意输出的每行后面都有一个空行,我就错了presentation error。。。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

struct node
{
    int x,y;
    node(int a,int b){x=a; y=b;}
};
int dr[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int n,m,mv,t,sx,sy;
int dis[105][105];
bool vis[105][105];
char mp[102][102];

bool work(int x,int y)
{
    if(mp[x][y]==Y) return 0;
    for(int i=0;i<4;i++)
    {
        int xx=x+dr[i][0];
        int yy=y+dr[i][1];
        if(xx<0 || xx>=n || yy<0 || yy>=m) continue;
        if (mp[xx][yy]==E) return 1;
    }
    return 0;
}
void spfa()
{
    queue<node> Q;
    memset(dis,-1,sizeof(dis));
    memset(vis,0,sizeof(vis));
    Q.push(node(sx,sy));
    dis[sx][sy]=mv;
    vis[sx][sy]=1;
    while(!Q.empty())
    {
        node p=Q.front();
        vis[p.x][p.y]=0;
        Q.pop();
        if (work(p.x,p.y)) dis[p.x][p.y]=0;
        if (dis[p.x][p.y]>0)
        for(int i=0;i<4;i++)
        {
            int xx=p.x+dr[i][0];
            int yy=p.y+dr[i][1];
            char ch=mp[xx][yy];
            if(xx<0 || xx>=n || yy<0 || yy>=m) continue;
            if(ch==#) continue;
            if(ch==.|| ch==T || ch==R)
            {
                int k;
                if (ch==.) k=1; else
                if (ch==T) k=2; else
                if (ch==R) k=3;
                if (dis[xx][yy]>=dis[p.x][p.y]-k) continue;
                dis[xx][yy]=dis[p.x][p.y]-k;
                if(!vis[xx][yy])
                {
                    Q.push(node(xx,yy));
                    vis[xx][yy]=1;
                }
            }
            if(ch==P && dis[p.x][p.y]>1)
            {
                 if (dis[xx][yy]>=dis[p.x][p.y]-1) continue;
                 dis[xx][yy]=dis[p.x][p.y]-1;
                 if(!vis[xx][yy])
                 {
                    Q.push(node(xx,yy));
                    vis[xx][yy]=1;
                 }
            }
        }
    }
}
int main()
{
    scanf("%d",&t);
    for(;t>0;t--)
    {
        scanf("%d%d%d",&n,&m,&mv);
        for(int i=0;i<n;i++)
        {
            scanf("%s",&mp[i]);
            for(int j=0;j<m;j++)
            if (mp[i][j]==Y) sx=i,sy=j;
        }

        spfa();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if (dis[i][j]<0) printf("%c",mp[i][j]);
                else
                {
                    if (mp[i][j]==E || mp[i][j]==P || mp[i][j]==Y) printf("%c",mp[i][j]);
                    else printf("*");
                }
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

 




转自:http://www.bkjia.com/cjjc/857812.html
/*
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=0&&x=0&&yq;
    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=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

标签:

原文地址:http://www.cnblogs.com/stepping/p/5744565.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!