dfs, 枚举, 二维全排列

题意:给定两个整数n, m(<=7)代表矩阵的长和宽, 要求矩阵中每一个元素必须大于或等于其左边和上面的元素, 求出所有可能的情况。

题解:dfs枚举所有可能的情况, 枚举完所有的N*M个格子后就可以增加答案并回溯。

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<cstdlib>
#include<string>
#define INF 0x3f3f3f3f
#define MAXM 100000 + 10
#define MAXN 1000 + 10

using namespace std;
typedef unsigned long long ll;
const ll MOD = 1e9 + 7;

int n, m;
int a[MAXN][MAXN];

int res, ans[MAXN][MAXN];
void dfs(int x, int y)
{
    if(x > n){
        res ++;
        return ;
    }
    if(a[x-1][y] <= 0 && a[x][y-1] <= 0){
        a[x][y] = 0;
        if(y < m){
            dfs(x, y + 1);
        }
        else{
            dfs(x + 1, 1);
        }
    }
    if(a[x-1][y] <= 1 && a[x][y-1] <= 1){
        a[x][y] = 1;
        if(y < m){
            dfs(x, y + 1);
        }
        else{
            dfs(x + 1, 1);
        }
    }
    if(a[x-1][y] <= 2 && a[x][y-1] <= 2){
        a[x][y] = 2;
        if(y < m){
            dfs(x, y + 1);
        }
        else{
            dfs(x + 1, 1);
        }
    }
}

int main()
{
    for(int i = 1; i < 8; i ++){
        for(int j = 1; j < 8; j ++){
            res = 0;
            n = i, m = j;
            dfs(1, 1);
            ans[i][j] = res;
        }
    }

    int t;
    while(~scanf("%d", &t)){
        while(t --){
            int x, y;
            scanf("%d %d", &x, &y);
            printf("%d\n", ans[x][y]);
        }
    }
}

/*

The WAM is F**KING interesting .

*/

/**************************************************************
    Problem: 1233
    User: Bo
    Language: C++
    Result: 正确
    Time:164 ms
    Memory:2020 kb
****************************************************************/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值