A Knight's Journey

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 51   Accepted Submission(s) : 17
Problem 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
PKU
 
 
第一次写深搜,感觉还不错
 
#include<iostream>
#include<cstring>
using namespace std;
int m,n;
int c; //已走过的步数
bool f; //标记是否找到答案了
int chess[30][30]={0};
int step[2][8]={{-1, 1,-2, 2,-2,2,-1,1}, //每一种可能的走法,注意要按字典序排列
{-2,-2,-1,-1, 1,1, 2,2}};
char ans1[64];
int ans2[64];
bool move(int i,int j,int k)
{
if(i+step[0][k]>=0&&i+step[0][k]<m&&j+step[1][k]>=0&&j+step[1][k]<n&&!chess[i+step[0][k]][j+step[1][k]])
{
chess[i+step[0][k]][j+step[1][k]]=1;
return true;
}
else
return false;
}
void DFS(int i,int j)
{ ans1[c]=j+'A';
ans2[c]=i+1; if(c==m*n) //找到结果,回退
{
f=true;
return;
}
for(int t=0;t<8;t++) //尝试每一种走法
{
if(move(i,j,t))
{
c++;
DFS(i+step[0][t],j+step[1][t]);
if(c==m*n) //如果找到结果就一直回退
return;
chess[i+step[0][t]][j+step[1][t]]=0; //还原
c--;
}
}
return;
} int main()
{
int T;
cin>>T;
int t=T;
while(T--)
{
int i,j,k;
cin>>m>>n;
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
c=1;
memset(chess,0,sizeof(chess));
chess[i][j]=1;
f=false;
DFS(i,j);
if(f)
{
cout<<"Scenario #"<<t-T<<":"<<endl;
for(k=1;k<=m*n;k++)
cout<<ans1[k]<<ans2[k];
cout<<endl;
break;
}
}
if(f)
break;
}
if(!f)
{
cout<<"Scenario #"<<t-T<<":"<<endl;
cout<<"impossible"<<endl;
}
cout<<endl;
}
}
 
 
 
 

HDOJ-三部曲一(搜索、数学)- A Knight's Journey的更多相关文章

  1. A Knight's Journey 分类: POJ 搜索 2015-08-08 07:32 2人阅读 评论(0) 收藏

    A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35564 Accepted: 12119 ...

  2. POJ2488-A Knight's Journey(DFS+回溯)

    题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Tot ...

  3. POJ 2488 A Knight's Journey(深搜+回溯)

    A Knight's Journey Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  4. 迷宫问题bfs, A Knight's Journey(dfs)

    迷宫问题(bfs) POJ - 3984   #include <iostream> #include <queue> #include <stack> #incl ...

  5. poj2488 A Knight's Journey裸dfs

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 35868   Accepted: 12 ...

  6. POJ2488A Knight's Journey[DFS]

    A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 41936   Accepted: 14 ...

  7. POJ 2488 A Knight's Journey(DFS)

    A Knight's Journey Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 34633Accepted: 11815 De ...

  8. A Knight's Journey 分类: dfs 2015-05-03 14:51 23人阅读 评论(0) 收藏

    A Knight’s Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 34085 Accepted: 11621 ...

  9. poj2488 A Knight's Journey

      A Knight's Journey Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24840   Accepted:  ...

随机推荐

  1. C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组

    #include<stdio.h> #include<stdlib.h> #include<windows.h> /* 举列子说明什么是函数指针 */ //以一个加 ...

  2. ARM指令集(上)

    ADuC702x可以用两套指令集:ARM指令集和Thumb指令集.本小节介绍ARM指令集.在介绍ARM指令集之前,先介绍指令的格式. A.2.1  指令格式         (1)基本格式       ...

  3. python 循环设计

    for循环 1.range()用法 for循环后的in跟随一个序列的画,循环每次使用的序列元素而不是序列的下标 例:s='abcdefg' for i in range(0,len(s),3): pr ...

  4. MD4C/CO46/MD04一个很棒的工单缺料分析

    大家好~~~ 之前在MD04物料分析的时候,看到有订单预留,双击有个订单报告可以显示一个订单物料是否缺料清单 这个单独的工单分析可以在T-code:MD4C,CO46查看,如果只是单独的使用,那么这两 ...

  5. BOM组件物料重复检查

    好吧,今天同事让做个BOM组件物料重复检查 网上有很多例子都是在保存的时候检查的,用的是BADI :BOM_UPDATE 自己也试了一下,麻烦....很麻烦...尤其是在重复检查的时候: METHOD ...

  6. MASM6.15汇编程序例子

    /***************通过调用(INT 21H)表中的01h号功能号从键盘输入一个字符并回显到视频显示器上*****************/ DATAS SEGMENT ;此处输入数据段代 ...

  7. 在浏览器中将表格导入到本地的EXCEL文件,注意控制内存

    if ($export_flag == 1) { $rr = $this->mdl->test($test); header("Content-Type: application ...

  8. Greenplum——升级的分布式PostgresSQL

    Greenplum数据库基于PostgreSQL开源技术.本质上讲,它是多个PostgreSQL实例一起充当一个数据库管理系统.Greenplum以PostgreSQL 8.2.15为基础构建,在SQ ...

  9. 一段linux shell 代码涉及for循环和if esle

    if [ 0 -ne $# ]; then echo "USAGE: prog [IN]input_file" >&2; exit 1;fisource /etc/p ...

  10. JBoss JMX登录需要用户名密码的解决办法

    /opt/jboss/eap5.1.2/jboss-as/server/default/conf/props/jmx-console-users.properties 取消#admin=admin的注 ...