前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ----Safecracker(1015)

HDUOJ----Safecracker(1015)

作者头像
Gxjun
发布2018-03-21 13:26:58
5850
发布2018-03-21 13:26:58
举报
文章被收录于专栏:mlml

Safecracker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6779    Accepted Submission(s): 3370

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 World War II. Fortunately old Brumbaugh from research knew Klein's secrets and wrote them down before he died. A Klein safe has two distinguishing features: a combination lock that uses letters instead of numbers, and an engraved quotation on the door. A Klein quotation always contains between five and twelve distinct uppercase letters, usually at the beginning of sentences, and mentions one or more numbers. Five of the uppercase letters form the combination that opens the safe. By combining the digits from all the numbers in the appropriate way you get a numeric target. (The details of constructing the target number are classified.) To find the combination you must select five letters v, w, x, y, and z that satisfy the following equation, where each letter is replaced by its ordinal position in the alphabet (A=1, B=2, ..., Z=26). The combination is then vwxyz. If there is more than one solution then the combination is the one that is lexicographically greatest, i.e., the one that would appear last in a dictionary." v - w^2 + x^3 - y^4 + z^5 = target "For example, given target 1 and letter set ABCDEFGHIJKL, one possible solution is FIECB, since 6 - 9^2 + 5^3 - 3^4 + 2^5 = 1. There are actually several solutions in this case, and the combination turns out to be LKEBA. Klein thought it was safe to encode the combination within the engraving, because it could take months of effort to try all the possibilities even if you knew the secret. But of course computers didn't exist then." === Op tech directive, computer division, 2002/11/02 12:30 CST === "Develop a program to find Klein combinations in preparation for field deployment. Use standard test methodology as per departmental regulations. Input consists of one or more lines containing a positive integer target less than twelve million, a space, then at least five and at most twelve distinct uppercase letters. The last line will contain a target of zero and the letters END; this signals the end of the input. For each line output the Klein combination, break ties with lexicographic order, or 'no solution' if there is no correct combination. Use the exact format shown below."

Sample Input

1 ABCDEFGHIJKL 11700519 ZAYEXIWOVU 3072997 SOUGHT 1234567 THEQUICKFROG 0 END

Sample Output

LKEBA YOXUZ GHOST no solution

Source

Mid-Central USA 2002

 搜索....dfs

代码:

代码语言:javascript
复制
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 char maze[30],key[7];
 6 int str[7];
 7 /*rn长与cm寛*/
 8 int n,cnt,cm,cmt;
 9 int cmp(const void *a ,const void *b)
10 {
11     return *(char *)b-*(char *)a;
12 }
13 bool dfs(int step)   /*深度搜索*/
14 {
15   if(step==5&&(str[0]-str[1]*str[1]+str[2]*str[2]*str[2]-str[3]*str[3]*str[3]*str[3]+str[4]*str[4]*str[4]*str[4]*str[4])==n)  //单词有5个
16   {  
17     return true;
18   }
19   /*如果没有找到,继续搜索*/
20   if(step<5)
21   {
22       int i;
23    for( i=0;i<cm;i++)     //作为宽度   
24       {
25           if(maze[i])        //不为0
26           {
27              key[cnt++]=maze[i];
28              str[cmt++]=maze[i]-64;
29              maze[i]='\0';
30              if(dfs(step+1))
31                  return true;
32              maze[i]=key[--cnt];
33              key[cnt]='\0';
34              str[--cmt]=0;
35           }
36       }
37   }
38   return false;
39 }
40 
41 int main()
42 {
43    while(scanf("%d %s",&n,maze)!=EOF)
44     {
45        if(n==0&&strcmp(maze,"END")==0) break;
46         cmt=cnt=0;
47         cm=strlen(maze);   /*作为搜索的数组*/
48         memset(key,'\0',sizeof(key));
49         memset(str,0,sizeof(str));
50         qsort(maze,cm,sizeof(char),cmp);
51         if(dfs(0))
52             printf("%s\n",key);
53         else
54             printf("no solution\n");
55     } 
56   return 0;
57 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-25 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Safecracker
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档