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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

洛谷 P1596 [USACO10OCT]Lake Counting S-dfs

發布時間:2023/12/4 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 洛谷 P1596 [USACO10OCT]Lake Counting S-dfs 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目描述

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.

由于近期的降雨,雨水匯集在農民約翰的田地不同的地方。我們用一個NxM(1<=N<=100;1<=M<=100)網格圖表示。每個網格中有水(‘W’) 或是旱地(’.’)。一個網格與其周圍的八個網格相連,而一組相連的網格視為一個水坑。約翰想弄清楚他的田地已經形成了多少水坑。給出約翰田地的示意圖,確定當中有多少水坑。
輸入格式

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.

第1行:兩個空格隔開的整數:N 和 M 第2行到第N+1行:每行M個字符,每個字符是’W’或’.’,它們表示網格圖中的一排。字符之間沒有空格。
輸出格式

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

一行:水坑的數量

輸入:

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

輸出:

3

解題思路:
找到一個W,從這個W開始dfs,把8個方向的W都消除,然后繼續找W,計算次數。

代碼如下:

#include <iostream> using namespace std; const int N = 110; int ans;int dx[] = {0, 0, 1, -1, 1, -1, 1, -1};int dy[] = {1, -1, 0, 0, -1, -1, 1, 1}; char g[N][N]; int n, m;void dfs(int x, int y) {g[x][y] = '.';for (int i = 0; i < 8; i++) {int xx = x + dx[i], yy = y + dy[i];if (xx >= 0 && xx < n && yy >= 0 && yy < m && g[xx][yy] == 'W') {dfs(xx, yy);}} }int main() {cin >> n >> m;for (int i = 0; i < n; i++)cin >> g[i];for (int i = 0; i < n; i++)for (int j = 0; j < m; j++) {if (g[i][j] == 'W') {dfs(i, j);ans++;}}cout << ans << endl;return 0; }

總結

以上是生活随笔為你收集整理的洛谷 P1596 [USACO10OCT]Lake Counting S-dfs的全部內容,希望文章能夠幫你解決所遇到的問題。

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