Tile Style Blue Bridge Zhenti

The old routine is to find the blank position in the upper left corner every time it recurses. This place must be filled. If you can't put a suitable tile, you can return directly to have a strong pruning effect.

The parameter for recursion is the paved area. If it is full, the answer can be accumulated after judging the weight.

 

#include <bits/stdc++.h>
using namespace std;
const int maxn=20;

map <int,bool> mp;
int book[maxn][maxn];
int n=3,m=10,sum=30;

void getpos(int &x,int &y)
{
    int i,j;
    for(i=1;i<=n;i++){
        for(j=1;j<=m;j++){
            if(book[i][j]==-1){
                x=i,y=j;
                return;
            }
        }
    }
}

bool judgeII(int x,int y)
{
    if(1<x&&1<y){
        if(book[x-1][y-1]==book[x-1][y]&&book[x-1][y]==book[x][y]&&book[x][y]==book[x][y-1]) return 0;
        else return 1;
    }
    else return 1;
}

bool judgeI(int x,int y)
{
    return judgeII(x,y)&&judgeII(x,y+1)&&judgeII(x+1,y)&&judgeII(x+1,y+1);
}

int dfs(int area)
{
    int res,i,j,x,y,val,cnt;
    if(area==sum){
        val=0,cnt=0;
        for(i=1;i<=n;i++){
            for(j=1;j<=m;j++){
                val+=book[i][j]*(1<<cnt);
                cnt++;
            }
        }
        if(!mp[val]){
            mp[val]=1;
            return 1;
        }
        else return 0;
    }
    getpos(x,y);
    //printf("*%d %d*\n",x,y);
    res=0;
    if(x<n&&book[x+1][y]==-1){
        book[x][y]=book[x+1][y]=0;
        if(judgeI(x,y)&&judgeI(x+1,y)){
            res+=dfs(area+2);
        }
        book[x][y]=book[x+1][y]=1;
        if(judgeI(x,y)&&judgeI(x+1,y)){
            res+=dfs(area+2);
        }
        book[x][y]=book[x+1][y]=-1;
    }
    if(y<m&&book[x][y+1]==-1){
        book[x][y]=book[x][y+1]=0;
        if(judgeI(x,y)&&judgeI(x,y+1)){
            res+=dfs(area+2);
        }
        book[x][y]=book[x][y+1]=1;
        if(judgeI(x,y)&&judgeI(x,y+1)){
            res+=dfs(area+2);
        }
        book[x][y]=book[x][y+1]=-1;
    }
    return res;
}

int main()
{
    memset(book,-1,sizeof(book));
    printf("%d\n",dfs(0));
    return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325477281&siteId=291194637