第二十五天:递归的应用(回溯法枚举 + 图的遍历) +深度优先遍历 ( DFS )

T1

Prime ring problem
时间限制:2 秒
内存限制:32 兆
特殊判题:否
题目描述: 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.

输入: n (1 < n < 17).

输出: 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.

样例输入:
6
8

样例输出:
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

//
//  main.cpp
//  PrimeRing
//
//  Created by Apple on 2019/8/29.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int ans[22];
bool hashes[22];
int n;
int prime[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43};

int judge(int a){
    for(int i = 0;i<14;i++){
        if(prime[i] == a)
            return true;
    }
    return false;
}

void check(){
    if(!judge(ans[1] + ans[n]))
        return;
    for(int i = 1;i <= n;i++){
        if(i != 1)
            printf(" ");
        printf("%d", ans[i]);
        
    }
    printf("\n");
}

void DFS(int num){
    if(num > 1)
        if(!judge(ans[num] + ans[num - 1]))
           return;
    if(num == n){
        check();
        return;
    }
    for(int i = 2;i <= n;i++){
        if(!hashes[i]){
            hashes[i] = true;
            ans[num + 1] = i;
            DFS(num + 1);
            hashes[i] = false;
        }
    }
}

int main(int argc, const char * argv[]) {
    int cas = 0;
    for(int i = 0;i < 22;i++)
        hashes[i] = false;
    while(scanf("%d", &n) != EOF){
        cas++;
        
        ans[1] = 1;
        printf("Case %d:\n", cas);
        DFS(1);
        printf("\n");
    }
    return 0;
}

T2

Oil Deposit
时间限制:1 秒
内存限制:32 兆
特殊判题:否
题目描述: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.

输入: The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100.
Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either *', representing the absence of oil, or@’, representing an oil pocket.
输出: For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

样例输入:
1 1
*
3 5
@@*
@
@@*
1 8
@@***@
5 5
****@
@@@
@**@
@@@
@
@@**@
0 0

样例输出:
0
1
2
2

//
//  main.cpp
//  Deposit
//
//  Created by Apple on 2019/8/29.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <stdio.h>
using namespace std;

char maze[101][101];
bool mark[101][101];
int n, m;
int go[][2] = {
    0, 1,
    0, -1,
    1, 0,
    -1, 0,
    1, 1,
    1, -1,
    -1, 1,
    -1, -1,
};

void DFS(int x, int y){
    for(int i = 0;i<8;i++){
        int nx = x + go[i][0];
        int ny = y + go[i][1];
        if(nx <= 0 || nx > n || ny <= 0 || ny > m)
            continue;
        if(maze[nx][ny] == '*')
            continue;
        if(mark[nx][ny])
            continue;
        mark[nx][ny] = true;
        DFS(nx, ny);
    }
    return;
}

int main(int argc, const char * argv[]) {
    while(scanf("%d%d", &n, &m) != EOF){
        if(n == 0 && m == 0)
            continue;
        for(int i = 1;i <= n;i++)
            scanf("%s", maze[i] + 1);
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++)
                mark[i][j] = false;
        int ans = 0;
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= m;j++){
                if(maze[i][j] == '*')
                    continue;
                if(mark[i][j])
                    continue;
                DFS(i, j);
                ans++;
            }
        }
        printf("%d\n", ans);
        
    }
    
    return 0;
}

T3

Temple of the bone
时间限制:1 秒
内存限制:32 兆
特殊判题:否

题目描述: The doggie found a bone in an ancient maze, which fascinated him a lot.
However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

输入: The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters.
A character is one of the following:
‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door;
‘.’: an empty block.
The input is terminated with three 0’s. This test case is not to be processed.

输出: For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.

样例输入:
4 4 5
S.X.
…X.
…XD

3 4 5
S.X.
…X.
…D
0 0 0

样例输出:
NO
YES

//
//  main.cpp
//  TempleBone
//
//  Created by Apple on 2019/8/29.
//  Copyright © 2019 Apple_Lance. All rights reserved.
//

#include <iostream>
#include <stdio.h>
char maze[8][8];
int n, m, t;
bool success;
int go[][2] = {
    0,1,
    0,-1,
    1,0,
    -1,0
};

void DFS(int x, int y, int time){
    for(int i = 0;i < 4;i++){
        int nx = x + go[i][0];
        int ny = y + go[i][1];
        if(nx <= 0 || nx > n || ny <= 0 || ny > m)
            continue;
        if(maze[nx][ny] == 'X')
            continue;
        if(maze[nx][ny] == 'D'){
            if(time + 1 == t){
                success = true;
                return;
            }
            else
                continue;
        }
        maze[nx][ny] = 'X';
        DFS(nx, ny, time + 1);
        maze[nx][ny] = '.';
        if(success == true)
            return;
    }
}

int main(int argc, const char * argv[]) {
    while(scanf("%d%d%d", &n, &m, &t) != EOF){
        if(n == 0 && m == 0 && t == 0)
            break;
        for(int i = 1;i <= n;i++)
            scanf("%s", maze[i] + 1);
        success = false;
        
        int sx, sy;
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++)
                if(maze[i][j] == 'D'){
                    sx = i;
                    sy = j;
                }
        for(int i = 1;i <= n;i++)
            for(int j = 1;j <= m;j++)
                if(maze[i][j] == 'S' && (i + j)%2 == ((sx + sy)%2 + t%2)%2){
                    maze[i][j] = 'X';
                    DFS(i, j, 0);
                }
        puts(success ? "YES" : "NO");
                    
    }
    
    return 0;
}
发布了182 篇原创文章 · 获赞 101 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/lancecrazy/article/details/100124714