Simple search solution to a problem thinking + + personal record

Simple search solution to a problem thinking + + personal record

A - chessboard

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

Test input comprising a plurality of sets of 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).

Sample Input

2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1

Sample Output

2
1

A DFS title, to a level matrix n, k pawn, with the proviso that each column of each row can only put a pawn. Therefore, we can search for each row of each line, as an n-tier tree, find the next search after a possible node level, if no return to the previous level to continue the search. Code + Notes below.

#include<bits/stdc++.h> 
using namespace std;
char mp[10][10];//绘制地图 
int vis[10];//因为从每行搜,所以只需标记每列是否被访问
int r,c,n,k,sum;//行,列,矩阵大小,棋子个数,方案总和
void dfs(int r,int w){  //r表示搜索当前行,w表示为当前放棋子个数 
	  if(w>=k)//终止条件,如果放完所有棋子,方案数+1 
	  {
		 sum++;//!!!!!此处if(里面两个语句应该大括号,没输大括号导致WA 
	    return;
	  }
	   for(int i=r;i<n;i++)
	   for(int j=0;j<n;j++)
	{
		if(!vis[j]&&mp[i][j]=='#')
		{
			vis[j]=1;//放置该棋子后该列标记
			dfs(i+1,w+1);//!!!!此处应该是搜索i+1行,而不是r+1 
			vis[j]=0;//清楚标记,防止下次访问再放置该列
		}
	}
	return;	    
}
int main(){
	while(cin>>n>>k)
	{
		if(n==-1&&k==-1) break;
		memset(vis,0,sizeof(vis));//每次输入一组数据就将其初始化 
		memset(mp,0,sizeof(mp));
		for(int i=0;i<n;i++)
		   cin>>mp[i];
		   sum=0;
		   dfs(0,0);
		   cout<<sum<<endl;
	}
	return 0;	
}  

Oil collection B-

With the issue of maritime transport of oil spills, a lucrative new industry being born, and that is skimming oil industry. Now, in the Gulf of Mexico massive oil floating, it attracted many businessmen's attention. These business people have a special plane, you can skip the whole poured sea 20 meters 10 meters take such a big rectangle. (Up and down about adjacent or neighboring lattice, can not be placed at an angle) Of course, this requires a spoonful of skim past all the oil, if there are oil poured water, then it is meaningless, completely unable to take advantage of resources . Now, traders want to know, in this area, how much he can get up to scoop oil.

The map is a network of N × N, each grid represents 10m × 10m square area of ​​each region is indicated on an oil or water

Input

Test input comprising a plurality of test data
of the first line gives the number of test data in the test data T (T <75)
for each test sample are digital N (N <50) to indicate the size of the map area, the next N rows, each row has N characters, wherein the symbols '.' represents the sea, the symbol '#' indicates oil.

Output

Output format "Case X: M" (X starting from 1), M is a merchant can get the most amount of oil.

Example 1

Entry

1
6
......
.##...
......
.#..#.
.#..##
......

Export

Case 1: 3

A DFS + math problems, a n-level matrix, the largest oil find, the establishment of an xy coordinate system, easy to know the coordinates of two adjacent up and down and will be for a odd one even, that a fuel must contain two coordinates and is a an odd-even point is known from the following figure, takes a minimum value odd and even number of oil is the
Found that the minimum value of the odd and even to
following code

#include<bits/stdc++.h>
using namespace std;
char s[60][60];
int odd,even,n;
void dfs(int x,int y){
    if (x>=n||y>=n||x<0||y<0||s[x][y]=='.') return;//越界或者无油则返回 
    (x+y)&1?++odd:++even;//如果x+y和为奇数则odd++,偶数even++; 
    s[x][y]='.';//标记 
    dfs(x+1,y);//四个方向搜索 
    dfs(x-1,y);
    dfs(x,y+1);
    dfs(x,y-1);
}
int main(){
    int t,kase=0;
    cin>>t;
    while(t--){
        scanf("%d",&n);
        int ans=0;
        for (int i=0; i<n; ++i) cin>>s[i];//绘图 
        for (int i=0; i<n; ++i)
        for (int j=0; j<n; ++j)
        if (s[i][j]=='#'){
            odd=even=0;//每次都要初始化 
            dfs(i,j);
            ans+=min(odd,even);
        }
        printf("Case %d: %d\n",++kase,ans);
    }
}
Published 18 original articles · won praise 14 · views 371

Guess you like

Origin blog.csdn.net/weixin_45750972/article/details/103212719