2017年浙江中医药大学大学生程序设计竞赛(重现赛)H - 剪纸

题目描述

DD wants to send a gift to his best friend CC as her birthday is coming. However, he can’t afford expensive gifts, and he is so lazy that he is not willing to do complex things. So he decides to prepare a paper cut for CC’s birthday gift, which symbolizes their great friendship~~
DD has a square colored paper which consists of n*n small squares. Due to his tastes, he wants to cut the paper into two identical pieces. DD also wants to cut as many different figures as he can but each sheet can be only cut once, so he asks you how many sheets does he need to prepare at most.
for example:

输入描述:

Input contains multiple test cases.
The first line contains an integer T (1<=T<=20), which is the number of test cases.Then the first line of each test case contains an integer n (1<=n<10).

输出描述:

The answer;
示例1

输入

1
4

输出

11

题解

暴力搜索。

因为是两个一模一样的图形,所以肯定中心对称,所以从中心出发,同时走两条相反方向的路即可。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 15;
int T, n;
int f[maxn][maxn];
int ans;
int dir[4][2] = {
  {1, 0},
  {-1, 0},
  {0, 1},
  {0, -1},
};

bool check(int x, int y) {
  if(x == 0 || x == n || y ==0 || y == n) return 1;
  return 0;
}

bool out(int x, int y) {
  if(x >= 0 && x <= n && y >= 0 && y <= n) return 0;
  return 1;
}

void dfs(int x1, int y1, int x2, int y2) {
  if(check(x1, y1) && check(x2, y2)) {
    ans ++;
    return ;
  }
  for(int i = 0; i < 4; i ++) {
    int tx1 = x1 + dir[i][0];
    int ty1 = y1 + dir[i][1];
    int tx2 = x2 - dir[i][0] ;
    int ty2 = y2 - dir[i][1];
    if(f[tx1][ty1]) continue;
    if(f[tx2][ty2]) continue;
    if(out(tx1, ty1)) continue;
    if(out(tx2, ty2)) continue;
    f[tx1][ty1] = 1;
    f[tx2][ty2] = 1;
    dfs(tx1, ty1, tx2, ty2);
    f[tx1][ty1] = 0;
    f[tx2][ty2] = 0;
  }
}

int main() {
  scanf("%d", &T);
  while(T --) {
    scanf("%d", &n);

    if(n % 2 == 1) {
      printf("0\n");
      continue;
    }
    for(int i = 0; i <= n; i ++) {
      for(int j = 0; j <= n; j ++) {
        f[i][j] = 0;
      }
    }
    ans = 0;
    f[n / 2][n / 2] = 1;
    dfs(n / 2, n / 2, n / 2, n / 2);
    printf("%d\n", ans / 4);

  }
  return 0;
}

  

posted @ 2017-12-21 14:48  Fighting_Heart  阅读(252)  评论(0编辑  收藏  举报