方格分割 2017蓝桥杯C++题解

方格分割 2017蓝桥杯C++题解

描述

标题:方格分割

6x6的方格,沿着格子的边线剪开成两部分。
要求这两部分的形状完全相同。

如图:p1.png, p2.png, p3.png 就是可行的分割法。

试计算:
包括这3种分法在内,一共有多少种不同的分割方法。
注意:旋转对称的属于同一种分割法。

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

方格分割 2017蓝桥杯C++题解

方格分割 2017蓝桥杯C++题解

方格分割 2017蓝桥杯C++题解

思路

从中心出发,向四周深搜,最后结果除以四

代码

#include<iostream>
#include<cstring>

using namespace std;
int book[10][10];
int dire[4][2] = { -1,0,1,0,0,-1,0,1 };
const int N = 6;
int ans;
void dfs(int x, int y)
{
	if (x == 0 || y == N || x == N || y == 0) {
		ans++;
		return;
	}
	for (int i = 0; i < 4; i++)
	{
		int nx = x + dire[i][0];
		int ny = y + dire[i][1];
		if (nx<0 || nx>N || y<0 || ny>N) continue;
		if (!book[nx][ny])
		{
			book[nx][ny] = 1;
			book[N - nx][N - ny] = 1;
			dfs(nx, ny);

			book[nx][ny] = 0;
			book[N - nx][N - ny] = 0;
		}
	}
}
int main()
{
	book[N / 2][N / 2] = 1;
	dfs(N / 2, N / 2);
	cout << ans / 4 << endl;
	return 0;
}

//最后结果 509

本人也是新手,也是在学习中,勿喷

转载请注明出处

欢迎有问题的小伙伴一起交流哦~