Meter Garlic-A1139 dfs

On a  grid map of n \ times m n × m, some squares are placed with bombs. After manually detonating a bomb, the bomb will detonate all the bombs in the row and column where the bomb is located, and the detonated bomb can detonate other bombs, which will continue to chain.

 

 

Now, in order to detonate all the bombs on the map, you need to manually detonate some of them. In order to minimize the danger, please calculate how many bombs can be detonated manually.

Input format

Enter two integers n, m n , m in the first line  , separated by spaces.

In the next  n n lines, enter a character string of length m m in each line to  represent the map information. Means no bomb, means bomb.01

Data convention:

For  the data of 60 \% 6 0 %: 1 \ le n, m \ le 100 1 n , m 1 0 0;

For data of  100 \% 1 0 0 %: 1 \ le n, m \ le 1000 1 n , m 1 0 0 0;

The amount of data is relatively large, cininput is not recommended .

Output format

An integer is output, indicating the minimum number of bombs that need to be manually detonated.

Sample input

5 5
00010
00010
01001
10001
01000

Sample output

 

2

 

answer:

You must first understand that if you ignite the No. 1 bomb for the first time to detonate the No. 2, 3, 4, 5, 6 bomb, then the first time you ignite the No. 2 bomb can also detonate the No. 1, 3, 4, 5, No. 6 bomb, the same is true for bombs No. 3, 4, 5, 6

 

Then we randomly pick a bomb to ignite it first, delete the bomb ignited by this bomb, and then select one from the remaining bombs, and proceed in turn until we know that there is no bomb

 

Code:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<math.h>
 6 #include<vector>
 7 #include<queue>
 8 #include<map>
 9 using namespace std;
10 typedef long long ll;
11 const int maxn=1010;
12 const int INF=0x3f3f3f3f;
13 int Map[maxn][maxn];
14 int m,n,ans=0;
15 int row[maxn];
16 int col[maxn];
17 int dfs(int i,int j)
18 {
19     Map[i][j]=0;
20     if(!col[j])
21     {
22         col[j]=1;
23         for(int k=1; k<=n; k++)
24         {
25             if(Map[k][j]==1)
26             {
27                 dfs(k,j);
28                 //Map[k][j]=0;
29             }
30 
31         }
32     }
33     if(!row[i])
34     {
35         row[i]=1;
36         for(int l=1; l<=m; l++)
37         {
38             if(Map[i][l]==1)
39             {
40                 dfs(i,l);
41                 //Map[i][l]=0;
42             }
43  
44          }
 45      }
 46  }
 47  int main ()
 48  {
 49      scanf ( " % d% d " , & n, & m);
 50      for ( int i = 1 ; i <= n; i ++ )
 51      {
 52          for ( int j = 1 ; j <= m; j ++ )
 53          {
 54              scanf ( " % 1d " , & Map [i] [j]);    // % 1d means just input a number and end the input 
55          }
56     }
57     for(int i=1; i<=n; i++)
58     {
59         for(int j=1; j<=m; j++)
60         {
61             if(Map[i][j]==1)
62             {
63                 dfs(i,j);
64                 ans++;
65             }
66         }
67     }
68     printf("%d\n",ans);
69     return 0;
70 }

 

Guess you like

Origin www.cnblogs.com/kongbursi-2292702937/p/12736008.html