POJ-2386-Lake counting

Source: Internet
Author: User

 

A simple deep-search question, suitable for practice

1 // question link: http://poj.org/problem? Id = 2386
2 // solution: Define a lake as 1, define a land as 0, and search for each point by DFS.
3 # include <stdio. h>
4 # include <string. h>
5
6 int map [110] [110], vis [110] [110]; // map the simulated map. vis is used to mark whether the map has been accessed.
7 int dir [8] [2] = {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0 },{ 1,-1 },{ 0,-1 },{-1,-1 }}; // eight directions
8 int n, m;
9
10 void DFS (int x, int y ){
11 int I, J;
12 if (Map [x] [Y] = 0 | Vis [x] [Y] = 1) return; // accessed or the current location is blank
13 vis [x] [Y] = 1; // The tag point (x, y) has been accessed.
14 For (I = 0; I <8; I ++ ){
15 DFS (x + dir [I] [0], Y + dir [I] [1]); // recursively accesses eight directions
16}
17}
18
19 int main (){
20 int I, J;
21 char STR [110];
22 int ans;
23 while (scanf ("% d", & N, & M )! = EOF ){
24 memset (MAP, 0, sizeof (MAP); // all grids are initialized to land, including a circle around
25 memset (VIS, 0, sizeof (VIS); // all vertices are initialized to the inaccessible state.
26 For (I = 1; I <= N; I ++ ){
27 scanf ("% s", STR );
28 For (j = 1; j <= m; j ++ ){
29 If (STR [J-1] = 'W') {// 'W' is 1, '.' is 0
30 map [I] [J] = 1;
31}
32 else {
33 map [I] [J] = 0;
34}
35}
36}
37 ans = 0;
38 for (I = 1; I <= N; I ++ ){
39 For (j = 1; j <= m; j ++ ){
40 if (Map [I] [J] = 1 & Vis [I] [J] = 0) {// search all unaccessed points in DFS
41 ans ++;
42 DFS (I, j );
43}
44}
45}
46 printf ("% d \ n", ANS );
47}
48 return 0;

49}

POJ-2386-Lake counting

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.