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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 2386 Lake Counting

發(fā)布時(shí)間:2023/12/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 2386 Lake Counting 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

鏈接:http://poj.org/problem?id=2386


Lake Counting

Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24263 Accepted: 12246

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.


Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

Source

USACO 2004 November


大意——給你一個(gè)n*m的矩形網(wǎng)格。每一個(gè)格子里面要么是‘W’。要么是‘.’,它們分別表示水和旱地。

如今要你計(jì)算出有多少個(gè)池塘。

每一個(gè)池塘由若干個(gè)水組成,水能連接的方向有8個(gè),僅僅要是能連接到的水都屬于一個(gè)池塘。


思路——一個(gè)簡單的DFS題。我們將所有的網(wǎng)格所有遍歷一次,假如我們找到一個(gè)池塘的源頭,就能夠進(jìn)行一次計(jì)數(shù)。而且將眼下找到的池塘源頭標(biāo)記為‘.’,那么下一次就不會反復(fù)訪問了。再從八個(gè)方向進(jìn)行深搜,將能到達(dá)相鄰的‘W’所有標(biāo)記,以免反復(fù)訪問。這樣最后得到的計(jì)數(shù)就是答案。


復(fù)雜度分析——時(shí)間復(fù)雜度:O(n*m),空間復(fù)雜度:O(n*m)


附上AC代碼:


#include <iostream> #include <cstdio> #include <string> #include <cmath> #include <iomanip> #include <ctime> #include <climits> #include <cstdlib> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <map> //#pragma comment(linker, "/STACK:102400000, 102400000") using namespace std; typedef unsigned int li; typedef long long ll; typedef unsigned long long ull; typedef long double ld; const double pi = acos(-1.0); const double e = exp(1.0); const double eps = 1e-8; const int maxn = 105; const int dir[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {1, -1}, {-1, 1}, {1, 1}}; // 8個(gè)方向 int n, m; // 矩形的行列 char mat[maxn][maxn]; // 矩形void dfs(int x, int y); // 深度優(yōu)先搜索int main() {ios::sync_with_stdio(false);while (~scanf("%d%d", &n, &m)){int cnt = 0;for (int i=0; i<n; i++)scanf("%s", mat[i]);for (int i=0; i<n; i++)for (int j=0; j<m; j++)if (mat[i][j] == 'W'){ // 找到池塘源頭,計(jì)數(shù)并深搜cnt++;dfs(i, j);}printf("%d\n", cnt);}return 0; }void dfs(int x, int y) {mat[x][y] = '.'; // 訪問過了。標(biāo)記for (int i=0; i<8; ++i) // 從八個(gè)方向找相鄰的if (x+dir[i][0]>=0 && x+dir[i][0]<n && y+dir[i][1]>=0 &&y+dir[i][1]<m && mat[x+dir[i][0]][y+dir[i][1]]=='W')dfs(x+dir[i][0], y+dir[i][1]); // 找到相鄰的,繼續(xù)深搜 }

轉(zhuǎn)載于:https://www.cnblogs.com/liguangsunls/p/7054945.html

總結(jié)

以上是生活随笔為你收集整理的POJ 2386 Lake Counting的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。