zoukankan      html  css  js  c++  java
  • War Chess (hdu 3345)

    http://acm.hdu.edu.cn/showproblem.php?pid=3345

     

     

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

     

    Source
     
    主要的是规则:Y  是它的起始点
                 .  需要让 cost 减1
                 T   需要让 cost 减2
                 R  需要让  cost 减3
                 #  不能走
                 P   需要让 cost 减1
                 E  不能走, 但走到它周围的 cost 都要变为 0
                 除了 P 以外能到达的点, 走过后都要变为 * 
     
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #include<queue>
    #include<cmath>
    #include<vector>
    using namespace std;
    
    typedef long long LL;
    
    const int INF = 0x3f3f3f3f;
    const int N =  1100;
    #define met(a, b) memset (a, b, sizeof(a))
    
    struct node
    {
        int x, y, cost;
        bool operator < (const node &a)const
        {
            return cost<a.cost;
        }
    }Y;
    
    int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
    int n, m, vis[N][N];
    char s[N][N];
    
    int Judge(int x, int y)
    {
        if(x>=0 && x<n && y>=0 && y<m)
            return 1;
        return 0;
    }
    
    int Judge1(int x, int y)
    {
        int nx, ny, i;
        for(i=0; i<4; i++)
        {
            nx = x + dir[i][0];
            ny = y + dir[i][1];
            if(Judge(nx, ny) && s[nx][ny]=='E')
                return 0;
        }
        return 1;
    }
    
    void BFS()
    {
        node p, q;
    
        met(vis, 0);
        vis[Y.x][Y.y] = 1;
    
        priority_queue<node>Q;
        Q.push(Y);
    
        while(Q.size())
        {
            p = Q.top(), Q.pop();
            for(int i=0; i<4; i++)
            {
                q.x = p.x + dir[i][0];
                q.y = p.y + dir[i][1];
                if(Judge(q.x, q.y) && !vis[q.x][q.y] && s[q.x][q.y]!='#' && s[q.x][q.y]!='E')
                {
                    if(s[q.x][q.y]=='.' || s[q.x][q.y]=='P')
                        q.cost = p.cost - 1;
                    if(s[q.x][q.y]=='T')
                        q.cost = p.cost - 2;
                    if(s[q.x][q.y]=='R')
                        q.cost = p.cost - 3;
                    if(Judge1(q.x, q.y))
                    {
                        vis[q.x][q.y] = 1;
                        Q.push(q);
                    }
                    if(s[q.x][q.y]!='P'&& q.cost>=0)
                            s[q.x][q.y]='*' ;
                }
            }
        }
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while(T--)
        {
            int i, j, cost;
    
            scanf("%d%d%d", &n, &m, &cost);
    
            for(i=0; i<n; i++)
            {
                scanf("%s", s[i]);
                for(j=0; j<m; j++)
                {
                  if(s[i][j]=='Y')
                        Y.x = i, Y.y = j, Y.cost = cost;
                }
            }
    
            BFS();
    
            for(i=0; i<n; i++)
                printf("%s
    ", s[i]);
            printf("
    ");
        }
        return 0;
    }

     

     
    勿忘初心
  • 相关阅读:
    《设计模式》(精华集)
    TClientDataSet使用(二)
    害我查了半天的错误!av错误,小心Component对象使用Application当Owner
    释放自己
    最近在转C#
    TClientDataSet的使用技巧
    小心使用可修改的常量。
    指数函数和正弦函数相乘
    adb 常用命令
    win7下ie9设置无法保存的问题
  • 原文地址:https://www.cnblogs.com/YY56/p/5436369.html
Copyright © 2011-2022 走看看