H、Magic necklace
链接: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的更多相关文章
- 如何在项目中引入 #include .h、.lib、 .dll、.cpp (转)
源:http://blog.csdn.net/vippolka/article/details/8552735 在项目中引入.h..lib和dll.以及.cpp 1..h的引入 解决办法1:把 XX ...
- stdafx.h、stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编
sstdafx.h.stdafx.cpp是干什么用的?为什么我的每一个cpp文件都必须包含stdafx.h? Windows和MFC的include文件都非常大,即使有一个快速的处理程序,编译程序也要 ...
- stdafx.h是什么用处, stdafx.h、stdafx.cpp的作用
http://blog.csdn.net/songkexin/article/details/1750396 stdafx.h头文件的作用 Standard Application Fram Exte ...
- stdafx.h、stdafx.cpp的作用
这两个文件用于建立一个预编译的头文件".PCH"和一个预定义的类型文件"STDAFX.OBJ".由于MFC体系结构非常大,各个源文件中都包含许多头文件,如果每次 ...
- sed初理多行合并+sed之G、H、g、h使用+sed n/N使用说明
转载:[shell]sed处理多行合并 - seyjs - 博客园 (cnblogs.com) 文件格式 table=t1 name owner address table=t2 id text co ...
- 合理编写C++模块(.h、.cc)
模块划分 合理编写模块的 demo.h.demo.cc 下例为C++为后端服务编写的探活检测服务 health_server.h #ifndef HEALTH_SERVER_H #define HEA ...
- <string> 与<string.h>、<cstring>的区别
<string.h> <string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <cstring> 在C++标准化(1998年 ...
- C/C++ - <string> 与<string.h>、<cstring>的区别
<string.h><string.h>是C版本的头文件,包含比如strcpy.strcat之类的字符串处理函数. <string><string>是C ...
- Linux移植之auto.conf、autoconf.h、Mach-types.h的生成过程简析
在Linux移植之make uImage编译过程分析中分析了uImage文件产生的过程,在uImage产生的过程中,顺带还产生了其它的一些中间文件.这里主要介绍几个比较关键的文件 1.linux-2. ...
随机推荐
- PYTHON 第二天学习记录
- 关于独立部署web项目到tomcat服务器详情
步骤: 1.设置端口号:找到所解压的tomcat的目录下中的conf文件夹,再用editPlus打开conf文件夹中的server.xml文件,tomcat初始端口为8005,8080,8009,如果 ...
- 【OO第三次课下讨论】农场主的饲料分配问题
需求分析与项目设计 本思考题的设计需求是力图找到一个简单且可行的饲料分配方案,由于不涉及到饲料价格或者是营养均衡之类的优化问题,因此在假设总的饲料量必能满足所有动物的热量需求的前提下,我们只需要采 ...
- Unsafe中CAS的实现
前言 Unsafe 是位于 sun.misc 包下的一个类.Unsafe 提供的 API 大致可分为内存操作.CAS.Class 相关.对象操作.线程调度.系统信息获取.内存屏障.数组操作等几类.由于 ...
- 自定义添加$_SERVER中的变量
如何根据自己项目中的需求自定义$_SERVER中的变量呢?比如$_SERVER['XU_TEXT_PARAM'],这个超全局变量输出肯定是会报错的.那如何自定义它,让它输出我们想要输出的值呢? 1.在 ...
- 解析Laravel框架下的Contracts契约
Contracts Laravel 的契约是一组定义框架提供的核心服务的接口, 例如我们在介绍用户认证的章节中到的用户看守器契约IllumninateContractsAuthGuard 和用户提供器 ...
- 03.文件I/O
UNIX系统中的大多数文件I/O只需用到5个函数:open.read.write.lseek和close. 本章所说明的函数称为不带缓冲的I/O.不带缓冲指的是每个read和write都调用内核中的一 ...
- ipadmini iOS8.4.1系统精简
主要是删除一些系统自带的应用.语言,以及关闭不需要的进程. 精简之后运行流畅性还是能看得出有所提升的.(流畅的玩点游戏还是可行的) 文件的操作可以用Filza.iFile,也可以用爱思助手,但是爱思助 ...
- 【Weiss】【第03章】练习3.8:有序多项式求幂
[练习3.8] 编写一个程序,输入一个多项式F(X),计算出(F(X))P.你程序的时间复杂度是多少? Answer: (特例:P==0时,返回1.) 如果P是偶数,那么就递归计算((F(X))P/2 ...
- HTML每日学习笔记(2)
7.16.2019 1.html表单:用于得到用户不同类型的输入 <form>元素定义表单: <form> First name:<br> <input ty ...