杨利霞 发表于 2019-6-28 15:58

2019第十届蓝桥杯B组决赛题解第五题

2019第十届蓝桥杯B组决赛题解第五题
在一个5*5的方格上走边界点,其实也就是6*6的图,从左上角开始走,不走重复点且在12步之内走回左上角点,问方案数
直接dfs,需要减掉 (0,0)->(1,0)->(0,0)和(0,0)->(0,1)->(0,0),这两个路线都重合了
结果: 208-2=206   
代码:
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 3e5+55555;
const ll mod = 998244353;
const double eps = 1e-7;

bool vis;
int ne = {1,0,-1,0,0,1,0,-1};
int ans;

void output() {
    for(int i = 1;i<= 6;i++) {
        for(int j = 1;j<= 6;j++) {
            cout<<vis<<' ';
        }
        cout<<endl;
    }
    cout<<endl<<endl;
}

void dfs(int x,int y,int step) {
    if(step> 6) {
        return ;
    }
    if(x == 1&&y == 1&&vis == true) {
        ans++;
        output();
        return ;
    }
    for(int i = 0;i< 4;i++) {
        int tx = x+ne;
        int ty = y+ne;

        if(x> 6||y> 6||x< 1||y< 1||vis) continue;
        vis = true;
        dfs(tx,ty,step+1);
        vis = false;
    }
    return ;
}

int main() {
    dfs(1,1,0);
    cout<<ans<<endl;

    return 0;
}
---------------------
作者:nka_kun
来源:CSDN


页: [1]
查看完整版本: 2019第十届蓝桥杯B组决赛题解第五题