zoukankan      html  css  js  c++  java
  • 18.06.30 POJ 2488:A Knight's Journey

    描述

    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.

    输入

    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, . ..

    输出

    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.

    样例输入

    3
    1 1
    2 3
    4 3

    样例输出

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

    来源

    TUD Programming Contest 2005, Darmstadt, Germany

     1 #include <cstdio>
     2 #include <string>
     3 #include <memory.h>
     4 #include <algorithm>
     5 #include <stdlib.h>
     6 #include <math.h>
     7 #include <iostream>
     8 #include<queue>
     9 #include <vector>
    10 #include <bitset>
    11 using namespace std;
    12 
    13 int r, c;
    14 int kase;
    15 int visited[10][10];
    16 int jing = 0;
    17 bool flag;
    18 char sign[9] = { 0,'A','B','C','D','E','F','G','H' };
    19 int dir1[8] = { -2,-2,-1,-1,1,1,2,2 }, dir2[8] = {-1,1,-2,2,-2,2,-1,1};
    20 struct node {
    21     int x, y;
    22     node(int a, int b) :x(a), y(b) {}
    23 };
    24 vector<node> solution;
    25 void dfs(int x,int y ) {
    26     if (jing == r * c) {
    27         printf("Scenario #%d:
    ", kase);
    28         vector<node>::iterator i1 = solution.begin(), i2 = solution.end();
    29         for (; i1 != i2; i1++) 
    30             cout << sign[(*i1).x] << (*i1).y;
    31         printf("
    
    ");
    32         flag = true;
    33         return;
    34     }
    35     if (flag)return;
    36     for (int i = 0; i <= 7; i++) {
    37         int xx = x + dir1[i], yy = y + dir2[i];
    38         if (visited[xx][yy]||xx<1||xx>r||yy<1||yy>c)
    39             continue;
    40         visited[xx][yy] = 1;
    41         solution.push_back(node(xx, yy));
    42         jing++;
    43         dfs(xx, yy);
    44         if (flag == true)
    45             return;
    46         solution.pop_back();
    47         visited[xx][yy] = 0;
    48         jing--;
    49     }
    50 }
    51 
    52 int main()
    53 {
    54     int t;
    55     scanf("%d", &t);
    56     for(kase=1;kase<=t;kase++){
    57         scanf("%d%d", &c, &r);
    58         solution.clear();
    59         memset(visited, 0, sizeof(int) * 10 * 10);
    60         jing = 1;
    61         flag = false;
    62         visited[1][1] = 1;
    63         solution.push_back(node(1, 1));
    64         dfs(1, 1);
    65         if (flag == false) {
    66             printf("Scenario #%d:
    ", kase);
    67             printf("impossible
    
    ");
    68         }
    69     }
    70     return 0;
    71 }
    View Code

    输出格式

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    多线程爬取图片(生产者-消费者模式)
    数据结构1_C---单链表的逆转
    Java基础面试知识点总结
    Java工程师必备书单
    Java工程师修炼之路(校招总结)
    Java秋招面经大合集
    我的秋招经验分享(已拿BAT头条网易滴滴)
    听说go语言越来越火了?那么请收下这一份go语言书单吧!
    2020还是AI最火?推荐几本深度学习的书籍帮你入门!
    人工智能真的有那么神秘么,推荐一份机器学习入门书单搞定它!
  • 原文地址:https://www.cnblogs.com/yalphait/p/9247147.html
Copyright © 2011-2022 走看看