题目大意:
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[][],flag[];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%d",&a[i][j]); memset(flag,INF,sizeof(flag));
queue <int> q;
q.push(m);
flag[m]=;
while(!q.empty())
{
m=q.front(); q.pop();
for(int i=;i<=n;i++)
if(a[m][i]==&&flag[i]==INF)
{
flag[i]=flag[m]+;
q.push(i);
}
} int sign=;
for(int i=;i<n;i++)
{
sign=;
for(int j=;j<=n;j++)
if(flag[j]==i)
{
if(sign==)
{
printf("%d",j);
sign=;
}
else printf(" %d",j);
}
if(sign==) printf("\n");
} } return ;
}

Pathfinding 模板题 /// BFS oj21413的更多相关文章

  1. Red and Black 模板题 /// BFS oj22063

    题目大意: Description There is a rectangular room, covered with square tiles. Each tile is colored eithe ...

  2. POJ:Dungeon Master(三维bfs模板题)

    Dungeon Master Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16748   Accepted: 6522 D ...

  3. POJ-2251 Dungeon Master (BFS模板题)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  4. hdu1242 又又又是逃离迷宫(bfs模板题)

    题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...

  5. 用一道模板题理解多源广度优先搜索(bfs)

    题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...

  6. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  7. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  8. AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

随机推荐

  1. python爬虫 mac下安装使用Fiddler

    HTTP代理工具Fiddler Fiddler是一款强大Web调试工具,它能记录所有客户端和服务器的HTTP请求. Getting started 在安装之前需要准备Mono环境 If you don ...

  2. MySql精简

    安装的是免安装版MySql 由于MySql是开源的,故下载的时候源码也会包含,如果单纯只是使用其功能,则可以将这些文件删除为MySql减肥 可以删除的文件有如下: 1.mysql-test 文件夹: ...

  3. 使用并行ssh提高工作效率

    我们经常需要ssh到多个主机上执行相同的命令,为了提高效率,我们通常会自己写个脚本,循环遍历执行我们的命令,比如: for host in `cat hosts.txt`;do ssh usernam ...

  4. 1.6 flux介绍

    这一节将介绍 React 的核心应用架构模式 Flux,包括内容: Flux 介绍 MVC 架构之痛 Flux 的理解 Flux 相关库和工具介绍 Flux 与 React 实例 最后我们将会把之前的 ...

  5. 1、Locust压力测试环境搭建

    环境准备:阿里云服务器一台.python2.7.pip Locust 介绍 Locust 是一个开源负载测试工具.使用 Python 代码定义用户行为,也可以仿真百万个用户. Locust 简单易用, ...

  6. Linux源码与编译出的目标文件汇编代码的一致性问题

    start_kernel是内核启动时比较重要的一个函数,然而我发现一个问题,我编译出来的目标文件中的汇编代码与C源码并不完全对应,这是怎么一回事呢? asmlinkage void __init st ...

  7. MySQL数据库(三)—— 表相关操作(二)之约束条件、关联关系、复制表

    表相关操作(二)之约束条件.关联关系.复制表 一.约束条件  1.何为约束 除了数据类型以外额外添加的约束 2.约束条件的作用 为了保证数据的合法性,完整性 3.主要的约束条件 NOT NULL # ...

  8. python :Django url /views /Template 文件介绍

    1,Django URL 路由系统 URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL模式以及要为该URL模式调用的视图函数之间的映射表:你就是以这种方式告诉Django ...

  9. JUC源码分析-集合篇(九)SynchronousQueue

    JUC源码分析-集合篇(九)SynchronousQueue SynchronousQueue 是一个同步阻塞队列,它的每个插入操作都要等待其他线程相应的移除操作,反之亦然.SynchronousQu ...

  10. C# WinfForm 控件之dev报表 XtraReport (四) 动态绑定主从关系表

    一般的单据都是由主从关系的,比如部门与人员.单据表头与表身.仓库与存货.分类与档案等等 所以主从关系是报表用的最多的 1.准备数据库 简单方便 --主表 create table RdRecord ( ...