第八届蓝桥杯第4题:方格分割

第八届蓝桥杯第4题:方格分割

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

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

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

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

以位置3,3(格子)为起点相反方向的分割,遇到x == 0 或 6以及y == 0 或 6表示到达边界。
#include<iostream>
#include<cstdio>
#include<cstring>
int mark[10][10];
int count_ = 0;
int direction[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};

using namespace std;

void dfs(int x, int y);

int main() {
  memset(mark, 0, sizeof(mark));
  mark[3][3] = 1;
  dfs(3, 3);
  printf("%d\n", count_ / 4); 
  return 0;
}

void dfs(int x, int y) {
  if (x == 0 || y == 0 || x == 6 || y == 6) {
    count_++;
  }else {
    for (int i = 0; i < 4; i++) {
      int temp_x = x + direction[i][0];
      int temp_y = y + direction[i][1];
      int temp_x2 = 6 - temp_x;
      int temp_y2 = 6 - temp_y;

      if (mark[temp_x][temp_y] == 0 && mark[temp_x2][temp_y2] == 0) {
        mark[temp_x][temp_y] = 1;
        mark[temp_x2][temp_y2] = 2;

        dfs(temp_x, temp_y);

        mark[temp_x][temp_y] = 0;
        mark[temp_x2][temp_y2] = 0;
      }
    }
  }
}

猜你喜欢

转载自blog.csdn.net/zwhsoul/article/details/79702814