日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SDUTOJ3468_广度优先搜索练习之神奇的电梯(BFS + 用vector建图)

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SDUTOJ3468_广度优先搜索练习之神奇的电梯(BFS + 用vector建图) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 廣度優先搜索練習之神奇的電梯

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Time Limit:?1000 ms?Memory Limit:?65536 KiB

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Submit?Statistic

Problem Description

有一座已知層數為n的高樓,這座高樓的特殊之處在于只能靠電梯去上下樓,所以要去到某一層要非常耽誤時間,然而更悲哀的是,這座高樓的電梯是限號的,小鑫最開始的時候在1層,他想去第x層,問題是他最起碼要經過多少層(包含第x層)才能到達第x層。

Input

多組輸入。
第一行是三個正整數n,m,q。分別代表樓的總層數,給定的m條信息和q次查詢。
接下來的m行,每行的第一個整數pos代表這是第pos層的電梯,第二個數代表從這一層可以去的樓層總共有num個,之后的num個數字代表從第pos層代表可以去的樓層。
最后的q行,每行一個整數代表小鑫想去的樓層號碼。
1<=m,pos,num<=n<=200
1<=q<=20

Output

對于每次詢問輸出一個整數,占一行。代表如果要去某個樓層最少要經過多少層,如果到不了的話就輸出-1。

Sample Input

10 4 3 1 2 6 7 3 4 4 6 8 10 5 2 2 3 7 3 10 5 6 4 5 9

Sample Output

5 3 -1

Hint

?

Source

Casithy

?

第一次使用STL庫的vector (容器) 可變數組 創建圖

關于vector的詳細操作 請看另一篇博客

https://blog.csdn.net/Cherishlife_/article/details/85859846

?

#include <bits/stdc++.h> using namespace std; struct node {int num; // 樓層int step; // 次數 }; vector<int> gra[205]; // 用vector 創建圖 (可變數組 比稀疏鄰接矩陣更節省空間) int cat[205]; // 標記數組 int n, m, q; int bfs(int i, int wantto) {cat[i] = 1;node p = {i, 1};queue<node> q;q.push(p);while (!q.empty()){node temp = q.front();q.pop();if (temp.num == wantto)return temp.step;vector<int>::iterator it; // it指針指向第一個元素for (it = gra[temp.num].begin(); it < gra[temp.num].end(); it++){node t = {*it, temp.step + 1};if (!cat[*it]){cat[*it] = 1;q.push(t);}}/*****************或者如下寫法*******************/// for (int i = 0; i < gra[temp.num].size(); i++)// {// node t = {gra[temp.num][i], temp.step + 1};// if (!cat[gra[temp.num][i]])// {// cat[gra[temp.num][i]] = 1;// q.push(t);// }// }}return -1; } int main() {int num, pos, to;while (cin >> n >> m >> q){for (int i = 0; i < 205; i++) // 每次初始化否則必錯gra[i].clear();for (int i = 0; i < m; i++){cin >> pos >> num;while (num--){cin >> to;gra[pos].push_back(to); // 這句等價于 gra[pos][cnt++] = to;}}int wantto;while (q--){cin >> wantto;memset(cat, 0, sizeof(cat));cout << bfs(1, wantto) << endl;}}return 0; }

?

轉載于:https://www.cnblogs.com/iQXQZX/p/10258739.html

總結

以上是生活随笔為你收集整理的SDUTOJ3468_广度优先搜索练习之神奇的电梯(BFS + 用vector建图)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。