HDU 1015 dfs回溯

题目真长。。。。。看了好长时间才看懂。。

就是给你一个32位数字和一个最多15个字符的字符串,从字符串中选出5个字符,若满足题中给的那个式子,输出字典序最大的那5个字符,若不满足,输出no
solution。

为了解决字典序问题,在输入字符串后,把字符串按从大到小排一下序,搜索一下若满足条件输出即可。

贴代码。


 1 #include <stdio.h>
2 #include <string.h>
3 #include <algorithm>
4 #include <iostream>
5 using namespace std;
6
7 int num;
8 char a[15], b[15]; //a是原串,b储存答案。
9 int flag[15]; //标记a中元素是否使用。
10 int n, f;
11
12 bool cmp(int x,int y) //排序
13 {
14 return x>y;
15 }
16
17 void dfs(int k)
18 {
19 if(f) return; //满足条件一层层返回
20 if(k==5)
21 {
22 int sum=b[0]-b[1]*b[1]+b[2]*b[2]*b[2]-b[3]*b[3]*b[3]*b[3]+b[4]*b[4]*b[4]*b[4]*b[4];
23 if(sum==num)
24 {
25 for(int i=0;i<5;i++)
26 {
27 char c=b[i]-1+‘A‘;
28 printf("%c",c);
29 }
30 cout<<endl;
31 f=1;
32 }
33 return;
34 }
35 for(int i=0;i<n;i++)
36 {
37 if(!flag[i])
38 {
39 flag[i]=1;
40 b[k]=a[i];
41 dfs(k+1);
42 flag[i]=0; //别忘了释放
43 }
44 }
45 }
46 main()
47 {
48 int i, j, k;
49 while(scanf("%d%s",&num,a)!=EOF&&strcmp(a,"END"))
50 {
51 memset(flag,0,sizeof(flag));
52 n=strlen(a);
53 for(i=0;i<n;i++)
54 a[i]=a[i]-‘A‘+1;
55 sort(a,a+n,cmp);
56 f=0;
57 dfs(0);
58 if(!f) printf("no solution\n");
59 }
60 }

HDU 1015 dfs回溯,布布扣,bubuko.com

时间: 2024-12-28 12:08:36

HDU 1015 dfs回溯的相关文章

hdu 4155 dfs回溯

数据并不大,dfs回溯即可. 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 30; 7 char str[N]; 8 int digit[N]; 9 10 int dfs( int sum ) 11 { 12 if ( sum > 31 ) return 1; 13 for ( int i = 1; i

HDU - 1010 (DFS回溯法 + 奇偶剪枝)

题目大意:给出一个N*M的迷宫,迷宫中有一扇门D,只有在T时刻会打开,现在你0时刻位于S,你需要在正好在T时刻到达D,你只能上下左右移动,每次移动耗费1时刻,且同一个位置不能进入两次,问是否能在T时刻刚好到达D处. 范围 1 < N,M < 7, 1 < T < 50,这个范围有点大,直接DFS回溯搜会TLE,我们要加上一个剪枝,这个剪枝十分重要,我们求出起点到终点的距离(横向距离 + 纵向距离),若这个距离为奇数,则时间T必须也要为奇数,否则时间T必须为偶数(其中的道理显而易见)

hdu 1015 dfs

Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

hdu4499 Cannon (DFS+回溯)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4499 Cannon Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 214 Problem Des

hdu 1015 Safecracker (纯暴力)

Safecracker Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8183    Accepted Submission(s): 4143 Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein

CodeForces 550B Preparing Olympiad(DFS回溯)

[题目链接]:click here~~ [题目大意] 一组题目的数目(n<=15),每个题目有相应的难度,问你选择一定的题目(大于r个且小于l个)且选择后的题目里最小难度与最大难度差不小于x,求选择方案数. [解题思路]: DFS+回溯. 先发一发比较拙的代码: #include <bits/stdc++.h> using namespace std; const int N=1e5+10; int num[N],mum[N]; int n,m,q,t,l,r; int top,ans,

hdu 1015 Safecracker 水题一枚

题目链接:HDU - 1015 === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in World

HDU 1015 Safecracker 题解

Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

HDU4499 Cannon DFS 回溯的应用

题意就是给你一个n*m的棋盘,然后上面已经有了 棋子,并给出这些棋子的坐标,但是这些棋子是死的就是不能动,然后让你在棋盘上面摆炮,但是炮之间不能互相吃,吃的规则我们斗懂得 炮隔山打嘛,问你最多能放几个炮 肯定是搜索了,n,m最大才5,可能挺久没做了,对于回溯反而把握不好了,写了好久调试了好久,才过 #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #