Problem A: 走道铺砖[减弱版]

Problem A: 走道铺砖[减弱版]

Description
有一个m行n列的矩阵,用1*2的骨牌(可横放或竖放)完全覆盖,骨牌不能重叠,有多少种不同的覆盖的方法?你只
需要求出覆盖方法总数的值即可。

Input
三个整数数n,m
n<=10

Output
一个整数:总数
Sample Input
7 2
Sample Output
21
//用dfs暴力搜索

直接上代码

#include<bits/stdc++.h>
using namespace std;
int x,y,ans=0;
bool used[20][20];
void find(int xx,int yy)
{
    if(xx==x+1)     {
        ans++;
        return;
    }
    if(used[xx][yy]==false)     {
        if(yy==y) find(xx+1,1);
        else find(xx,yy+1);
    }
    if(used[xx][yy]==true)     {
        if(yy<y)         {
            if(used[xx][yy+1]==true)             {
                used[xx][yy]=false;
                used[xx][yy+1]=false;
                if(yy+1==y) find(xx+1,1);
                else find(xx,yy+2);
                used[xx][yy+1]=true;
                used[xx][yy]=true;
            }
        }
        if(xx<x)         {
            if(used[xx+1][yy]==true)             {
                used[xx+1][yy]=false;
                used[xx][yy]=false;
                if(yy==y) find(xx+1,1);
                else find(xx,yy+1);
                used[xx+1][yy]=true;
                used[xx][yy]=true;
            }
        }
    }
}
int main()
{
    cin>>x>>y;
    memset(used,true,sizeof(used));
    find(1,1);
    cout<<ans;
}

猜你喜欢

转载自blog.csdn.net/qq_41654438/article/details/81428670