Nyoj-27-Number of pools

Source: Internet
Author: User
1 // question address: http://acm.nyist.net/JudgeOnline/status.php? PID = 27
2 // solution idea: bare DFS, traversing each lattice DFS to access the phase grid of the current Lattice
3 # include <stdio. h>
4 # include <string. h>
5
6 int map [110] [110], vis [110] [110];
7 int DX [4] = {0, 0,-1, 1 };
8 int dy [4] = {-1, 1, 0, 0 };
9
10 void DFS (int x, int y ){
11 int I;
12 if (Map [x] [Y] = 0 | Vis [x] [Y] = 1) return; // if the pool has been accessed or does not exist, return
13 vis [x] [Y] = 1;
14 For (I = 0; I <4; I ++ ){
15 DFS (x + dx [I], Y + dy [I]);
16}
17 // DFS (X-1, y );
18 // DFS (x + 1, y );
19 // DFS (X, Y-1 );
20 // DFS (X, Y + 1 );
21}
22
23 int main (){
24 int t, n, m, I, J;
25 int ans;
26 scanf ("% d", & T );
27 while (t --){
28 scanf ("% d", & N, & M );
29 memset (MAP, 0, sizeof (MAP ));
30 memset (VIS, 0, sizeof (VIS ));
31 For (I = 1; I <= N; I ++ ){
32 For (j = 1; j <= m; j ++ ){
33 scanf ("% d", & map [I] [J]);
34}
35}
36 ans = 0;
37 For (I = 1; I <= N; I ++ ){
38 for (j = 1; j <= m; j ++ ){
39 if (Map [I] [J] = 1 & Vis [I] [J] = 0 ){
40 ans ++;
41 DFS (I, j );
42}
43}
44}
45 printf ("% d \ n", ANS );
46}
47 return 0;
48}

Nyoj-27-Number of pools

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.