首页 > 搜索 > BFS搜索 > HDU 3442-Three Kingdoms-优先队列-[解题报告]HOJ
2014
03-23

HDU 3442-Three Kingdoms-优先队列-[解题报告]HOJ

Three Kingdoms

问题描述 :

Three Kingdoms is a funny game. Often Liu Bei is weak and has to run away,so in the game Liu Bei has a skill called "Dunzou". This time you are playing the role of Liu Bei.As Cao Cao’s power is so strong, there is nothing you can do but escaping. Please select an optimal path to achieve the purpose .
Rotation
To simplify the problem, Liu Bei can only move in one of the four direction (up, down,right,left) each time. The map contains the following characters:
‘A’ : Representative of watchtower, which has an attack range of 2(measured by Manhattan distance),and an attack damage of 1.
‘B’ : Representative of Fort, which has an attack range of 3(measured by Manhattan distance),and an attack damage of 2.
‘C’ : Representative of Flame, which has an attack damage of 3 to those who step onto it.
‘D’ : Representative of Archer, which has anattack range of 2(measured by Manhattan distance), and an attack damage of 4.
‘E’ : Representative of Ordinary soldier, which has anattack range of 1(measured by Manhattan distance), and an attack damage of 5.
‘$’ : Representative of Liu Bei.
‘!’ : Representative of Destination.
‘#’ : Representative of obstacles
‘.’ : Representative of floor.
Liu Bei can not enter watchtower, forts, Archers, ordinary soldiers,But he can step onto flame or floor.
Some important rules you should note:
1.  Liu Bei will not be hurt by the same thing twice.For example,if Liu Bei has been hurt by one watchtower before,then he step into the attack range of some watchtower later,he will not be hurt.
2.  When Liu Bei reach the destination,you should first judge the attack damage at the destination then finish the game.
3.  You needn’t judge the attack damage at the start position.
Please choose a path which LiuBei would cost the least HP.

输入:

In the first line there is an integer T, indicates the number of test cases.(T<=60)
In each case,the first line of the input contains two integer n,m(1<=n,m<=50),reperesenting the size of map(n*m).Then follow n lines,each line contain m characters.
There may be some blank lines between each case.

输出:

In the first line there is an integer T, indicates the number of test cases.(T<=60)
In each case,the first line of the input contains two integer n,m(1<=n,m<=50),reperesenting the size of map(n*m).Then follow n lines,each line contain m characters.
There may be some blank lines between each case.

样例输入:

1
4 3
.$.
ACB
ACB
.!.

样例输出:

Case 1: 6



    已被周赛虐成挂零狗。
    BFS + 优先队列 == 最短路,貌似是可以这样理解的。这样得到的结果即为花费最少的一条路。貌似初级计划里面有一个类似的题。
    因为受过的伤不会再受,所以需要抽象出第三维来表示受过的哪几种伤(状压)。寒假集训的时候还给大一的选手出过这种题。。。竟然还有脸给大一的讲题。。

    曼哈顿距离:在欧几里得空间的固定直角坐标系上两点所形成的线段对轴产生的投影的距离总和。

   例如在平面上,坐标(x1, y1)的i点与坐标(x2, y2)的j点的曼哈顿距离为:

   d(i,j)=|X1-X2|+|Y1-Y2|.


#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <stack>

#pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long int
#define _LL __int64
#define _INF 0x3f3f3f3f
#define INF (1<<62)
#define Mod 1000000007

using namespace std;

char Map[51][51];

int hurt[51][51][5];

bool mark[51][51][33];

void Init_Hurt(int n,int m)
{
    int i,j,k,l;

    memset(hurt,0,sizeof(hurt));

    for(i =1;i <= n; ++i)
    {
        for(j = 1;j <= m ; ++j)
        {
            if(Map[i][j] == 'A')
            {
                for(k = i-2;k <= i+2; ++k)
                {
                    for(l = j-2;l <= j+2; ++l)
                    {
                        if(1 <= k && k <= n && 1 <= l && l <= m && abs(k-i)+ abs(l-j) <= 2)
                        {
                            hurt[k][l][0] = 1;
                        }
                    }
                }
            }
            else if(Map[i][j] == 'B')
            {
                for(k = i-3;k <= i+3; ++k)
                {
                    for(l = j-3;l <= j+3; ++l)
                    {
                        if(1 <= k && k <= n && 1 <= l && l <= m && abs(k-i)+ abs(l-j) <= 3)
                        {
                            hurt[k][l][1] = 2;
                        }
                    }
                }
            }
            else if(Map[i][j] == 'C')
            {
                hurt[i][j][2] = 3;
            }
            else if(Map[i][j] == 'D')
            {
                for(k = i-2;k <= i+2; ++k)
                {
                    for(l = j-2;l <= j+2; ++l)
                    {
                        if(1 <= k && k <= n && 1 <= l && l <= m && abs(k-i)+ abs(l-j) <= 2)
                        {
                            hurt[k][l][3] = 4;
                        }
                    }
                }
            }
            else if(Map[i][j] == 'E')
            {
                for(k = i-1;k <= i+1; ++k)
                {
                    for(l = j-1;l <= j+1; ++l)
                    {
                        if(1 <= k && k <= n && 1 <= l && l <= m && abs(k-i)+ abs(l-j) <= 1)
                        {
                            hurt[k][l][4] = 5;
                        }
                    }
                }
            }
        }
    }
}

struct N
{
    int x,y,sta,ans;
    bool operator < (const N &a) const
    {
        return a.ans < ans;
    }
};

int jx[] = {-1, 0, 1, 0};
int jy[] = { 0, 1, 0,-1};

bool Judge(char c)
{
    if(c == '$' || c == 'C' || c == '!' || c == '.')
        return true;
    return false;
}

void bfs(int n,int m,int s,int e)
{
    priority_queue<N> q;

    N f,t;

    f.x = s,f.y = e,f.sta = 0,f.ans = 0;

    memset(mark,false,sizeof(mark));

    mark[s][e][0] = true;

    q.push(f);

    while(q.empty() == false)
    {
        f = q.top();
        q.pop();

        if(f.x != s || f.y != e)
        {
            for(int k = 0;k < 5; ++k)
            {
                if((f.sta&(1<<k)) == 0 && hurt[f.x][f.y][k])
                {
                    f.ans += hurt[f.x][f.y][k];
                    f.sta += (1<<k);
                }
            }
        }

        if(Map[f.x][f.y] == '!')
        {
            printf("%d\n",f.ans);
            return ;
        }

        for(int i = 0;i < 4; ++i)
        {
            t.ans = f.ans;
            t.sta = f.sta;
            t.x = f.x + jx[i];
            t.y = f.y + jy[i];

            if(1 <= t.x && t.x <= n && 1 <= t.y && t.y <= m && mark[t.x][t.y][t.sta] == false && Judge(Map[t.x][t.y]) )
            {
                q.push(t);
                mark[t.x][t.y][t.sta] = true;
            }
        }
    }
    printf("-1\n");
}

void Solve(int n,int m)
{
    int i,j;

    for(i = 1; i <= n; ++i)
    {
        for(j = 1;j <= m; ++j)
        {
            if(Map[i][j] == '$')
            {
                bfs(n,m,i,j);
                return ;
            }
        }
    }
}

int main()
{
    int T,icase = 0;

    int i,j,k,n,m;

    scanf("%d",&T);

    while(T--)
    {
        scanf("%d %d",&n,&m);

        for(i = 1;i <= n ; ++i)
        {
            scanf("%*c%s",Map[i]+1);
        }

        Init_Hurt(n,m);

        printf("Case %d: ",++icase);

        Solve(n,m);

    }
    return 0;
}

参考:http://blog.csdn.net/zmx354/article/details/21708627


  1. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!

  2. Hello Web Admin, I noticed that your On-Page SEO is is missing a few factors, for one you do not use all three H tags in your post, also I notice that you are not using bold or italics properly in your SEO optimization. On-Page SEO means more now than ever since the new Google update: Panda. No longer are backlinks and simply pinging or sending out a RSS feed the key to getting Google PageRank or Alexa Rankings, You now NEED On-Page SEO. So what is good On-Page SEO?First your keyword must appear in the title.Then it must appear in the URL.You have to optimize your keyword and make sure that it has a nice keyword density of 3-5% in your article with relevant LSI (Latent Semantic Indexing). Then you should spread all H1,H2,H3 tags in your article.Your Keyword should appear in your first paragraph and in the last sentence of the page. You should have relevant usage of Bold and italics of your keyword.There should be one internal link to a page on your blog and you should have one image with an alt tag that has your keyword….wait there's even more Now what if i told you there was a simple WordPress plugin that does all the On-Page SEO, and automatically for you? That's right AUTOMATICALLY, just watch this 4minute video for more information at.

  3. 我没看懂题目
    2
    5 6 -1 5 4 -7
    7 0 6 -1 1 -6 7 -5
    我觉得第一个应该是5 6 -1 5 4 输出是19 5 4
    第二个是7 0 6 -1 1 -6 7输出是14 7 7
    不知道题目例子是怎么得出来的