• [poj]2488 A Knight's Journey dfs+路径打印


    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 45941   Accepted: 15637

    Description

    Background 
    The knight is getting bored of seeing the same black and white squares again and again and has decided to make a journey 
    around the world. Whenever a knight moves, it is two squares in one direction and one square perpendicular to this. The world of a knight is the chessboard he is living on. Our knight lives on a chessboard that has a smaller area than a regular 8 * 8 board, but it is still rectangular. Can you help this adventurous knight to make travel plans? 

    Problem 
    Find a path such that the knight visits every square once. The knight can start and end on any square of the board.

    Input

    The input begins with a positive integer n in the first line. The following lines contain n test cases. Each test case consists of a single line with two positive integers p and q, such that 1 <= p * q <= 26. This represents a p * q chessboard, where p describes how many different square numbers 1, . . . , p exist, q describes how many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .

    Output

    The output for every scenario begins with a line containing "Scenario #i:", where i is the number of the scenario starting at 1. Then print a single line containing the lexicographically first path that visits all squares of the chessboard with knight moves followed by an empty line. The path should be given on a single line by concatenating the names of the visited squares. Each square name consists of a capital letter followed by a number. 
    If no such path exist, you should output impossible on a single line.

    Sample Input

    3
    1 1
    2 3
    4 3

    Sample Output

    Scenario #1:
    A1
    
    Scenario #2:
    impossible
    
    Scenario #3:
    A1B3C1A2B4C2A3B1C3A4B2C4
    

     按照字典序输出路径,方向要按照字典序来搜索。

    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    bool v[10][10];
    int p, q; 
    int dir[8][2] = {{-2,-1},{-2,1},{-1,-2},{-1,2},
                        {1,-2},{1,2},{2,-1},{2,1}};
    int px[100], py[100];
    int step, flag;
    char R[8] = {'A','B','C','D','E','F','G','H'};
    
    int dfs(int x, int y, int step)
    {
        if (step == p*q) {
            flag = 1;
            for (int i = 0; i < p*q; i++) {
                printf("%c%d", R[px[i]],py[i]+1);
            }
            printf("
    
    ");
            return 1;
        }
    
        int nx, ny;
        for (int i = 0; i < 8; i++) {
            nx = x + dir[i][0];
            ny = y + dir[i][1];
            if (!v[nx][ny] && nx>=0 && nx<q && ny>=0 && ny<p) { 
                v[nx][ny] = 1;
                px[step] = nx; py[step] = ny;
                dfs(nx, ny, step+1);
                if (flag) return 1;   //只搜索一次
                v[nx][ny] = 0;
            }
        }
        return 0;
    }
    
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        int T;
        int t = 0;
        cin >> T;
        while (T--) {
            cin >> p >> q;
            printf("Scenario #%d:
    ", ++t);
            memset(v, 0, sizeof(v));
            px[0] = 0; py[0] = 0;
            v[0][0] = 1;
            flag = 0;
            step = 0;
            if(!dfs(0, 0, 1))
                printf("impossible
    
    ");
        }
    
    
    
    
        return 0;
    }
  • 相关阅读:
    微软WP7本地数据库之Sqlite编程技巧(转)
    AutoResetEvent详解
    桥接模式的简单分析
    解决VS2008 调试启动特别慢
    软件概要设计
    解决windows8不能安装ZUNE的问题
    CDATA的对特殊字符作用说明
    DataTable对象在内存中的使用(二)
    DataTable对象在内存中的使用(一)
    关于MVC3 CODE FIRST的安装
  • 原文地址:https://www.cnblogs.com/whileskies/p/7236290.html
Copyright © 2020-2023  润新知