dfs两道题

两题很像,但第一题是统计种类数,每行每列都只能放一个(按行搜每个位置,限制条件是当前这列是否以放过),第二题是统计能放的最大位置个数,每行每列可能放多个(直接挨个搜每个位置,限制条件要写一个函数判断)。共同点是可能某个位置能放也会选择不放,这时要注意在最后不判断条件直接dfs下一个位置,但第一个时一整行都不再放,第二个是只一个位置不再放。注意两个都要回溯

 

A - 棋盘问题

在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别。要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘,摆放k个棋子的所有可行的摆放方案C。

Input

输入含有多组测试数据。
每组数据的第一行是两个正整数,n k,用一个空格隔开,表示了将在一个n*n的矩阵内描述棋盘,以及摆放棋子的数目。 n <= 8 , k <= n
当为-1 -1时表示输入结束。
随后的n行描述了棋盘的形状:每行有n个字符,其中 # 表示棋盘区域, . 表示空白区域(数据保证不出现多余的空白行或者空白列)。

Output

对于每一组数据,给出一行输出,输出摆放的方案数目C (数据保证C<2^31)。

Sample Input

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

Sample Output

2
1
#include<string>
#include<iostream>

using namespace std;
int n,k;
int res=0;
string a[10];
int col[10];       //记录一列是否已经放过棋子
void dfs(int start,int num)
{
    int i;
    for(i=0; i<n; i++)
    {
        int flag=0;
        if(a[start][i]=='#'&&col[i]==0)
        {
            col[i]=1;
            num--;
            flag=1;
            if(num==0)    //放的棋子数等于n解决方案加一
                res++;
            if(start+1<n&&num!=0)
                dfs(start+1,num);//搜下一行
            //回溯
            col[i]=0;
            num++;
        }

    }
    if(start+1<n&&num!=0)
        dfs(start+1,num);   //这一行不放搜下一行
}



int main()
{



    while(cin>>n>>k)
    {
        if(n==-1&&k==-1)
            return 0;
        res=0;
        int i,j;

        for(i=0; i<n; i++)
        {

            cin>>a[i];

        }
        dfs(0,k);
        cout<<res<<endl;
    }
}

A - Fire Net

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall.

A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.

Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.

The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4x4) that contain walls through which bullets cannot run through.

The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.



Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.

Input

The input file contains one or more map descriptions, followed by a line containing the number 0 that signals the end of the file. Each map description begins with a line containing a positive integer n that is the size of the city; n will be at most 4. The next n lines each describe one row of the map, with a '.' indicating an open space and an uppercase 'X' indicating a wall. There are no spaces in the input file.

Output

For each test case, output one line containing the maximum number of blockhouses that can be placed in the city in a legal configuration.

Sample Input

4
.X..
....
XX..
....
2
XX
.X
3
.X.
X.X
.X.
3
...
.XX
.XX
4
....
....
....
....
0

Sample Output

5
1
5
2
4
#include<bits/stdc++.h>
using namespace std;
int n;
int maxx=0;
char mp[5][5];
vector<int>v[5];
int check(int row,int col){   //检查上和左面是否放过炮塔,因为按顺序放,因此只检查上和左就可以
    int x=row;
    int y=col;
  /*  while(x<n){
        if(mp[x][col]=='X')
        break;
        if(mp[x][col]=='!')
            return 0;
        x++;
    }
    x=row;*/
    while(x>=0){
        if(mp[x][col]=='X')
             break;
        if(mp[x][col]=='!')
            return 0;
        x--;
    }
    /*while(y<n){
        if(mp[row][y]=='X')
        break;
        if(mp[row][y]=='!')
            return 0;
        y++;
    }
    y=col;*/
    while(y>=0){
        if(mp[row][y]=='X')
        break;
        if(mp[row][y]=='!')
            return 0;
        y--;
    }
    return 1;
}
void dfs(int pos,int sum){ //搜每个位置共n*n个,不是按行搜,因为一行可以放多个,sum统计炮塔个数 
    int x=pos/n;
    int y=pos%n;
    if(check(x,y)==1&&mp[x][y]=='.'){
        mp[x][y]='!';
        sum++;
        if(sum>maxx)
            maxx=sum;
        if(pos+1<n*n)
        dfs(pos+1,sum);          
        sum--;
        mp[x][y]='.';
    }
    if(pos+1<n*n)       //如果能放这个位置也选择不放的情况
    dfs(pos+1,sum);

}
int main()
{

    while(cin>>n,n)
    {
        maxx=0;
        for(int i=0;i<n;i++)
            for(int j=0;j<n;j++)
            {
                cin>>mp[i][j];

            }


        dfs(0,0);
        cout<<maxx<<endl;
    }


}

猜你喜欢

转载自blog.csdn.net/weixin_42165786/article/details/89452714