Solution of pressure dp

Digital dp solution:

A. Special checkerboard

  • Title: Put a car on the checkerboard in \ (n \ times n \)n , some of the squares cannot be placed, and seek the total number of solutions that prevent them from attacking each other. Note: There can only be one car in the same row or column, otherwise they will attack each other.

  • analysis:

    • First of all, there can only be one car per line, as long as each line is placed one line at a time to ensure that there is no attack between the same line.
    • Secondly, only one car can be placed in each column, as long as it can record which column has a car, it will be no longer necessary to consider this column next time.
    • Use a binary number Sto indicate whether a car has been placed in a column. For example n=5, S=01101, it means that the first, third, and fourth columns (starting from the lower position) have placed cars. We finally find S=11111the number of solutions in the state .
    • The definition f[s]represents the snumber of scenarios in the state . \ (s \ in [0 \ sim 2 ^ n-1] \)
      • Suppose s=01101, obviously:f[01101]= f[01100]+f[01001]+f[00101]
      • But what if there is an obstacle at the current location? So when we put the car again, we judge whether there is an obstacle in the current position. See the code for specific implementation.
  • Code:

    #include<bits/stdc++.h>
    const int maxn=(1<<20)-1;
    typedef long long LL;
    LL f[maxn],a[25];//a[x]记录第x行的障碍状态
    int lowbit(int x){return x & -x;};
    int main(){
        int n,m;scanf("%d%d",&n,&m);
        for(int i=1;i<=m;++i){
            int x,y;scanf("%d%d",&x,&y);
            a[x]+=1<<(y-1);
        }
        f[0]=1;//一个也不放也是一种方案
        int maxs=1<<n;//最大的状态
        for(int S=1;S<maxs;++S){//从状态1开始枚举
            int cnt=0;//计算状态S里的1的个数,几个1说明处理到第几行
            for(int i=S;i;i-=lowbit(i))cnt++;
            for(int i=S;i;i-=lowbit(i)){//依次判断当前状态每一个1
                if(!(a[cnt] & lowbit(i))){//状态S当前的1所在列如果没有障碍
                    int s=S^lowbit(i);//说明lowbit(i)处可以放车,当前就在此处放车
                    f[S]+=f[s];//s<S,保证能转移
                }
            }
        }
        printf("%lld\n",f[maxs-1]);
        return 0;
    }
    

B. Non-aggression

  • Meaning of the questions: in \ (n \ times n \) board which put na king, so that they do not attack each other, put the total number of species program. The king can attack it up, down, left, right, top left, bottom right, top right, bottom right, and a grid in each of the eight directions nearby, a total of grids.
  • analysis:
    • This question is very similar to the example corn field we talked about. There are fewer obstacles, but the number limit is increased. Generally, if there is one more limit, we will increase the state of one dimension.
    • Let the state of placing the king on the dp[ i ][ j ][ k ]first i row be the jnumber kof plans on the board .
    • The stage is obvious, that is, we can process it line by line, the first line is special, we can preprocess
    • Judgment conflict: suppose sthe current line s'is the previous line.
      • Row conflicts, that is, can not be adjacent, just judge (s & (s<<1))==0, non-zero means there is adjacent.
      • Column conflict:
        • Upside-down conflict: judge (s & s')==0, if not 0, it means upside-down conflict.
        • Upper left conflict: judge ((s<<1) & s')==0, if not 0, explain the conflict.
        • Upper right conflict: judge ((s>>1) & s')==0, if not 0, explain the conflict.
      • To determine 1the number of states in the current state , use lowbit.

Guess you like

Origin www.cnblogs.com/hbhszxyb/p/12710188.html