• A Knight's Journey POJ 2488


    Knight's Journey
    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    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
     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 int main()
     5 {
     6     int T;
     7     int i,j,k,p,q,z,flg,u,v,cas=0;
     8     int x[100],y[100],t[100],d[10][10];
     9     int a[9]={0,-1,1,-2,2,-2,2,-1,1};
    10     int b[9]={0,-2,-2,-1,-1,1,1,2,2};
    11     
    12     scanf("%d",&T);
    13     while(T--)
    14     {
    15         memset(t,0,sizeof(t));
    16         memset(d,0,sizeof(d));
    17         scanf("%d %d",&p,&q);
    18         i=1;z=0;
    19         x[i]=1,y[i]=1,d[i][i]=1;
    20         while(i>0)
    21         {
    22             flg=0;
    23             for(k=t[i]+1;k<=8;k++)
    24             {
    25                 u=x[i]+a[k],v=y[i]+b[k];
    26                 if(u>0 && u<=p && v>0 && v<=q && d[u][v]==0)
    27                 {
    28                     x[i+1]=u,y[i+1]=v,d[u][v]=1;
    29                     t[i]=k;
    30                     flg=1;
    31                     break;
    32                 }
    33             }
    34 
    35             if(flg==1 && i==p*q-1)
    36             {
    37                 z=1;
    38                 break;
    39             }
    40 
    41             else if(flg==1)
    42                 i++;
    43             else
    44             {
    45                 t[i]=d[x[i]][y[i]]=0;
    46                 i--;
    47             }
    48         }
    49         if(p==1 && q==1)
    50             z=1;
    51         printf("Scenario #%d:
    ",++cas);
    52         if(z==1)
    53         {
    54             for(i=1;i<=p*q;i++)
    55             {
    56                 printf("%c%d",'A'+y[i]-1,x[i]);
    57             }
    58             printf("
    ");
    59         }
    60         else
    61         {
    62             printf("impossible
    ");
    63         }
    64         printf("
    ");
    65     }
    66     return 0;
    67 }
    View Code
  • 相关阅读:
    Android学习笔记(四十):Preference的使用
    我的Android笔记(十一)——使用Preference保存设置
    Vim简明教程【CoolShell】
    普通人的编辑利器——Vim
    终端shell显示当前git分支_修订版
    代码规范须知_V1.0_20140703
    Android 4.4源码编译过程
    一个帖子学会Android开发四大组件
    什么是软件质量?
    软件配置管理的作用?软件配置包括什么?
  • 原文地址:https://www.cnblogs.com/cyd308/p/4771404.html
Copyright © 2020-2023  润新知