蓝桥杯之方格填数

相关知识点请参考
算法专题之DFS
DFS详解

标题:方格分割
6x6的方格,沿着格子的边线剪开成两部分。 要求这两部分的形状完全相同。
包括这3种分法在内,一共有多少种不同的分割方法。 注意:旋转对称的属于同一种分割法。
这里写图片描述

这里写图片描述

这里写图片描述
请提交该整数,不要填写任何多余的内容或说明文字。

#include<iostream>
#include<cstring>

using namespace std;

bool book[10][10];//记录状态数组
int cnt = 0;

void dfs(int x,int y)
{
    int next[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};//定义方向数组,便于向四个方向拓展
    int tx,ty;
    if(x == 0 || y == 0 || x == 6 || y == 6)//判断边界
    {
        cnt++;//到达边界,数量加1
        return ;
    }

    for(int k = 0 ; k <= 3 ; k++)//向四个方向拓展
    {
        tx = x + next[k][0];
        ty = y + next[k][1];
        if(book[tx][ty] == 1)//如果标记走过,跳出此次循环
            continue;
        book[tx][ty] = 1;
        book[6 - tx][6 - ty] = 1;
        dfs(tx,ty);//如果成立,在此点继续搜索
        book[tx][ty] = 0;
        book[6 - tx][6 - ty] = 0;
    }
}

int main()
{
    memset(book, 0,sizeof(book));
    book[3][3] = 1;
    dfs(3,3);//在中心点(3,3)开始拓展
    cout << cnt / 4 << endl;//由于图形是中心对称,因此要除以4
}

猜你喜欢

转载自blog.csdn.net/ancientear/article/details/79703127