题目大意:
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("\n");}}return 0;
}

View Code

转载于:https://www.cnblogs.com/zquzjx/p/8409060.html

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. poj2449 K短路模板题

    昨晚看WC论文发现自己连K短路的经典A*算法还不会,补了一波,模板题输出-1后没return继续跑wa了一早上...... 算法流程: ①在反向图中求出t到每个点的最短路 ②从原点bfs,估价f=d+ ...

  3. 个人算法题精简导航整理(精炼汇总,含知识点、模板题、题单)

    文章目录 前言 导航 注意事项 技巧类 自定义Pair 排序 N维数组转一维 位运算 状态压缩 算法基础 枚举 √ 指数型枚举 排列型枚举 组合型枚举 左右区间枚举 模拟 √ 日期天数问题:平年闰年情 ...

  4. 图论-有向图的连通性模板题(hdu1296)(hdu1827)

    1.强连通分量: 强连通分量可以理解为边数最少的情况下是一个环. 这里写了一个模板题用的是tarjan算法,当然还有其他算法. tarjan算法的关键其实还是对于num数组和low数组的使用 然后可以 ...

  5. P1339 热浪 最短路径模板题

    这么naive的题面一看就是最短路模板题~~~ ok.首先是floyd算法,tts,记得把k放在最外面就行了. 1 #include <cstdio> 2 #include <cst ...

  6. HDU2255 奔小康赚大钱(km模板题)

    Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.         这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好有n家老百姓, ...

  7. HDU1166 敌兵布阵(树状数组模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. 数单词 (AC自动机模板题)

    数单词 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 为了能够顺利通过英语四六级考试,现在大家每天早上都会早起读英语. LYH本来以为自己在6月份的考试中可以通过六级, ...

  9. CSP认证201509-4 高速公路[C++题解]:强连通分量、tarjan算法模板题

    题目分析 来源:acwing 分析: 所求即为强连通分量的个数,然后计算每个强连通分量中点的个数,相加即可. 所谓强连通分量,它是一个子图,其中任意两点可以相互到达,并且再加一个点,就不能满足任意两点 ...

最新文章

  1. pxe安装linux后命令不可用,CentOS7下的PXE无人值守系统安装(亲测成功)
  2. mysql中pager命令妙用
  3. Struts2 注解中跳转 action
  4. Codeforce - 920C- Swap Adjacent Elements 排序|思维
  5. 下拉加载更多--判断页面距离
  6. 【leetcode】【动态规划】最长回文子序列
  7. Python+OpenCV:理解支持向量机(SVM)
  8. 手机算通用计算机还是,通用盾是一种u盾。手机和电脑都可以用。问一...
  9. 威纶通触摸屏上传错误_威纶通触摸屏程序怎么上传?
  10. 2019,我们被“黑”科技薅过的羊毛?
  11. 基于R语言的主成分分析
  12. Linux重置root 密码
  13. mfc对话框ok没效果_利用PS制作逼真双重曝光效果案例演示,合成紫色城市建筑风格海报图片...
  14. EDM电商邮件营销模板设计规范流程
  15. 巨头秀区块链肌肉 原生军机会何在?
  16. 计算机网络的常见面试题
  17. c51语言中数据的存储类型,C51-数据存储类型
  18. Attention-over-Attention模型
  19. 案例: 场景:比如 在项目中显示所有项目/某用户的所有项目Controller中 ulr对应的名字是一致
  20. 微信小程序css篇----1.边框(Border)

热门文章

  1. SEO网站优化真的比其他网络推广方式好吗,优势在哪里?
  2. 【技术美术】千人千面如何炼成 技术讲解捏脸系统设计原理
  3. 编程题目:使用C++语言模拟完成一个简单的计算机系统
  4. 计算机专业吐槽搞笑,大学里那些专业搞笑描述,简直太形象了!网友:逗死我了!...
  5. 2022年2月最受欢迎编程语言排行榜|Python遥遥领先
  6. verilog中always和initial的区别
  7. 程序设计综合实践——京东管理系统(C语言实现)
  8. 不在上学了能不能考计算机二级,好多学生都问到底要不要参加计算机二级考试?...
  9. python求sinx近似值_c语言求sinx的近似值 c语言求sinx近似值
  10. JDK、JER、JVM三者间的联系与区别