2139=數據結構實驗之圖論五:從起始點到目標點的最短步數(BFS)

 1 #include<stdio.h>
 2 #include<string.h>
 3 int map[1000][1000],visit[1000];
 4 int step,mark;
 5 int queue[1000];//用來儲存已經遍歷了的數據。
 6 void BFS(int k)
 7 {
 8     int i,o=0,p=0,temp,end=0;//temp用來表示當前所在地。o表示下一步從哪個頂點向下出發。
 9     while(o<=p)//p表示在當前一層內有多少個頂點。
10     {
11         temp=queue[o];
12         for(i=1;i<=k;i++)
13         {
14             if(map[temp][i]==1&&visit[i]==0)
15             {
16                 if(i==1){mark=1;return;}
17                 queue[++p]=i;
18                 visit[i]=1;
19             }
20         }
21         if(o==end)//此時表示該層的所有頂點已經遍歷完畢。
22         {
23             step++;//每遍歷完一層步數+1。
24             end=p;//將下一層最后一個頂點放入遍歷順序中。
25         }
26         o++;//遍歷下個頂點。
27     }
28 }
29 int main()
30 {
31     int k,n;
32     while(scanf("%d %d",&k,&n)!=EOF)
33     {
34         mark=0;//用來標記是否達到近衛軍團所在地。
35         step=1;//用來記錄步數。
36         memset(map,0,sizeof(map));
37         memset(visit,0,sizeof(visit));
38         memset(queue,0,sizeof(queue));
39         int i,a,b;
40         for(i=0;i<n;i++)
41         {
42             scanf("%d %d",&a,&b);
43             map[a][b]=1;//單項路徑。
44         }
45         visit[k]=1;//起點已經搜索過。
46         queue[0]=k;//k作為起點,先儲存好。
47         BFS(k);
48         if(mark==1)printf("%d\n",step);
49         else printf("NO\n");
50     }
51 }

?

轉載于:https://www.cnblogs.com/Angfe/p/10395640.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/278308.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/278308.shtml
英文地址,請注明出處:http://en.pswp.cn/news/278308.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vnc數量限制_通過限制視覺效果在Vista上加速VNC

vnc數量限制This article was written by MetrotekGeek from Metrotek Solutions, a friend of the How-To Geek 本文由Metrotek Solutions的MetrotekGeek撰寫&#xff0c;Metrotek Solutions是How-To Geek的朋友 As a computer field tech, I use the remote desktop program…

思科AP-什么是COS AP?

COS:Click OS 所有新的wave 2 AP都帶有COS。它建立在IOS之上&#xff0c;但behaves 不同。 COS APs是Click OS APs&#xff08;較新的AP型號&#xff0c;Wave 2等&#xff09; 例如&#xff1a;18xx&#xff0c;28xx&#xff0c;38xx&#xff0c;48xx型號Click OS APs或COS AP。…

[轉帖]外殼命名空間擴展

一般介紹 很多人一定用過ZipMagic&#xff0c;對它能把一個壓縮文件映射成文件夾感到很奇怪&#xff0c;不知道它使用了什么技術&#xff0c;實際上它用到的技術就是實現了一個外殼的命名空間擴展&#xff08;Shell Namespace Extention&#xff09;。 文件夾和視圖&#xff1a…

使Safari在Windows Vista上每20秒停止崩潰

The new Safari for Windows is a very slick browser that beats the pants off everything else in the speed department, but it crashes so much on Windows Vista that it’s virtually unusable. 新的Windows版Safari瀏覽器非常流暢&#xff0c;可以超越速度部門的所有…

js----與瀏覽列表有關的對象(瀏覽器對象)

document  location  history  navigator  screen   frame History 對象包含用戶&#xff08;在瀏覽器窗口中&#xff09;訪問過的 URL Location 對象包含有關當前 URL 的信息 Window 對象表示瀏覽器中打開的窗口 Navigator 對象包含有關瀏覽器的信息 轉載于:https:/…

[svc]jdk+tomcat部署.jforum論壇部署

安裝jdk和tomcat jdk1.7.0_13(系列)下載url 我這里用的最新的jdk. 去官網下載即可cd /usr/local/src/ tar xf jdk-8u162-linux-x64.tar.gz -C /usr/local/ ln -s /usr/local/jdk1.8.0_162 /usr/local/jdk tar xf apache-tomcat-8.5.29.tar.gz -C /usr/local/ ln -s /usr/local/…

ipad和iphone切圖_如何從iPhone和iPad上的Mail應用程序刪除電子郵件帳戶

ipad和iphone切圖Nicole Lienemann/Shutterstock妮可利尼曼(Nicole Lienemann)/ ShutterstockWhen you add your Google account to your iPhone or iPad in the Settings app, you’re adding your Gmail account to the Mail app. If you prefer to use third-party email cl…

使用nmcli 實現 bond0 網絡組 網橋三種模式

使用nmcli 實現 bond 網絡組 網橋模式 bond0&#xff08;負載均衡&#xff09; step1&#xff1a;創建一個bond0的主屬nmcli connection add con-name bond0 type bond ifname bond0 mode active-backup 之所以不為綠色是因為還沒有創建從屬&#xff0c;這個bond0相當于一個虛擬…

RabbitMQ是如何運轉的?

前言 之前已經介紹了RabbitMQ交換機模型的相關簡單概念&#xff0c;都是作為此篇的基礎鋪墊&#xff0c;如果對此篇不懂的可以先看我上一篇的介紹認識RabbitMQ交換機模型&#xff0c;或者聯系評論&#xff0c;分享《RabbitMQ實戰指南》電子書給大家&#xff0c;里面雖然有些許錯…

如何種植屢獲殊榮的青豆

Most people don’t know this yet, but I’ve decided to give up computers and become a farmer instead. Since I’m the helpful type, I’ve decided to share everything I know about farming with you, starting with how I won my prize winning green beans. 大多數…

充分利用Microsoft Planner的6種方法

Microsoft Planner is pretty simple to use, but some of its more useful features aren’t front and center. If you’re just creating and moving tasks, here are six ways to get a bit more out of Planner. Here’s everything you need to know. Microsoft Planner的…

IOS - UTF-8轉碼問題

2016.07.06 21:45* 字數 61 閱讀 921評論 0喜歡 2 IOS中提供的轉碼。 [utf8str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 轉碼后發現&#xff0c;與java的不一樣。 原來IOS中轉碼的標準不一致&#xff0c;導致出現錯誤。 不過&#xff0c;可以使用下…

手把手教你如何實現繼承

本文將從最簡單的例子開始&#xff0c;從零講解在 JavaScript 中如何實現繼承。 小例子 現在有個需求&#xff0c;需要實現 Cat 繼承 Animal &#xff0c;構造函數如下&#xff1a; function Animal(name){this.name name }function Cat(name){this.name name } 復制代碼注&a…

最詳細的排序解析,理解七大排序

最詳細的排序解析&#xff0c;理解七大排序 mp.weixin.qq.com點擊上方“方志朋”&#xff0c;選擇“置頂或者星標” 你的關注意義重大&#xff01; 注&#xff1a; lgN在這里為1og2N簡寫 為了方便描述,本文默認用int類型比較&#xff0c;從小到大排序 本文排序算法以java語言…

xp刪除管理員賬戶_在Windows XP中從登錄屏幕刪除用戶帳戶

xp刪除管理員賬戶So you login to your computer every single day, but there’s more than one account to choose from… either because you got the computer from somebody else, or some software package added a user account that you really don’t want to see. So…

Java網絡爬蟲實操(8)

上一篇&#xff1a;Java網絡爬蟲實操&#xff08;7&#xff09; 大家好&#xff0c;本篇文章介紹一下NetDiscovery爬蟲框架里的downloader對象 1) 前言 面向對象設計仍然是目前編程的核心思想&#xff0c;從下面截圖可以了解爬蟲框架的主要對象&#xff1a; 程序在本地組織好一…

Pycharm下將py文件打包成exe文件

1. 在PyCharm下安裝PyInstaller 1. 首先&#xff0c;打開自己要發布的工程 2. 點擊底部的【Terminal】打開終端&#xff0c;中輸入命令pip install pyinstaller后回車&#xff0c;如圖所示進行安裝 3. 輸入命令 pyinstaller&#xff0c;回車顯示安裝成功 4. 輸入命令 pyinstall…

什么是自然語言處理,它如何工作?

NicoElNino/Shutterstock.comNicoElNino / Shutterstock.comNatural language processing enables computers to process what we’re saying into commands that it can execute. Find out how the basics of how it works, and how it’s being used to improve our lives. 自…

GIT速查手冊

為什么80%的碼農都做不了架構師&#xff1f;>>> 一、GIT 1.1 簡單配置 git是版本控制系統&#xff0c;與svn不同的是git是分布式&#xff0c;svn是集中式 配置文件位置 # 配置文件 .git/config 當前倉庫的配置文件 ~/.gitconfig 全局配置文件# 查看所有配置項 git …

4-3邏輯非運算符及案例 4-4

創建類 LoginDemo3 這里取反 !(n%30) package com.imooc.operator; import java.util.Scanner;public class LoginDemo3 {public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println("請輸入一個整數");Scanner scnew Scanner(…