链接: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. centos7搭建ceph集群

    一.服务器规划 主机名 主机IP 磁盘配比 角色 node1 public-ip:10.0.0.130cluster-ip:192.168.2.130 sda,sdb,sdcsda是系统盘,另外两块数 ...

  2. ASP.NETMVC中js非空验证实例

    页面代码 @using (Html.BeginForm("Edit", "Home", FormMethod.Post, new { @Id = "f ...

  3. Java自学路线图之Java框架自学

    Java自学路线图的框架分为两个阶段,第一阶段的Java框架包含六个内容:MyBatis,Spring,SpringMVC,Maven高级,Git,Dubbo. 在Java自学过程中掌握框架的使用,对 ...

  4. Flutter 日期时间DatePicker控件及国际化

    注意:无特殊说明,Flutter版本及Dart版本如下: Flutter版本: 1.12.13+hotfix.5 Dart版本: 2.7.0 DatePicker Flutter并没有DatePick ...

  5. C++ 别踩白块小游戏练习

    #include <iostream> #include <stdio.h> #include <stdlib.h> #include <easyx.h> ...

  6. .net webapi 接收保存图片到服务器,并居中剪裁压缩图片

    原文链接:https:////www.cnblogs.com/Jackyye/p/12510943.html 每天解决一些c#小问题,在写微信小程序,或者一些手机软件接口,我们经常要用到上传图片到服务 ...

  7. 【Win10】我们无法更新系统保留的分区

      前言 笔者是一个萌新,这个方案也是慢慢摸索出来的,有更好的方案欢迎大家提出 前段时间用公司电脑发现win10新版本还行,回家升级自己的电脑却提示“我们无法更新系统保留的分区”.(O_o)?? 笔者 ...

  8. 033.Kubernetes集群安全-API Server认证及授权

    一 Kubernetes集群安全 1.1 安全机制 Kubernetes通过一系列机制来实现集群的安全控制,其中包括API Server的认证授权.准入控制机制及保护敏感信息的Secret机制等.集群 ...

  9. Spring Cloud - Nacos注册中心入门单机模式及集群模式

    近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...

  10. ASP.NET MVC5实现芒果分销后台管理系统(二):Code First快速集成EntityFramework

    在上一篇文章中,我们已经搭建了整个芒果后台管理系统整个工程架构,并集成了AutoMapper,日志组件等,接下来我们将使用Entity Framework完善系统的持久化存储部分.这篇EF的构造,我将 ...