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

纸雕礼物算法题解析
本文解析了一道关于如何通过算法确定制作最多数量相同纸雕礼物的问题。利用中心对称特性,采用暴力搜索方法,实现了从一张n*n大小的彩色正方形纸上切割出最多的相同图案。代码使用C++编写,并详细解释了实现过程。

题目描述

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;
}

  

转载于:https://www.cnblogs.com/zufezzt/p/8080555.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值