不简单的bfs

bfs记录路径   https://blog.csdn.net/chenguolinblog/article/details/8164657

 

题目1 : Saving Tang Monk II

时间限制:1000ms

单点时限:1000ms

内存限制:256MB

描述

《Journey to the West》(also 《Monkey》) is one of the Four Great Classical Novels of Chinese literature. It was written by Wu Cheng'en during the Ming Dynasty. In this novel, Monkey King Sun Wukong, pig Zhu Bajie and Sha Wujing, escorted Tang Monk to India to get sacred Buddhism texts.

During the journey, Tang Monk was often captured by demons. Most of demons wanted to eat Tang Monk to achieve immortality, but some female demons just wanted to marry him because he was handsome. So, fighting demons and saving Monk Tang is the major job for Sun Wukong to do.

Once, Tang Monk was captured by the demon White Bones. White Bones lived in a palace and she cuffed Tang Monk in a room. Sun Wukong managed to get into the palace, and he wanted to reach Tang Monk and rescue him.

The palace can be described as a matrix of characters. Different characters stand for different rooms as below:

'S' : The original position of Sun Wukong

'T' : The location of Tang Monk

'.' : An empty room

'#' : A deadly gas room.

'B' : A room with unlimited number of oxygen bottles. Every time Sun Wukong entered a 'B' room from other rooms, he would get an oxygen bottle. But staying there would not get Sun Wukong more oxygen bottles. Sun Wukong could carry at most 5 oxygen bottles at the same time.

'P' : A room with unlimited number of speed-up pills. Every time Sun Wukong entered a 'P' room from other rooms, he would get a speed-up pill. But staying there would not get Sun Wukong more speed-up pills. Sun Wukong could bring unlimited number of speed-up pills with him.

Sun Wukong could move in the palace. For each move, Sun Wukong might go to the adjacent rooms in 4 directions(north, west,south and east). But Sun Wukong couldn't get into a '#' room(deadly gas room) without an oxygen bottle. Entering a '#' room each time would cost Sun Wukong one oxygen bottle.

Each move took Sun Wukong one minute. But if Sun Wukong ate a speed-up pill, he could make next move without spending any time. In other words, each speed-up pill could save Sun Wukong one minute. And if Sun Wukong went into a '#' room, he had to stay there for one extra minute to recover his health.

Since Sun Wukong was an impatient monkey, he wanted to save Tang Monk as soon as possible. Please figure out the minimum time Sun Wukong needed to reach Tang Monk.

输入

There are no more than 25 test cases.

For each case, the first line includes two integers N and M(0 < N,M ≤ 100), meaning that the palace is a N × M matrix.

Then the N×M matrix follows.

The input ends with N = 0 and M = 0.

输出

For each test case, print the minimum time (in minute) Sun Wukong needed to save Tang Monk. If it's impossible for Sun Wukong to complete the mission, print -1

样例输入

2 2
S#
#T
2 5
SB###
##P#T
4 7
SP.....
P#.....
......#
B...##T
0 0

样例输出

-1
8
11

点的标记,好好看看。以后再详细讲解一下。

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

char mp[110][110];
bool vis[110][110][10000];
int d[4][2] = { { 1,0 },{ -1,0 },{ 0,1 },{ 0,-1 } };
int x, y;

struct node
{
    int x, y;
    int step;
    int o;
    bool operator < (const node &a) const
    {
        return step > a.step;
    }
};


int bfs(node st)
{
    priority_queue<node> q;
    st.step = 0;
    st.o = 0;
    q.push(st);
    node now, next;

    while (!q.empty())
    {
        now = q.top();
        q.pop();

        for (int i = 0; i < 4; i++)
        {
            next = now;
            next.step++;
            next.x += d[i][0];
            next.y += d[i][1];
            if (next.x < 0 || next.y < 0 || next.x >= x || next.y >= y)
                continue;
            if (next.o == 0 && mp[next.x][next.y] == '#')
                continue;

            if (mp[next.x][next.y] == 'T')
            {
                return next.step;
            }

            if (mp[next.x][next.y] == 'P')
                next.step--;
            else if (mp[next.x][next.y] == '#')
            {
                next.step++;
                next.o--;
            }
            else if (mp[next.x][next.y] == 'B')
            {
                if(next.o < 5)
                    next.o++;
            }

            if (vis[next.x][next.y][next.o]==1)
                continue;


            vis[next.x][next.y][next.o] = 1;
            q.push(next);
        }
    }
    return -1;
}

int main()
{
    while (cin >> x >> y && (x || y))
    {
        memset(vis, 0, sizeof(vis));
        node st;
        for (int i = 0; i < x; i++)
            for (int j = 0; j < y; j++)
            {
                cin >> mp[i][j];
                if (mp[i][j] == 'S')
                {
                    st.x = i;
                    st.y = j;
                }
            }
        cout << bfs(st) << endl;
    }

    return 0;
}

Problem D

Lost in Translation

The word is out that you’ve just finished writing a book entitled

How to Ensure Victory at a

Programming Contest

and requests are flying in. Not surprisingly, many of these requests are

from foreign countries, and while you are versed in many programming languages, most spoken

languages are Greek to you. You’ve done some investigating and have found several people who

can translate between languages, but at various costs. In some cases multiple translations might

be needed. For example, if you can’t find a person who can translate your book from English to

Swedish, but have one person who can translate from English to French and another from French to

Swedish, then you’re set. While minimizing the total cost of all these translations is important to you,

the most important condition is to minimize each target language’s distance (in translations) from

English, since this cuts down on the errors that typically crop up during any translation. Fortunately,

the method to solve this problem is in Chapter 7 of your new book, so you should have no problem

in solving this, right?

 

 

4 6

Pashto French Amheric Swedish

English Pashto 1

English French 1

English Amheric 5

Pashto Amheric 1

Amheric Swedish 5

French Swedish 1

 

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
char str[10005], str1[10005];
int Map[105][105];
map<string, int>mp;
struct node
{
    int num, step, w;

    bool operator<(const node &b)const
    {
        if(step==b.step)
            return w>b.w;   //相反
        else
            return step>b.step;
    }
};
int vis[105];
int bfs(int ans)
{
    int sum=0;
    memset(vis, 0, sizeof(vis));
    priority_queue<node>q;
    q.push((node)
    {
        0, 0, 0
    });
    while(!q.empty())
    {
        node now=q.top();
        q.pop();
        if(vis[now.num]==1)    //注意状态!!!!
            continue;
        vis[now.num]=1;
        sum+=now.w;
        for(int i=1; i<ans; i++)
        {
            if(Map[now.num][i]!=INF)
            {
                q.push((node)
                {
                    i, now.step+1, Map[now.num][i]
                });
            }
        }
    }
    for(int i=1; i<ans; i++)
        if(vis[i]==0)
            return -1;

    return sum;
}
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        int ans=0;
        mp["English"]=ans++;
        while(n--)
        {
            scanf("%s", str);
            mp[str]=ans++;
        }

        int w;
        memset(Map, INF, sizeof(Map));
        for(int i=0; i<m; i++)
        {
            scanf("%s %s %d", str, str1, &w);
            if(Map[mp[str]][mp[str1]]>w)
                Map[mp[str1]][mp[str]]=Map[mp[str]][mp[str1]]=w;
        }
        int i=bfs(ans);
        if(i!=-1)
            printf("%d\n", i);
        else
            printf("Impossible\n");
    }
    return 0;
}


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

z6q6k6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值