Pathfinding 模板题 /// BFS oj21413
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.
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
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.
4 1
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0
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的更多相关文章
- Red and Black 模板题 /// BFS oj22063
题目大意: Description There is a rectangular room, covered with square tiles. Each tile is colored eithe ...
- POJ:Dungeon Master(三维bfs模板题)
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16748 Accepted: 6522 D ...
- 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 ...
- hdu1242 又又又是逃离迷宫(bfs模板题)
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/1242/ 这次的迷宫是有守卫的,杀死一个守卫需要花费1个单位的时间,所以以走的步数为深度,在每一层进行搜索,由于走 ...
- 用一道模板题理解多源广度优先搜索(bfs)
题目: //多元广度优先搜索(bfs)模板题详细注释题解(c++)class Solution { int cnt; //新鲜橘子个数 int dis[10][10]; //距离 int dir_x[ ...
- HDU-3549 最大流模板题
1.HDU-3549 Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...
- HDU 4280:Island Transport(ISAP模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...
- AC自动机 - 多模式串匹配问题的基本运用 + 模板题 --- HDU 2222
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- POJ 1459 Power Network(网络最大流,dinic算法模板题)
题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数. 接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...
随机推荐
- 初探gitlab & gitlab-runner & asp.net core持续集成
文章简介 gitlab & gitlab-runner 简介 基于gitlab & gitlab-runner 的asp.net core webapi 极简持续集成实践 gitla ...
- ajax图片上传
使用ajaxfileupload.js插件 html代码: <p> <label>ajax上传</label> <input type="file& ...
- Haproxy负载均衡/动静分离(haproxy各选项详细解释)
在前端领域做负载均衡,动静分离的程序有很多,比较常用的是nginx和Haproxy,今天就说一下 Haproxy在这两方面的表现,文章参考很多网文写成,再加上自己的实验成果,文中所有解释都经过实际环境 ...
- 调用API接口,查询手机号码归属地(1)
使用https://www.juhe.cn/提供的接口,查询归属地 在官网注册key即可使用. 代码如下 #!/usr/bin/python # -*- coding: utf-8 -*- impor ...
- html5本地存储(一)------ web Storage
在html5中与本地存储相关的两个相关内容:Web Storage 与本地数据库 web Storage存储机制是对html4中的cookie存储机制的一个改善.web Storage就是在web上 ...
- B-彻底删除卸载Ubuntu中的MySQL并重新安装(已验证)
Ubuntu-16.04,MySQL-5.7,寻找多篇有关如何彻底卸载删除MySQL的博文, 最终验证下面转发博文真实有效,推荐! https://www.jianshu.com/p/c76b31df ...
- 原型对象(JS中的父类)
原型 prototype 我们所创建的每一个函数,解析器都会向函数中添加 一个属性prototype ,这个属性对应的对象就是我们所谓的原型对象 判断函数中是否含有prototype属性,有则返回 ...
- session之memcache
nginx服务器配置:192.168.200.111[root@nginx ~]# hostname nginx[root@nginx ~]# bash[root@nginx ~]# vim /usr ...
- Mariadb 10.2.8版本GTID主从环境搭建以及切换
1.首先搭建主从 主环境:192.168.1.117 从环境:192.168.1.123 a.首先以二进制包的形式安装好MariaDB (忽略不计) b.配置环境的变量 通配 [mysqld] bin ...
- Echart中X轴数据过多时横向拉动展示
chart.setOption( { tooltip: { trigger: 'axis' }, toolbox: { feature: { saveAsImage: {} } }, grid: { ...