列表

详情


NC14654. 剪纸

描述

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 74ms, 内存消耗: 420K, 提交时间: 2020-08-14 16:56:52

#include<bits/stdc++.h>
using namespace std;
int T,n,A[15][15],t;
int Di[]={0,1,0,-1};
int Dj[]={1,0,-1,0};

void dfs(int i,int j)
{
    if(i==0||j==0||i==n||j==n)
    {
        t++;
        return ;
    }
    for(int z=0;z<4;z++)
    {
        int x=i+Di[z];
        int y=j+Dj[z];
        if(A[x][y]||x<0||x>n||y<0||y>n)
            continue;
        A[x][y]=A[n-x][n-y]=1;
        dfs(x,y);
        A[x][y]=A[n-x][n-y]=0;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin>>T;
    while(T--)
    {
        t=0;
        cin>>n;
        memset(A,0,sizeof(A));
        if(n%2==1)
            cout<<1<<endl;
        else
        {
            int x=n/2;
            A[x][x]=A[n-x][n-x]=1;
            dfs(x,x);
            cout<<t/4<<endl;
        }
    }
    return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 86ms, 内存消耗: 456K, 提交时间: 2023-04-08 00:40:11

#include<bits/stdc++.h>
using namespace std;
int vis[105][105];
int sum=0;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int n;

bool judge(int x,int y){
	if(x<1||y<1||x>n||y>n) return false;
	else return true;
}

void dfs(int x,int y){
	if(x==0||y==0||x==n||y==n){
		sum++;//答案++
        return;
	}
	for(int i=0;i<4;++i){
		int xx=x+dx[i];
		int yy=y+dy[i];
		if(!vis[xx][yy]){
			vis[xx][yy]=1;
			vis[n-xx][n-yy]=1;
			dfs(xx,yy);
			vis[n-xx][n-yy]=0;
			vis[xx][yy]=0;
		}
	}
}

int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		memset(vis,0,sizeof(vis));
		scanf("%d",&n);
		if(n%2!=0){
			printf("0\n");
		}else{
			sum=0;
			int zx=n/2;
			int zy=n/2;
			vis[zx][zy]=1;
			dfs(zx,zy);
			printf("%d\n",sum/4);
		}
	}
}

C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 384K, 提交时间: 2017-12-20 19:11:52

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int T,n;cin>>T;
    while(T--)
    {
        cin>>n;
        if(n==2)cout<<1<<endl;
        else if(n==4)cout<<11<<endl;
        else if(n==6)cout<<509<<endl;
        else if(n==8)cout<<184525<<endl;
        else cout<<0<<endl;
    }
    return 0;
}

上一题