链接:https://ac.nowcoder.com/acm/contest/3570/H

来源:牛客网

题目描述

There was a magic necklace. The necklace is made of magical gems

numbered 1 through n. Unfortunately, the necklace was accidentally

broken yesterday. Every gem was scattered on the ground. Now you need

to restring the magic necklace with these n gems. In order to keep the

necklace magical, adjacent numbered gems cannot be placed next to each

other in the new necklace. Please output the number of ways to

successfully restring the necklace. If the relative positions of the

gems are the same, we think these are the same case. 输入描述: The first

line of the input contains one integer t — the number of test cases.(t

≤ 110) The next t lines contain test cases. Each line contains a

positive integer n(0 ≤ n ≤ 11)describing a test case. 输出描述: For each

test case, output a line containing an integer which indicates the

answer.

示例1

输入

3
1
2
5

输出

1
0
1

说明

The two pictures above show the same method.

思路如下

数据范围最大就到n = 11,dfs爆搜即可得到答案,由于题面是多组,需要预处理出答案以免TLE,还有这一题的去掉不符合题的情况也是很重要的

结题如下

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath> using namespace std; int n,Ans,t;
int vis[105],ans[15]; //vis存放每种情况,ans存储1~11个数字对应的答案 void dfs(int cur,int fa,int cnt,int x) //cnt表示递归了多少层、x表示要处理的数
{
if(cnt == x && cur != 2)
{
Ans++; //每种情况对应的方案数
return;
}
else if(cnt == x) return;
for(int i = 1;i <= x; i++)
{
if(vis[i]) continue;
if(i == fa || i == cur) continue;
if(abs(i - cur) == 1) continue;
vis[i] = 1;
dfs(i,cur,cnt + 1,x);
vis[i] = 0;
}
} void init(int x){
for(int i = 1;i <= x; i++) vis[i] = 0;
vis[1] = 1; //这里我们假定对应没个数字方n 我的都让方案以 1 开头(所以要把vis[1]进行标记),既然对应每个数字n方案的开头为1,则结尾就一定不能为2
Ans = 0;
dfs(1,0,1,x);
ans[x] = Ans / 2; //这里的得到的方案数为什么要除以2 ? ,这是因为方案中会有一半是重复的(还是举数字 n == 3这个例子来说吧:1 3 5 2 4 6 与 1 6 4 2 5 3 除了开头相同,其它的部分完全就是颠倒一下,这对与题意来说这两种情款完全是相同的),下面给出6的所有出现的方案Ans(包括重复的)
/*
n == 6 的所有Ans方案数
1 3 5 2 4 6
1 3 5 2 6 4
1 3 6 4 2 5
1 4 2 5 3 6
1 4 2 6 3 5
1 4 6 2 5 3
1 5 2 4 6 3
1 5 3 6 2 4
1 6 3 5 2 4
1 6 4 2 5 3
*/
} int main()
{
for(int i = 1;i <= 11; i++) init(i); ans[1] = 1;
ans[0] = 0; scanf("%d",&t);
while(t--){
scanf("%d",&n);
printf("%d\n",ans[n]);
}
}

H、Magic necklace的更多相关文章

  1. 如何在项目中引入 #include .h、.lib、 .dll、.cpp (转)

    源:http://blog.csdn.net/vippolka/article/details/8552735 在项目中引入.h..lib和dll.以及.cpp 1..h的引入 解决办法1:把  XX ...

  2. stdafx.h、stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编

    sstdafx.h.stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要 ...

  3. stdafx.h是什么用处, stdafx.h、stdafx.cpp的作用

    http://blog.csdn.net/songkexin/article/details/1750396 stdafx.h头文件的作用 Standard Application Fram Exte ...

  4. stdafx.h、stdafx.cpp的作用

    这两个文件用于建立一个预编译的头文件".PCH"和一个预定义的类型文件"STDAFX.OBJ".由于MFC体系结构非常大,各个源文件中都包含许多头文件,如果每次 ...

  5. sed初理多行合并+sed之G、H、g、h使用+sed n/N使用说明

    转载:[shell]sed处理多行合并 - seyjs - 博客园 (cnblogs.com) 文件格式 table=t1 name owner address table=t2 id text co ...

  6. 合理编写C++模块(.h、.cc)

    模块划分 合理编写模块的 demo.h.demo.cc 下例为C++为后端服务编写的探活检测服务 health_server.h #ifndef HEALTH_SERVER_H #define HEA ...

  7. <string> 与<string.h>、<cstring>的区别

    <string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...

  8. C/C++ - <string> 与<string.h>、<cstring>的区别

    <string.h><string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <string><string>是C ...

  9. Linux移植之auto.conf、autoconf.h、Mach-types.h的生成过程简析

    在Linux移植之make uImage编译过程分析中分析了uImage文件产生的过程,在uImage产生的过程中,顺带还产生了其它的一些中间文件.这里主要介绍几个比较关键的文件 1.linux-2. ...

随机推荐

  1. 【一统江湖的大前端(8)】matter.js 经典物理

    目录 [一统江湖的大前端(8)]matter.js 经典物理 一.经典力学回顾 二. 仿真的实现原理 2.1 基本动力学模拟 2.2 碰撞模拟 三. 物理引擎matter.js 3.1 <愤怒的 ...

  2. CentOS7系统更换软件安装源

    1.备份你的原镜像文件,以免出错后可以恢复. cp /etc/yum.repos.d/CentOS-Base.repo{,.backup} # 或者 mv /etc/yum.repos.d/CentO ...

  3. Java自学路线图之Java系统自学

    Java自学不是一朝一夕的事情.可以采用"懒开始"的方法,但是必须要坚持下去,才能真正自学Java掌握编程技术.那些企图学几天去包装一下找工作的,请绕道.如果你下定决心自学Java ...

  4. MATLAB神经网络(2) BP神经网络的非线性系统建模——非线性函数拟合

    2.1 案例背景 在工程应用中经常会遇到一些复杂的非线性系统,这些系统状态方程复杂,难以用数学方法准确建模.在这种情况下,可以建立BP神经网络表达这些非线性系统.该方法把未知系统看成是一个黑箱,首先用 ...

  5. 【5min+】 一个令牌走天下!.Net Core中的ChangeToken

    系列介绍 [五分钟的dotnet]是一个利用您的碎片化时间来学习和丰富.net知识的博文系列.它所包含了.net体系中可能会涉及到的方方面面,比如C#的小细节,AspnetCore,微服务中的.net ...

  6. call、apply和bind的学习

    相似之处:1.都是用来改变函数的this对象的指向的.2.第一个参数都是this要指向的对象.3.都可以利用后续参数传参. var xw = {  name : "小王",gend ...

  7. kafka启动报错"A broker is already registered on the path /brokers/ids/1"解决方案

    问题 kafka挂掉后,启动报错日志如下 [2020-03-19 17:50:58,123] FATAL Fatal error during KafkaServerStartable startup ...

  8. java-随机点名2(新手)

    //创建的一个包名. package qige; //导入一个包.import java.util.*; //定义一个类.public class SJdm { //公共静态的主方法. public ...

  9. PowerShell初探

    Windows PowerShell是一种命令行外壳程序和脚本环境,它内置在每个受支持的Windows版本中(Windows 7/Windows 2008 R2和更高版本),使命令行用户和脚本编写者可 ...

  10. 使用SpringBoot + JavaMailSender 发送邮件报错 Mail server connection failed;Could not connect to SMTP host

    说明: 出于安全考虑,阿里云默认封禁 TCP 25 端口出方向的访问流量,无法在阿里云上的云服务器通过 TCP 25 端口连接外部地址. [官方提示]:如果您需要使用阿里云上的云服务器对外部发送邮件, ...