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 深浅拷贝 元组 字典 集合操作
深浅拷贝 :值拷贝 :ls = [,,] res = ls 则print(res)就是[,,] 浅拷贝 :ls.copy() 深拷贝:ls3 = deepcopy(ls) # 新开辟列表空间,ls列表 ...
- docker 搭建本地私有仓库
1.使用registry镜像创建私有仓库 安装docker后,可以通过官方提供的 registry 镜像来简单搭建一套本地私有仓库环境: docker run -d -p : registry: 这将 ...
- springboot 整合logback
日志包使用的是springboot内置的日志包,所以我们不许要再专门导入日志包 1.logback-spring.xml配置 <?xml version="1.0" enco ...
- 结题报告--P5551洛谷--Chino的树学
题目:点此 题目描述 Chino树是一棵具有某种性质的满二叉树,具体来说,对于这棵树的每一个非叶子节点,它的左子节点(A)(A)(A)的右子节点(C)(C)(C)与它的右子节点(B)(B)(B)的左子 ...
- Js数组代替写循环的几个方法
简介 循环是个不可避免的结构,而且不好复用,同时循环还很难加入其他操作中.更麻烦的是,使用循环就意味着在每一个新的迭代中有更多变化需要响应. 上了循环的控制结构会使代码看起来变得复杂,故而这里提几个替 ...
- TLS/SSL 梳理
数据加密通篇都是为了防止第三方的劫持伪造,保证连接安全, 毫无遮掩的明文传输只有民风淳朴的时候才是安全的. 先是一些基础的内容: 对称加密 最开始为了对数据进行加密,使用的是对称加密算法,即双方协商好 ...
- Vue2.0 【第三季】第1节 propsData Option 全局扩展的数据传递
目录 Vue2.0 [第三季]第1节 propsData Option 全局扩展的数据传递 第1节 propsData Option 全局扩展的数据传递 Vue2.0 [第三季]第1节 propsDa ...
- Vue2.0 【第一季】第7节 v-bind指令
目录 Vue2.0 [第一季] 第7节 v-bind指令 第7节 v-bind指令 v-bind缩写 绑定CSS样式 Vue2.0 [第一季] 第7节 v-bind指令 第7节 v-bind指令 v- ...
- 微信小程序接入LeanCloud
大家在做小程序或者客户端开发的时候肯定会想使得数据进行联网,但这样就必须有对应的后台服务器以及数据库,再加上linux运维等各种细节,往往会对新手比较劝退,在这里给大家推荐一种bass(后端即服务), ...
- (转)浅析epoll-为何多路复用I/O要使用epoll
原文地址:http://www.cppfans.org/1417.html 浅析epoll-为何多路复用I/O要使用epoll 现如今,网络通讯中用epoll(linux)和IOCP(windows) ...