POJ 2488 A Knight's Journey

Source: Internet
Author: User

A Knight's JourneyTime Limit: 1000 MS Memory Limit: 65536 KTotal Submissions: 23108 Accepted: 7819 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 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: ,... 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 ches Sboard 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: A1 Scenario #2: impossible Scenario #3: A1B3C1A2B4C2A3B1C3A4B2C4Source TUD Programming Contest 2005, Darmstadt, Germany will do it after reading the question. The previous horses were online when they were jumping. This question is really not suitable for the square, it is good to convert it to online [cpp] # include <stdio. h> # include <string. h> int status [100] [100], s; int vex [] = {-2,-, 2,-1,-, 1 }; int vey [] = {-1000,-1000,-1000,-}; char path1 [], prepath1 []; int path2 [], prepath2 [1000], top, n, m, key, k; int main () {void dfs (int x, int y); int I, j, t, tem; scanf ("% d", & t); tem = 1; while (t --){ Scanf ("% d", & n, & m); memset (status, 0, sizeof (status )); if (n = 1 & m = 1) {printf ("Scenario # % d: \ n", tem); tem ++; printf ("A1 \ n"); printf ("\ n"); continue;} for (I = 1; I <= n; I ++) {for (j = 1; j <= m; j ++) {top = 0; s = 1; memset (status, 0, sizeof (status )); prepath1 [top] = 'a + J-1; prepath2 [top ++] = I; status [I] [j] = 1; key = 0; k = 0; dfs (I, j); if (k = 1) {break ;}} if (j! = M + 1) {break ;}} printf ("Scenario # % d: \ n", tem); tem ++; if (I! = N + 1) {for (I = 0; I <= n m-1; I ++) {printf ("% c % d", path1 [I], path2 [I]);} printf ("\ n");} else {printf ("impossible \ n");} printf ("\ n ");} return 0;} void dfs (int x, int y) {int I, j, xend, yend, change; for (I = 0; I <= 7; I ++) {xend = x + vex [I]; yend = y + vey [I]; if (xend> = 1 & xend <= n & ¥ d> = 1 & ¥ d <= m &&! Status [xend] [yend]) {s ++; prepath1 [top] = yend + 'a'-1; prepath2 [top ++] = xend; status [xend] [yend] = 1; if (s = n * m) {k = 1; if (key = 0) {for (j = 0; j <= m-1; j ++) {path1 [j] = prepath1 [j]; path2 [j] = prepath2 [j];} key = 1 ;} else {change =-1; for (j = 0; j <= m-1; j ++) {if (path1 [j]> prepath1 [j]) {change = 1; break;} else if (path1 [j] <prepath1 [j]) {change = 0; break ;} else {if (path2 [j]> prepath2 [j]) {change = 1; break;} else if (path2 [j] <prepath2 [j]) {change = 0; break ;}}if (change = 1) {for (j = 0; j <= n m-1; j ++) {path1 [j] = prepath1 [j]; path2 [j] = prepath2 [j] ;}}} else {dfs (xend, yend);} s --; top --; status [xend] [yend] = 0 ;}}}

Related Article

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.