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

13 篇文章 0 订阅

题目描述

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

point:

从中心开始DFS。


#include <set>
#include <map>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define  LL long long
const LL maxn = 55;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int n;
int ans;
int flag[maxn][maxn];
int check(int x,int y)
{
	if(x==1||x==n+1||y==1||y==n+1) return 1;
	return 0;
}

int out(int x,int y)
{
	if(x >= 1 && x <= n+1 && y >= 1 && y <= n+1) 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 xx1=x1+dir[i][0];
		int yy1=y1+dir[i][1];
		int xx2=x2-dir[i][0];
		int yy2=y2-dir[i][1];
		if(flag[xx1][yy1]||flag[xx2][yy2]) continue;
		if(out(xx1,yy1)||out(xx2,yy2)) continue;
		flag[xx1][yy1]=1;
		flag[xx2][yy2]=1;
		dfs(xx1,yy1,xx2,yy2);
		flag[xx1][yy1]=0;
		flag[xx2][yy2]=0;
	}
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--){
		ans = 0;
		scanf("%d",&n);
		memset(flag,0,sizeof flag);
		if(n&1){
			printf("0\n");
		}else{
			flag[n/2+1][n/2+1]=1;
			dfs(n/2+1,n/2+1,n/2+1,n/2+1);
			printf("%d\n",ans/4);
		}
	}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值