zoukankan      html  css  js  c++  java
  • [POJ2488] A Knight's Journey

    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
    

    Source

    TUD Programming Contest 2005, Darmstadt, Germany

    题解

    题目大意: 任选一个起点,按照国际象棋马的跳法,不重复的跳完整个棋盘,如果有多种路线则选择字典序最小的路线;否则输出“impossible”。

    按字典序枚举每一个点作起点即可。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int N=26,fq[9][2]={{0,0},{-2,-1},{-2,1},{-1,-2},{-1,2},{1,-2},{1,2},{2,-1},{2,1}};//注意字典序
    int p,q,pq,Ansx[N],Ansy[N];
    bool G[N][N],flag;
    
    bool Ok(int x,int y)
    {
    	if(x<1||x>q||y<1||y>p) return 1;
    	return G[x][y];
    }
    
    void Dfs(int x,int y,int step)
    {
    	if(flag) return;
    	Ansx[step]=x,Ansy[step]=y;
    	if(step==pq) {flag=1;return;}
    	int nx,ny;
    	for(int i=1;i<=8;++i)
    	{
    		nx=x+fq[i][0],ny=y+fq[i][1];
    		if(Ok(nx,ny)) continue;
    		G[nx][ny]=1;
    		Dfs(nx,ny,step+1);
    		if(flag) return;
    		G[nx][ny]=0;
    	}
    }
    
    void Print()
    {
    	for(int i=1;i<=pq;++i) printf("%c%d",Ansx[i]+64,Ansy[i]);
    	puts("
    ");
    }
    
    int main()
    {
    	int T; scanf("%d",&T);
    	for(int i=1;i<=T;++i)
    	{
    		flag=0; memset(G,0,sizeof(G));
    		scanf("%d%d",&p,&q),pq=p*q;
    		for(int j=1;j<=q;++j)
    		{
    			for(int k=1;k<=p;++k)
    			{
    				G[j][k]=1;
    				Dfs(j,k,1);
    				if(flag) break;
    				G[j][k]=1;
    			}
    			if(flag) break;
    		}
    		if(flag) printf("Scenario #%d:
    ",i),Print();
    		else printf("Scenario #%d:
    impossible
    
    ",i);
    	}
    	return 0;
    }
    

    本文作者:OItby @ https://www.cnblogs.com/hihocoder/

    未经允许,请勿转载。

  • 相关阅读:
    Codeforces 1255B Fridge Lockers
    Codeforces 1255A Changing Volume
    Codeforces 1255A Changing Volume
    leetcode 112. 路径总和
    leetcode 129. 求根到叶子节点数字之和
    leetcode 404. 左叶子之和
    leetcode 104. 二叉树的最大深度
    leetcode 235. 二叉搜索树的最近公共祖先
    450. Delete Node in a BST
    树的c++实现--建立一棵树
  • 原文地址:https://www.cnblogs.com/hihocoder/p/11381065.html
Copyright © 2011-2022 走看看