zoukankan      html  css  js  c++  java
  • Pathfinding 模板题 /// BFS oj21413

    题目大意:
    Description

    Bessie is stranded on a deserted arctic island and wants to determine all the paths she might take to return to her pasture. She has tested her boat and knows she can travel from one island to another island in 1 unit of time if a route with proper currents connects the pair.

    She has experimented to create a map of the ocean with valid single-hop routes between each pair of the N (1 ≤ N ≤ 100) islands, conveniently numbered 1..N. The routes are one-way (unidirectional), owing to the way the currents push her boat in the ocean. It's possible that a pair of islands is connected by two routes that use different currents and thus provide a bidirectional connection. The map takes care to avoid specifying that a route exists between an island and itself.

    Given her starting location M (1 ≤ M ≤ N) and a representation of the map, help Bessie determine which islands are one 'hop' away, two 'hops' away, and so on. If Bessie can take multiple different paths to an island, consider only the path with the shortest distance.

    By way of example, below are N=4 islands with connectivity as shown (for this example, M=1):

    start--> 1-------->2
             |         |
             |         |
             V         V
             4<--------3

    Bessie can visit island 1 in time 0 (since M=1), islands 2 and 4 at time 1, and island 3 at time 2.

    The input for this task is a matrix C where the element at row r, column c is named Crc (0 ≤ Crc ≤ 1) and, if it has the value 1, means "Currents enable Bessie to travel directly from island r to island c in one time unit". Row Cr has Nelements, respectively Cr1..CrN, each one of which is 0 or 1.

    Input

    Multiple test cases. For each case:

    * Line 1:   Two space-separated integers: N and M       

    * Lines 2..N+1:   Line i+1 contains N space-separated integers: Cr

    Output

    For each case, output lines 1..???:   Line i+1 contains the list of islands (in ascending numerical order) that Bessie can visit at time i.  Do not include any lines of output after all reachable islands have been listed.

    Sample Input

    4 1
    0 1 0 1
    0 0 1 0
    0 0 0 1
    0 0 0 0

    Sample Output

    1
    2 4
    3

    其实就是一共N个点 从M出发 输入样例时已经把图建好了

    只不过这个图是从1—N 而不是0—N-1罢了

    直接遍历输出就行 注意末尾没有空格 

    #include <bits/stdc++.h>
    #define INF 0x3f3f3f3f
    using namespace std;
    int a[105][105],flag[105];
    int main()
    {
            int n,m;
            while(~scanf("%d%d",&n,&m))
            {
                for(int i=1;i<=n;i++)
                    for(int j=1;j<=n;j++)
                        scanf("%d",&a[i][j]);
    
                memset(flag,INF,sizeof(flag));
                queue <int> q;
                q.push(m);
                flag[m]=0;
                while(!q.empty())
                {
                    m=q.front(); q.pop();
                    for(int i=1;i<=n;i++)
                        if(a[m][i]==1&&flag[i]==INF)
                        {
                            flag[i]=flag[m]+1;
                            q.push(i);
                        }
                }
    
                int sign=0;
                for(int i=0;i<n;i++)
                {
                    sign=0;
                    for(int j=1;j<=n;j++)
                        if(flag[j]==i)
                        {
                            if(sign==0)
                            {
                                printf("%d",j);
                                sign=1;
                            }
                            else printf(" %d",j);
                        }
                    if(sign==1) printf("
    ");
                }
    
            }
    
            return 0;
    }
    View Code
  • 相关阅读:
    c++ 从vector扩容看noexcept应用场景
    c++11-17 模板核心知识(十一)—— 编写泛型库需要的基本技术
    动态链接的PLT与GOT
    c++11-17 模板核心知识(十)—— 区分万能引用(universal references)和右值引用
    Golang性能分析与优化
    c++11-17 模板核心知识(九)—— 理解decltype与decltype(auto)
    [LuoguP4808][CCC 2018]平衡树(数论分块+记忆化搜索)(有复杂度证明)
    [NOI2016]区间(线段树+尺取法)
    [BZOJ4316]小C的独立集(仙人掌+树形DP)
    [CTSC2002]灭鼠行动(模拟)
  • 原文地址:https://www.cnblogs.com/zquzjx/p/8409060.html
Copyright © 2011-2022 走看看