【a502】符号三角形问题
Time Limit: 1 second
Memory Limit: 32 MB
【问题描述】
在一般情况下,符号三角形的第一行有n个符号。按照2个同号的下面是“+”号,2个异号的下面是“-”号的规则生成符号三角形。例如:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
即是一个由14个“+”号和14个“-”号组成的符号三角形。要求对于给定的n,计算有多少个不同的符号三角形,使其所包含的“+”和“-”的个数相同。要求用回溯法求解此问题。
【输入格式】
仅有一行,包含一个整数N,表示第一行有N个字符。
【输出格式】
仅有一行,不同的符号三角形的个数;当个数为0时,输出“No solution”
【输入样例】
4
【输出样例】
6
【题目链接】:http://noi.qz5z.com/viewtask.asp?ID=a502
【题解】
先用一层dfs枚举第一层的样子;
O(2^n)
然后根据第一层处理出整张图的样子;(也用dfs)
然后用趋近于O(N^2)的时间复杂度
处理出整张图的样子的时候;
可以加一个判断
if (负号或正号个数>(1+n)*n/4) return;
这个剪枝挺强力的吧.
处理的时候可以把所有的符号都移到最左边;左对齐;
a[i][j]由a[i-1][j]和a[i-1][j+1]决定;
(为它们的异或值)
一开始可以加一个判断if (1+n)*n/2为奇数;
那么就不可能有解;
依我对这个平台的了解;
它肯定会出一个(1+n)*n/2为奇数,然后n特别大的数据点。
[斜眼笑]
【完整代码】
#include <cstdio>
int n,ans = 0,temp;
int a[100][100];
void dfs2(int x,int y,int z,int f)
{
if (z>temp || f > temp)
return;
if (x>n)
{
ans++;
return;
}
if (y>n-x+1)
{
dfs2(x+1,1,z,f);
return;
}
a[x][y] = a[x-1][y] ^ a[x-1][y+1];
if (a[x][y]==1)
dfs2(x,y+1,z+1,f);
else
dfs2(x,y+1,z,f+1);
}
void dfs1(int x,int z,int f)
{
if (x > n)
{
dfs2(2,1,z,f);
return;
}
a[1][x] = 1;
dfs1(x+1,z+1,f);
a[1][x] = 0;
dfs1(x+1,z,f+1);
}
int main()
{
scanf("%d",&n);
if (((1+n)*n/2)&1)
{
puts("No solution");
return 0;
}
temp = (1+n)*n/4;
dfs1(1,0,0);
if (ans==0)
puts("No solution");
else
printf("%d\n",ans);
return 0;
}
【a502】符号三角形问题的更多相关文章
- OpenJudge 2990:符号三角形 解析报告
2990:符号三角形 总时间限制: 1000ms 内存限制: 65536kB 描述 符号三角形的第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“ ...
- 符号三角形——F
F. 符号三角形 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: Java class name: 符号 ...
- 符号三角形(hdu 2510 搜索+打表)
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 符号三角形_hdu_2510(深搜).java
http://acm.hdu.edu.cn/showproblem.php?pid=2510 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- hdu 2510 符号三角形 (DFS+打表)
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- code vs 3376 符号三角形
3376 符号三角形 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 如下图是由14个“+”和14个“-”组 ...
- 【HDOJ】2510 符号三角形
暴力打表. #include <cstdio> ]={,,,,,,,,,,,,,,,,,,,,,,,,}; int main() { while (scanf("%d" ...
- HDU 2510 - 符号三角形
DFS后打表 #include <iostream> using namespace std; ,,,,,,,,,,,,,,,,,,,,,,,,}; int main() { int n; ...
- *HDU2510 DFS
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
随机推荐
- vector转数组
vector转数组 由于vector内部的数据是存放在连续的存储空间,vector转数组事实上只需要获取vector中第一个数据的地址和数据的长度即可.如果仅仅是传参,无需任何操作,直接传地址即可,如 ...
- 【Educational Codeforces Round 35 A】 Nearest Minimums
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找出最小的数字的位置. 最近的肯定是相邻的某对. [代码] #include <bits/stdc++.h> using ...
- 11G、12C Data Guard Physical Standby Switchover转换参考手册
Switchover转换 Step 1: switchover 切换先前检查 (1)确保主备两端log_archive_config和db_unique_name参数都已经正确设置. 需要注意的是 ...
- python 的 reshape强制转换格式的用途
shu=[[ 0.03046758], [ 0.05485586], [ 0.03282908], [ 0.02107211], [ 0.0391144 ], [ 0.07847244], [ 0.1 ...
- PatentTips - Method and Apparatus to Support Virtualization with Code Patches
BACKGROUND As recognized in Revision 2.0 of the Intel® Virtualization Technology Specification for t ...
- LaTeX indicator function(指示函数)(\mathbb {1} 不起作用)
问题说明: \mathbb字符的空心化显示仅对字符有效,对数字无效. 解决方法: 使用 bbm 包 \documentclass{article} \usepackage{bbm} \begin{do ...
- LeetCode Algorithm 03_Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- ZOJ 2850和ZOJ 1414
下午上数据结构,结果竟然没有新题.T T果断上OJ来水一发 ZOJ 2850 Beautiful Meadow 传送门http://acm.zju.edu.cn/onlinejudge/showP ...
- UIScrollView(滚动试图)
UIScrollView(滚动试图) 1.简介 为什么有UISCrollView: 在iOS开发中,由于移动设备的屏幕大小有限,所以不能像PC一样显示很多内容,因此当手机屏幕需要展示的内容较多超出一个 ...
- 【Codeforces Round #437 (Div. 2) B】Save the problem!
[链接]h在这里写链接 [题意] 给你一个金额N,和硬币的类型总数M; (完全背包),然后问你组成N的方案数. 使得,用这些硬币组成价值为N的金额的方案数为A; 现在A ...