EOJ2253

本文深入探讨了游戏开发领域的核心技术,包括游戏引擎、动画、3D空间视频等关键概念及其实现方法,为游戏开发者提供了一站式的技术指南。
A Knight's Journey

Time Limit:1000MSMemory Limit:65536KB
Total Submit:209Accepted:80

Description

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?

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


题目:eoj2253

 

题目分析:

用dfs按字典序搜索,存储遍历路径,若能成功输出路径。因为要按字典序搜索,要注意每次dfs的顺序,从A0位置按字典序对每个点每次按dy[8]={-2,-2,-1,-1,1,1,2,2},dx[8]={-1,1,-2,2,-2,2,-1,1}的顺序遍历下一层即可。用visited[28][28]标记点是否被访问过。往下一层遍历时,按上述顺序,若该点未被访问则更新当前路径,标记该点被访问,并往下一层遍历。若无法往下一层遍历则回溯,恢复上一层的状态,标记该点未被访问。当遍历深度达到棋盘个数点时则成功。

 

AC代码:

#include <iostream>

#include <cstring>

#include <cstdio>

 

using namespace std;

int dy[8]={-2,-2,-1,-1,1,1,2,2},dx[8]={-1,1,-2,2,-2,2,-1,1};

bool success;

struct Node{

    int x,y;

}a[100];

bool visited[28][28];

 

void dfs(int x,int y,int p,int q,int deep){

    if(success) return;

    visited[x][y]=true;

    a[deep].x=x;a[deep].y=y;

    if(deep==p*q-1){

        for(int j=0;j<p*q;++j)

           printf("%c%d",a[j].y+'A',a[j].x+1);

        printf("\n\n");

        success=true;return;

    }

    else{

        for(int i=0;i<8;++i){

            intxx=x+dx[i],yy=y+dy[i];

            if(0<=xx &&xx<p && 0<=yy && yy<q && !visited[xx][yy]){

               dfs(xx,yy,p,q,deep+1);

                visited[xx][yy]=false;

            }

        }

    }

}

 

int main()

{

     int t,i,j;

     cin>>t;

     for(int ii=1;ii<=t;++ii){

        int p,q;

        cin>>p>>q;

        success=false;

        cout<<"Scenario#"<<ii<<":"<<endl;

        for(j=0;j<q;++j){

            for(i=0;i<p;++i){

               memset(visited,false,sizeof(visited));

                dfs(i,j,p,q,0);

                if(success)

                    break;

            }

            if(success) break;

        }

        if(!success) cout<<"impossible"<<endl<<endl;

     }

    return 0;

}

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值