图结构练习——BFS——从起始点到目标点的最短步数

图结构练习——BFS——从起始点到目标点的最短步数

Time Limit: 1000MS Memory Limit: 65536KB

Problem Description
在古老的魔兽传说中,有两个军团,一个叫天灾,一个叫近卫。在他们所在的地域,有n个隘口,编号为1..n,某些隘口之间是有通道连接的。其中近卫军团在1号隘口,天灾军团在n号隘口。某一天,天灾军团的领袖巫妖王决定派兵攻打近卫军团,天灾军团的部队如此庞大,甚至可以填江过河。但是巫妖王不想付出不必要的代价,他想知道在不修建任何通道的前提下,部队是否可以通过隘口及其相关通道到达近卫军团展开攻击;如果可以的话,最少需要经过多少通道。由于n的值比较大(n<=1000),于是巫妖王找到了擅长编程的你 =_=,请你帮他解决这个问题,否则就把你吃掉变成他的魔法。为了拯救自己,赶紧想办法吧。

Input
输入包含多组,每组格式如下。
第一行包含两个整数n,m(分别代表n个隘口,这些隘口之间有m个通道)。
下面m行每行包含两个整数a,b;表示从a出发有一条通道到达b隘口(注意:通道是单向的)。

Output
如果天灾军团可以不修建任何通道就到达1号隘口,那么输出最少经过多少通道,否则输出NO。

Example Input
2 1
1 2
2 1
2 1

Example Output
NO
1

#include <bits/stdc++.h>
using namespace std;

int Map[1005][1005], vis[1005];
struct node
{
    int data;
    int step;
};
void BFS(int x)
{
    queue<struct node>q;
    struct node p;
    p.data = x;
    p.step = 0;
    vis[x] = 1;
    q.push(p);
    while(!q.empty())
    {
        struct node p1;
        p1 = q.front();
        q.pop();
        if(p1.data == 1)
        {
            cout << p1.step << endl;
            return ;
        }
        for(int i = 1; i <= x; i++)
        {
            if(Map[p1.data][i] && !vis[i])
            {
                struct node t;
                t.data = i;
                t.step = p1.step + 1;
                q.push(t);
            }
        }
    }
    cout << "NO" << endl;
    return ;
}
int main()
{
    int n, m;
    while(cin >> n >> m)
    {
        memset(Map,0,sizeof(Map));
        memset(vis,0,sizeof(vis));
        while(m--)
        {
            int u, v;
            cin >> u >> v;
            Map[u][v] = 1;
        }
        BFS(n);
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

int Map[1005][1005], vis[1005];
struct node
{
    int data;
    int step;
};
void BFS(int x)
{
    queue<struct node *>q;
    struct node *p;
    p = (struct node *)malloc(sizeof(struct node));
    p -> data = x;
    p -> step = 0;
    vis[x] = 1;
    q.push(p);
    while(!q.empty())
    {
        struct node *p1;
        p1 = (struct node *)malloc(sizeof(struct node));
        p1 = q.front();
        q.pop();
        if(p1 -> data == 1)
        {
            cout << p1 -> step << endl;
            return ;
        }
        for(int i = 1; i <= x; i++)
        {
            if(Map[p1 -> data][i] && !vis[i])
            {
                struct node *t;
                t = (struct node *)malloc(sizeof(struct node);
                t -> data = i;
                t -> step = p1 -> step + 1;
                q.push(t);
            }
        }
    }
    cout << "NO" << endl;
    return ;
}
int main()
{
    int n, m;
    while(cin >> n >> m)
    {
        memset(Map,0,sizeof(Map));
        memset(vis,0,sizeof(vis));
        while(m--)
        {
            int u, v;
            cin >> u >> v;
            Map[u][v] = 1;
        }
        BFS(n);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值