7*7矩阵(暴力搜索)

打印一个n*m矩阵,最大为7*7,每个矩阵a[i,j]>=a[i-1,j],并且a[i,j]>=a[i,j-1];

暴力搜索就行,但要一行一行搜索

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <list>
using namespace std;
typedef long long ll;
 
int a[10][10];
int s[10][10];
int n,m,ans;
 
int ok(int x,int y){
    if(x<1 || y<1) return 0;
    return a[x][y];
}
 
void dfs(int x,int y){
    if(x == n+1){//搜索完了
        ans++;
        return;
    }
    if(ok(x-1,y)<=0&&ok(x,y-1)<=0){
        a[x][y] = 0;
        if(y==m) dfs(x+1,1);//一行完了
        else dfs(x,y+1);
    }
    if(ok(x-1,y)<=1&&ok(x,y-1)<=1){
        a[x][y] = 1;
        if(y==m) dfs(x+1,1);
        else dfs(x,y+1);
    }
    if(ok(x-1,y)<=2&&ok(x,y-1)<=2){
        a[x][y] = 2;
        if(y==m) dfs(x+1,1);
        else dfs(x,y+1);
    }
    a[x][y] = ans;
}
 
int main()
{
    int i,j;
    for(i = 1;i<=7;i++)
        for(j = 1;j <= 7;j++){
            ans = 0;
            n = i,m = j;
            dfs(1,1);
            s[i][j] = ans;
        }
    int t;
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        printf("%d\n",s[n][m]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值