car

car

Description

Place n cars on the n*n (n≤20) square chessboard (you can attack the row and column), and some squares cannot be placed. Find the total number of solutions that can not attack each other.

Input

The size of the chessboard in the first line n
The number of obstacles in the
second line m The third line to m+3 are m obstacles

Output

total

Sample Input

4
2
1 1
2 2

Sample Output

14

Thought:
for ai a_iaiIndicates the status of the i-th row, 1 is barrier, and 0 is barrier-free. How does it transfer?

a[i]+=1 << (y - 1);
注意:它是在边数人边输出的,x,y表示障碍的位置。

Then we set f[i] to represent the total number of options with the most i cars selected. Is that based on the principle of addition, we add all the states before the current state to a total, is that the maximum value of f[i]?

for(ll i=1,c,j ; i<(1 << n) ; i++)
{
    
    
	for(c=0,j=i ; j ; j-=(j & -j),c++);
	for(j=i&~a[c]; j; j-=(j&-j)) f[i]+=f[i^(j&-j)];
} 
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#define ll long long
#define fre(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout);

using namespace std;

const ll MAX=2147483647;
const ll N=1 << 20;

ll n, m, x, y, a[N], f[N]={
    
    1};
int main()
{
    
    
	scanf("%lld%lld",&n, &m);
	for(ll i=1; i<=m; i++) scanf("%lld%lld", &x, &y), a[x]+=1 << (y - 1);
	for(ll i=1, c, j; i<(1 << n) ; i++)
	  {
    
    
		for(c=0, j=i ; j ; j-=(j & -j),c++);
		for(j=i&~a[c]; j; j-=(j&-j)) f[i]+=f[i^(j&-j)];
	  }  
	printf("%lld",f[(1 << n) - 1]);
	return 0;
} 

Guess you like

Origin blog.csdn.net/bigwinner888/article/details/108094339
car