UVA 572 Oil Deposits oilfield (DFS)

Source: Internet
Author: User

UVA 572 Oil Deposits oilfield (DFS)
Due to recent rains, water has pooled in varous 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 wowould like to figure out how sort 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 Ll eight of its neighbors. given a distriof Farmer John's field, determine how he 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. question: Enter the character matrix of m rows and n columns, and count the number of eight connected blocks consisting of the "W" character. If the two "W" characters are adjacent to the grid (horizontal, vertical, or diagonal), they belong to the same eight-connected block and are obtained through a dual loop. This is the principle of the connected block. Every time you access "W", write the marked number for it to facilitate inspection. AC code:

# Include <cstdio> # include <cstring> const int maxn = 1000 + 5; char tu [maxn] [maxn]; // input graph array int m, n, idx [maxn] [maxn]; // mark the array void dfs (int r, int c, int id) {if (r <0 | r> = m | c <0 | c> = n) return; if (idx [r] [c]> 0 | tu [r] [c]! = 'W') return; idx [r] [c] = id; for (int dr =-1; dr <= 1; dr ++) for (int dc =-1; dc <= 1; dc ++) // search for eight if (dr! = 0 | dc! = 0) dfs (r + dr, c + dc, id);} int main () {int I, j; while (scanf ("% d ", & m, & n) = 2 & m & n) {for (I = 0; I <m; I ++) scanf ("% s ", tu [I]); memset (idx, 0, sizeof (idx); int q = 0; for (I = 0; I <m; I ++) for (j = 0; j <n; j ++) if (idx [I] [j] = 0 & tu [I] [j] = 'W') dfs (I, j, ++ q ); printf ("% d \ n", q);} return 0 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.