符号三角形_hdu_2510(深搜).java
http://acm.hdu.edu.cn/showproblem.php?pid=2510
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 729 Accepted Submission(s): 361
Problem Description
符号三角形的 第1行有n个由“+”和”-“组成的符号 ,以后每行符号比上行少1个,2个同号下面是”+“,2个异 号下面是”-“ 。计算有多少个不同的符号三角形,使其所含”+“ 和”-“ 的个数相同 。 n=7时的1个符号三角形如下:
+ + - + - + +
+ - - - - +
- + + + -
- + + -
- + -
- -
+
Input
每行1个正整数n <=24,n=0退出.
Output
n和符号三角形的个数.
Sample Input
15
16
19
20
0
Sample Output
15 1896
16 5160
19 32757
20 59984
import java.util.Scanner;
public class Main{//打表AC
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int a[]={0,0,0,4,6,0,0,12,40,0,0,171,410,0,0,1896,5160,0,0,32757,59984,0,0,431095,822229};
while(true){
int n=input.nextInt();
if(n==0)
break;
System.out.println(n+" "+a[n]);
}
}
}
import java.util.Scanner;
public class Main{//深搜超时
static int n,sum;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
while((n=input.nextInt())!=0){
sum=0;
String b=new String("");
dfs(0,b);
System.out.println(n+" "+sum);
}
}
private static void dfs(int id, String b) {
if(id==n){
// System.out.println(b);
jc(b);
return;
}
dfs(id+1,b+"+");
dfs(id+1,b+"-");
}
private static void jc(String str) {
char s[]=str.toCharArray();
long a1=0,a2=0;
for(int i=0;i<s.length;i++){
if(s[i]=='+')a1++;
else a2++;
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=n-i;j++){
if(s[j-1]==s[j]){
a1++;
s[j-1]='+';
}
else{
a2++;
s[j-1]='-';
}
}
}
if(a1==a2){
//System.out.println("&&&&"+str);
sum++;
}
}
}
*import java.util.Scanner;
public class Main{//打表
static int n,sum;
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
for(n=1;n<25;n++){
sum=0;
String b=new String("");
dfs(0,b);
System.out.print(sum+",");
}
}
private static void dfs(int id, String b) {
if(id==n){
// System.out.println(b);
jc(b);
return;
}
dfs(id+1,b+"+");
dfs(id+1,b+"-");
}
private static void jc(String str) {
char s[]=str.toCharArray();
long a1=0,a2=0;
for(int i=0;i<s.length;i++){
if(s[i]=='+')a1++;
else a2++;
}
for(int i=1;i<=n-1;i++){
for(int j=1;j<=n-i;j++){
if(s[j-1]==s[j]){
a1++;
s[j-1]='+';
}
else{
a2++;
s[j-1]='-';
}
}
}
if(a1==a2){
//System.out.println("&&&&"+str);
sum++;
}
}
}
符号三角形_hdu_2510(深搜).java的更多相关文章
- HDU--杭电--1195--Open the Lock--深搜--都用双向广搜,弱爆了,看题了没?语文没过关吧?暴力深搜难道我会害羞?
这个题我看了,都是推荐的神马双向广搜,难道这个深搜你们都木有发现?还是特意留个机会给我装逼? Open the Lock Time Limit: 2000/1000 MS (Java/Others) ...
- 符号三角形——F
F. 符号三角形 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: Java class name: 符号 ...
- 【DFS深搜初步】HDOJ-2952 Counting Sheep、NYOJ-27 水池数目
[题目链接:HDOJ-2952] Counting Sheep Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 ...
- HDOJ/HDU 1015 Safecracker(深搜)
Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Kle ...
- ZOJ(ZJU) 1002 Fire Net(深搜)
Suppose that we have a square city with straight streets. A map of a city is a square board with n r ...
- 符号三角形(hdu 2510 搜索+打表)
符号三角形 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Hidden String(深搜)
Hidden String Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- HDU1342 Lotto 【深搜】
Lotto Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- ****Curling 2.0(深搜+回溯)
Curling 2.0 Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total ...
随机推荐
- Bakery CodeForces - 707B (最短路的思路题)
Masha wants to open her own bakery and bake muffins in one of the n cities numbered from 1 to n. The ...
- C和指针之学习笔记(3)
第8章 数组 1.数组与指针 数组名是一个个元素的地址. int a[10]; int b[10]; int *c; (1) c = & a[0]; &a[0]表示一个指向数 ...
- UESTC 1330 柱爷与远古法阵【高斯消元】
题目链接[http://acm.uestc.edu.cn/#/problem/show/1330] 题意:有一个长度为L(L <= 300)的长廊,有一人站在最左边,他要到最右边去,他每次可以走 ...
- 【BZOJ 4361】 4361: isn (DP+树状数组+容斥)
4361: isn Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 218 Solved: 126 Description 给出一个长度为n的序列A( ...
- 【BZOJ 2121】字符串游戏
http://www.lydsy.com/JudgeOnline/problem.php?id=2121 dp,设\(f(i,j,k,l)\)表示原串i到j这个子串能否被删成第k个串的长度为l的前缀. ...
- noip2012疫情控制 题解
题目大意 给出一棵n个节点的树,根是1,要在除根节点以外的点建立检查点,使得从每条根到叶子的路径上都至少存在一个检查点.检查点由军队来建立.初始军队的位置是给定的,移动军队走一条边需要花费这条边的权值 ...
- [UOJ61]怎样更有力气
这个题还是挺有意思的... 一个小结论是:在一个$n$点$m$边的图中,如果度数最小的点度数为$d$,那么$d^2=O(m)$,因为$d\leq\frac{2m}n$,所以$d^2\leq dn\le ...
- April Fools Day Contest 2016 A. Da Vinci Powers
A. Da Vinci Powers 题目连接: http://www.codeforces.com/contest/656/problem/A Description The input conta ...
- BR16F84 OBD II Interface Chip For PWM, VPW, and ISO 9141-2 Vehicles
http://faq.ford77.ru/pdf/eec/DataSheet.pdf FEATURES:Operating Voltage 5.0 VOperating Current 5 Ma. T ...
- 使用vue脚手架工具搭建vue-webpack项目
对于Vue.js来说,如果你想要快速开始,那么只需要在你的html中引入一个<script>标签,加上CDN的地址即可.但是,这并不算是一个完整的vue实际应用.在实际应用中,我们必须要一 ...