题意:给定两个整数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
****************************************************************/