dfs problem

Search a problem with state constraints

1.POJ-1321

In a checkerboard given shape (the shape may be irregular) placed above the pieces, pieces no difference. The requirements of any two pieces can not be placed in the same row or the same column in the board when the display is required, program for solving a given board size and shape, placing the k pieces of all possible placement scheme C.
Input
Input contains multiple test data.
The first line of each data are two positive integers, NK, separated by a space, and indicates the number of the board will be described in a matrix of n * n, and put the pieces. n <= 8, k <= n
when the end of input is represented by -1 -1.
Then n lines describe the shape of a checkerboard: n characters per line, where # represents the board area, indicates a blank area (extra blank line data is guaranteed not to appear or blank columns).
Output
For each set of data, one line of output is given, the number of output display program C (data guarantee C <2 ^ 31).

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<string.h>
 5 #include<cstdio>
 6 using namespace std;
 7 #define maxn 1000
 8 char a[10][10];
 9 int vis[10];
10 int n,k,m;
11 long long cnt=0;
12 void dfs(int x)//按行遍历
13 {
14     if(m==k)
15     {
16         cnt++;
17         return;
18     }
19     if(x>=n)
20         return ;
21     for(int i=0;i<n;i++)
22     {
23         if(a[x][i]=='#'&&vis[i]==0)
24         {
25             vis[i]=1;
26             m++;
27             dfs(x+1);
28             vis[i]=0;
29             m--;
30         }
31     }
32     dfs(x+1);
33 }
34 int main()
35 {
36     while(cin>>n>>k)
37     {
38     if(n==-1&&k==-1)
39     break;
40     cnt=0,m=0;
41     memset(vis,0,sizeof(vis));
42     for(int i=0;i<n;i++)
43          scanf("%s",&a[i]);
44     dfs(0);
45     cout<<cnt<<endl;
46     }
47 }

Method Two:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<string.h>
 5 #include<cstdio>
 6 using namespace std;
 7 int n,k,cnt=0,m;
 8 char a[10][10];
 9 int vis[10];
10 void dfs(int x)
11 {
12     if(m==k)
13     {
14         cnt++;
15         return;
16     }
17     if(x>=n)
18         return;
19     for(int j=x;j<n;j++)
20     for(int i=0;i<n;i++)
21     {
22         if(a[j][i]=='#'&&vis[i]==0)
23         {
24             vis[i]=1;
25             m++;
26             dfs(x+1);
27             m--;
28             vis[i]=0;
29         }
30     }
31 }
32 int main()
33 {
34     while(cin>>n>>k)
35     {
36         cnt=0,m=0;
37         memset(vis,0,sizeof(vis));
38         if(n==-1&&k==-1)
39             break;
40         for(int i=0;i<n;i++)
41             scanf("%s",&a[i]);
42         dfs(0);
43         cout<<cnt<<endl;
44     }
45 }

II: connectivity issues:

1. Analyzing the number of blocks communication problems

http://ybt.ssoier.cn:8088/problem_show.php?pid=1335

[Sample input]

3 3
1 1 1
0 1 0
1 0 1

[Sample Output]

3

Analysis: FIG three communication block represented by three colors are represented

1 1 1
0 1 0
1 0 1

 

Guess you like

Origin www.cnblogs.com/Aiahtwo/p/11035840.html
Recommended