搜尋1(廣度優先)Red and Black
Red and Black
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 14 Accepted Submission(s) : 13
Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to one of four adjacent tiles. But he can't move on red tiles, he can move only on black tiles.Write a program to count the number of black tiles which he can reach by repeating the moves described above.
Input The input consists of multiple data sets. A data set starts with a line containing two positive integers W and H; W and H are the numbers of tiles in the x- and y- directions, respectively. W and H are not more than 20. There are H more lines in the data set, each of which includes W characters. Each character represents the color of a tile as follows. '.' - a black tile '#' - a red tile '@' - a man on a black tile(appears exactly once in a data set)
Output For each data set, your program should output a line which contains the number of tiles he can reach from the initial tile (including itself).
Sample Input 6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#
Sample Output 45 59 6 13
我理解的搜尋就是從一個起始點按某種規律對地圖上的每個可檢索點進行判斷(規律如:按前後左右走),直到滿足情況時跳出或全部檢索完畢;
下面是我這題的程式碼:
#include<iostream> #include<string> #include<string.h> #include<sstream> #include<queue> using namespace std; struct node { int x,y; }s; //s為起點 int n,m,h; char a[20][20]; //地圖 int dir[4][2]={{1,0},{0,1},{0,-1},{-1,0}};//走的方向 void bfs() { queue<node>q; node t,x1; q.push(s); while (!q.empty()) { t=q.front(); q.pop(); for (int i=0;i<4;i++) //4個方向搜尋,滿足條件入隊 { x1.y=t.y+dir[i][1]; x1.x=t.x+dir[i][0]; if(x1.x>=0&&x1.x<m&&x1.y>=0&&x1.y<n&&a[x1.x][x1.y]=='.') { h++; a[x1.x][x1.y]='#'; //檢索的標記防止多次計算 q.push(x1); } } } cout<<h<<endl; } int main() { int i,j; while (cin>>n>>m,n+m) { h=1; for (i=0;i<m;i++) //輸入地圖找到起點 for (j=0;j<n;j++) { cin>>a[i][j]; if (a[i][j]=='@') { s.x=i; s.y=j; } } bfs(); } return 0; }
相關推薦
搜尋1(廣度優先)Red and Black
Red and Black Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submission(s) : 14 Accepted Su
c語言實現按層次(廣度優先)非遞歸遍歷二叉鏈樹
child str sizeof att col std 二叉樹 頭結點 oot 1 #include<stdio.h> 2 #include<conio.h> 4 #include<malloc.h> 5 typedef cha
演算法----圖的遍歷(深度優先搜尋DFS、廣度優先搜尋BFS)
圖的遍歷的定義:從圖的某個頂點出發訪問圖中所有的點,且每個頂點僅被訪問一次。 深度優先搜尋DFS: 準備:指定的起始點和終點,確定好當前點與鄰接點之間的偏移值、結束搜尋的條件、符合訪問的點所需條件、回溯處理; (1)若當前點的鄰接點有未被訪問的,則選一個進行訪問; (2)若當前點的鄰接點都不符合訪問條
廣度優先搜尋BFS(洛谷)
ACM題集:https://blog.csdn.net/weixin_39778570/article/details/83187443 深度優先搜尋DFS(洛谷) P1162 填塗顏色 題目:https://www.luogu.org/problemnew/show/P1162 題意:
1091. Acute Stroke (30)-PAT甲級真題(廣度優先搜尋)
1091. Acute Stroke (30)One important factor to identify acute stroke (急性腦卒中) is the volume of the str
Catch That Cow poj 4001(抓住那頭牛!) BFS(廣度優先搜尋)
題目連結 其實就是求最短路徑問題,採用BFS即可。 AC程式碼如下: #include <iostream> #include <queue> using namespace std; const int maxn=2e5+5; bool vis[maxn];
DFS(深度優先搜尋),BFS(廣度優先搜尋)小總結
這周正在學習DFS和BFS。 體驗中覺得這兩個演算法還是挺好用的,但是遇到某些資料大的情況就很容易超時。(所以到後面還是得學習一下如何優化,或者採用更佳的搜尋方法來解決問題) 然後學習了一段時間,感覺基本上了解了DFS和BFS的基礎實現原理以及應用(不過我認為還是得通過做題來培養自己的感覺,
圖的遍歷(廣度優先搜尋遍歷)
1、廣度優先搜尋遍歷過程 (1)從某個頂點V出發,訪問該頂點的所有鄰接點V1,V2..VN (2)從鄰接點V1,V2...VN出發,再訪問他們各自的所有鄰接點 (3)重複上述步驟,直到所有的頂點都被訪問過 若此時圖中還有頂點未被訪問,則在外控演算法的控制下,另選一
藍橋杯 第十題 分酒 (BFS(廣度優先搜尋)
分酒 泊松是法國數學家、物理學家和力學家。他一生致力科學事業,成果頗多。有許多著名的公式定理以他的名字命名,比如概率論中著名的泊松分佈。 有一次閒暇時,他提出過一個有趣的問題,後稱為:“泊松分酒”。在我國古代也提出過類似問題,遺憾的是沒有進行徹底探索,其中
演算法之BFS(廣度優先搜尋演算法)
</pre><pre> #include<stdio.h> /*有8個城市,編號分別為0~7,求從0號城市到7號城市的最短路線*/ int jz[8][8]= {
Hrbust oj 1316 移動 II——BFS(廣度優先搜尋)
移動 IITime Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 487(147 users)Total Accepted: 219(137 users)Rating: Special Judge: NoDescriptio
ZOJ—— 2165 Red and Black(搜尋)
題目: Problem Description There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is
HDU 2102 A計劃(廣度優先搜尋+資料)
A計劃 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 17222 Accepted Submissi
無權最短路徑BFS(廣度優先搜尋)演算法(圖論)
廣度優先搜尋(BFS)演算法類似於樹中的層次搜尋: 從任意點s出發,先遍歷與s相鄰的點,然後再遍歷於相鄰的點相鄰的點。注意有向圖必須是順方向的鄰接點。 為什麼說廣度優先搜尋可以用來求無權最短路徑呢?因為,廣度優先搜尋每次都會先發現距離s為k的所有頂點,然後才會 發現距離s
Red and Black(搜尋題目)
Red and Black Time Limit: 1000MSMemory Limit: 32768KB64bit IO Format: %I64d & %I64u Submit Status Description There is a rectangula
POJ 1979 Red and Black(簡單DFS)
either www enter ont false num present direction roo Red and Black Description There is a rectangular room, covered with square tile
Find a way (廣度優先搜索)
需要 sam namespace 比較 建立 繼續 tip total lag 題目: Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo o
HDU 1312 Red and Black(bfs,dfs均可,個人傾向bfs)
spec int ger time scrip follow stdio.h stack line 題目代號:HDU 1312 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 Red and Black Time Li
迷宮問題的求解(廣度優先搜索)
struct -o font 使用 sin 將在 地圖 數據 c++ 迷宮問題很容易可以理解為廣度優先搜索問題,站在一個點上,首先試一試自己周圍的點是否可以走,如果是路則加入待走隊列,如果是墻則丟棄。迷宮問題在廣度優先搜索的時候需要特別註意的就是要及時拋棄,遇到走
資料結構實現 3.1:二分搜尋樹(C++版)
資料結構實現 3.1:二分搜尋樹(C++版) 1. 概念及基本框架 2. 基本操作程式實現 2.1 增加操作 2.2 刪除操作 2.3 查詢操作 2.4 遍歷操作 2.5 其他操作 3. 演算法複雜度分