Quick cut poj2488 a knight's journey

Source: Internet
Author: User
A knight's journey
Time limit:1000 ms   Memory limit:65536 K
Total submissions:31195   Accepted:10668

Description

Background 
The knight is getting bored of seeing the same black and white squares 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 between different square numbers 1 ,..., P exist, Q describes how many different square letters exist. these are the first Q letters of the Latin alphabet: ,...

Output

The output for every scenario begins with a line ining "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 shoshould 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 shoshould output impossible on a single line.

Sample Input

31 12 34 3

Sample output

Scenario #1: a1scenario #2: impossiblescenario #3: a1b3c1a2b4c2a3b1c3a4b2c4

Actual Time: 20 min
Situation: cccca pay attention to Java and random changes
Note: one group has empty rows, but the last group has no 2 lexicographic orders.
#include <cstdio>#include <cstring>using namespace std;int n,m;typedef unsigned long long ull;bool used[8][8];char heap[64][3];const int dx[8]={-2,-2,-1,-1,1,1,2,2},dy[8]={-1,1,-2,2,-2,2,-1,1};bool judge(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m)return true;    return false;}bool dfs(int x,int y,int cnt){    used[x][y]=true;    heap[cnt][0]=x+‘A‘;    heap[cnt++][1]=y+‘1‘;    if(cnt==n*m)return true;    for(int i=0;i<8;i++){        int tx=x+dx[i],ty=y+dy[i];        if(judge(tx,ty)&&!used[tx][ty]){            if(dfs(tx,ty,cnt))return true;        }    }    used[x][y]=false;    return false;}int main(){    int T;scanf("%d",&T);    for(int ti=1;ti<=T;ti++){        scanf("%d%d",&m,&n);        memset(used,0,sizeof(used));        bool fl=dfs(0,0,0);        printf("Scenario #%d:\n",ti);        if(fl){            for(int i=0;i<n*m;i++){                printf("%s",heap[i]);            }            puts("");        }        else {            puts("impossible");        }        if(ti<T)puts("");    }    return 0;}

  

Quick cut poj2488 a knight's journey

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.