HDU - 1015(dfs.)、HDU - 1016(dfs.)、HDU - 1026(bfs.)

Time limit1000 ms
Memory limit32768 kB
OSWindows

题目:
=== 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.”
Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
Output
LKEBA
YOXUZ
GHOST
no solution
Sample Input
1 ABCDEFGHIJKL
11700519 ZAYEXIWOVU
3072997 SOUGHT
1234567 THEQUICKFROG
0 END
Sample Output
LKEBA
YOXUZ
GHOST
no solution

思路:用深搜即可,搜到第5个字母搜索结束,判断一下是否满足公式。因为要输出字典序最大的那一组,所以每搜索到一个符合的组合,就要比较一下;还有一种方法,就是将输出的字符串按照从大到小重新排序一下,那么搜索到的第一个符合的组合就是字典序最大的组合。如果没有符合的组合直接输出“no solution”。

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
using namespace std;
char answer[6],chara[13],asr[6];
int vis[13];
int n;
void DFS(int t)
{
    if (t==5)
    {
        answer[5]='\0';
        int num1=answer[0]-'A'+1;
        int num2=(answer[1]-'A'+1)*(answer[1]-'A'+1);
        int num3=(answer[2]-'A'+1)*(answer[2]-'A'+1)
        *(answer[2]-'A'+1);
        int num4=(answer[3]-'A'+1)*(answer[3]-'A'+1)
        *(answer[3]-'A'+1)*(answer[3]-'A'+1);
        int num5=(answer[4]-'A'+1)*(answer[4]-'A'+1)
        //这里不能用pow函数求次方,因为pow函数有误差,会影响正确结果。
        if (num1-num2+num3-num4+num5==n)
        {
            if (strcmp(answer,asr)>0)
                strcpy(asr,answer);
        }
        return ;
    }
    for (int i=0;i<strlen(chara);i++)
    {
        if (!vis[i])
        {
            answer[t]=chara[i];
            vis[i]=1;
            DFS(t+1);
            vis[i]=0;
        }
    }
}
int main()
{
    while (~scanf("%d %s",&n,chara))
    {
        memset(vis,0,sizeof(vis));
        if (n==0&&strcmp(chara,"END")==0)
            break;
        strcpy(asr,"@");
        DFS(0);
        if (strcmp(asr,"@")==0)
            cout<<"no solution"<<endl;
        else
            cout<<asr<<endl;
    }
    return 0;
}

Time limit2000 ms
Memory limit32768 kB
OSWindows

题目:
A ring is compose of n circles as shown in diagram. Put natural number 1, 2, …, n into each circle separately, and the sum of numbers in two adjacent circles should be a prime.

Note: the number of first circle should always be 1.

Input
n (0 < n < 20).
Output
The output format is shown as sample below. Each row represents a series of circle numbers in the ring beginning from 1 clockwisely and anticlockwisely. The order of numbers must satisfy the above requirements. Print solutions in lexicographical order.

You are to write a program that completes above process.

Print a blank line after each case.
Sample Input
6
8
Sample Output
Case 1:
1 4 3 2 5 6
1 6 5 2 3 4

Case 2:
1 2 3 8 5 6 7 4
1 2 5 8 3 4 7 6
1 4 7 6 5 8 3 2
1 6 7 4 3 8 5 2

代码:

#include <stdio.h>

int num[21],mark[21],n;
int prime_num[12] = {2,3,5,7,11,13,17,19,23,29,31,37};

//判断是否是质数,是返回1,不是返回0
int is_prime(int a)
{
    for(int i = 0; i < 12;i++)
    if(a==prime_num[i])return 1;
    return 0;
}
void print_num()
{
    for(int i = 1; i < n;i++)
    printf("%d ",num[i]);
    printf("%d",num[n]);
}

int dfs(int pre,int post,int flag)
{
    //如果不符合,直接返回
    if(!is_prime(pre+post))
    return 0;
    num[flag] = post;
    if(flag==n&&is_prime(post+1))
    {
        print_num();
        printf("\n");
        return 1;
    }
    //使用过了这个数字就标记为0
    mark[post] = 0;
    for(int i = 2;i<=n;i++)
    if(mark[i]!=0 && dfs(post,i,flag+1))break;
    //标记位恢复原状
    mark[post] = 1;
    return 0;
}

int main()
{
    int count;
    count = 1;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i = 1; i <= n; i++)
        mark[i] = i;
        num[1] = 1;
        printf("Case %d:\n",count++);
        if(n==1)printf("1\n");
        for(int i = 2;i<=n;i++)
        dfs(1,i,2);
        printf("\n");
    }
    return 0;
}

Time limit1000 ms
Memory limit32768 kB
Special judgeYes
OSWindows

题目:
The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166’s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*M two-dimensional array which left-top corner is (0,0) and right-bottom corner is (N-1,M-1). Ignatius enters at (0,0), and the door to feng5166’s room is at (N-1,M-1), that is our target. There are some monsters in the castle, if Ignatius meet them, he has to kill them. Here is some rules:

1.Ignatius can only move in four directions(up, down, left, right), one step per second. A step is defined as follow: if current position is (x,y), after a step, Ignatius can only stand on (x-1,y), (x+1,y), (x,y-1) or (x,y+1).
2.The array is marked with some characters and numbers. We define them like this:
. : The place where Ignatius can walk on.
X : The place is a trap, Ignatius should not walk on it.
n : Here is a monster with n HP(1<=n<=9), if Ignatius walk on it, it takes him n seconds to kill the monster.

Your task is to give out the path which costs minimum seconds for Ignatius to reach target position. You may assume that the start position and the target position will never be a trap, and there will never be a monster at the start position.
Input
The input contains several test cases. Each test case starts with a line contains two numbers N and M(2<=N<=100,2<=M<=100) which indicate the size of the labyrinth. Then a N*M two-dimensional array follows, which describe the whole labyrinth. The input is terminated by the end of file. More details in the Sample Input.
Output
For each test case, you should output “God please help our poor hero.” if Ignatius can’t reach the target position, or you should output “It takes n seconds to reach the target position, let me show you the way.”(n is the minimum seconds), and tell our hero the whole path. Output a line contains “FINISH” after each test case. If there are more than one path, any one is OK in this problem. More details in the Sample Output.
Sample Input
5 6
.XX.1.
…X.2.
2…X.
…XX.
XXXXX.
5 6
.XX.1.
…X.2.
2…X.
…XX.
XXXXX1
5 6
.XX…
…XX1.
2…X.
…XX.
XXXXX.
Sample Output
It takes 13 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
FINISH
It takes 14 seconds to reach the target position, let me show you the way.
1s:(0,0)->(1,0)
2s:(1,0)->(1,1)
3s:(1,1)->(2,1)
4s:(2,1)->(2,2)
5s:(2,2)->(2,3)
6s:(2,3)->(1,3)
7s:(1,3)->(1,4)
8s:FIGHT AT (1,4)
9s:FIGHT AT (1,4)
10s:(1,4)->(1,5)
11s:(1,5)->(2,5)
12s:(2,5)->(3,5)
13s:(3,5)->(4,5)
14s:FIGHT AT (4,5)
FINISH
God please help our poor hero.
FINISH

简单的BFS,再加上记录前1步可以从终点往前来获得路径。

代码:

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
 
struct node{
    int x,y,step;
    node(int x0=0,int y0=0,int step0=0){
        x=x0;y=y0;step=step0;
    }
};
 
const int offx[]={1,-1,0,0};
const int offy[]={0,0,-1,1};
int n,m,step[110][110],pre[110][110];
char a[110][110];
 
void solve(){
    memset(step,-1,sizeof(step));
    memset(pre,-1,sizeof(pre));
    queue <node> q;
    q.push(node(0,0,0));
    step[0][0]=0;
    while(!q.empty()){
        node s=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int dx=s.x+offx[i],dy=s.y+offy[i],newstep;
            if(dx<0 || dx>=n || dy<0 || dy>=m) continue;
            if(a[dx][dy]=='X') continue;
            if(a[dx][dy]=='.') newstep=s.step+1;
            else newstep=s.step+1+a[dx][dy]-'0';
            if(newstep<step[dx][dy] || step[dx][dy]==-1){
                step[dx][dy]=newstep;
                pre[dx][dy]=i;
                q.push(node(dx,dy,step[dx][dy]));
            }
        }
    }
}
 
void dfs(int dx,int dy){
    if(pre[dx][dy]==-1) return;
    int sx=dx-offx[pre[dx][dy]],sy=dy-offy[pre[dx][dy]];
    dfs(sx,sy);
    printf("%ds:(%d,%d)->(%d,%d)\n",step[sx][sy]+1,sx,sy,dx,dy);
    if(a[dx][dy]!='.'){
        for(int i=1;i<=a[dx][dy]-'0';i++){
            printf("%ds:FIGHT AT (%d,%d)\n",step[sx][sy]+1+i,dx,dy);
        }
    }
}
 
void output(){
    if(step[n-1][m-1]==-1){
        printf("God please help our poor hero.\n");
    }else{
        printf("It takes %d seconds to reach the target position, let me show you the way.\n",step[n-1][m-1]);
        dfs(n-1,m-1);
    }
    printf("FINISH\n");
}
 
int main(){
    while(scanf("%d%d",&n,&m)!=EOF){
        for(int i=0;i<n;i++) scanf("%s",a[i]);
        solve();
        output();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43967023/article/details/86684891