Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Submit Status

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 经典的DFS,大概题意就是让骑士走遍棋盘的所有格子。一路走到黑,看这条路能不能使骑士把格子都走完。
 #include<cstdio>
#include<string.h>
using namespace std;
int vis[][];
char str[];
int s[][]={-,-,-,,-,-,-,,,-,,,,-,,};//这种处理方式很常用的,要记住,即骑士当前所在位置的周围所能走到的位置
int p,q;
int dfs(int x,int y,int sum,int cnt)
{
if(sum==p*q) return ;//sum是记录走过的格子
int x1,y1;
for(int i=;i<;i++)
{
x1=x+s[i][];
y1=y+s[i][];
if(x1>=&&x1<q&&y1>=&&y1<p&&!vis[x1][y1])//边界条件及判断是否访问过吗
{
vis[x1][y1]=;
str[cnt+]=x1+'A';//开一个str数组,用来记录
str[cnt+]=y1+'';
if(dfs(x1,y1,sum+,cnt+))//记得这里要写成sum+1,cnt+2
return ;
vis[x1][y1]=;//回溯
}
}
return ;
}
int main()
{
int t;
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%d %d",&p,&q);
memset(vis,,sizeof(vis));
memset(str,,sizeof(str));
str[]='A';
str[]='';
vis[][]=;
if(dfs(,,,)){
printf("Scenario #%d:\n",i);
for(int j=;j<strlen(str);j++)
printf("%c",str[j]);
printf("\n\n");//记得每组数据输出后,有个空行
}
else{
printf("Scenario #%d:\n",i);
printf("impossible\n\n");
}
}
return ;
}

C - 搜索的更多相关文章

  1. SQLSERVER走起微信公众帐号已经开通搜狗微信搜索

    SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...

  2. solr_架构案例【京东站内搜索】(附程序源代码)

    注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...

  3. SQLServer地址搜索性能优化例子

    这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...

  4. HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置

    在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...

  5. bzoj1079--记忆化搜索

    题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...

  6. bzoj3208--记忆化搜索

    题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目.    我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...

  7. Android中通过ActionBar为标题栏添加搜索以及分享视窗

    在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...

  8. 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)

    前言 这次开发的博客主要功能或特点:    第一:可以兼容各终端,特别是手机端.    第二:到时会用到大量html5,炫啊.    第三:导入博客园的精华文章,并做分类.(不要封我)    第四:做 ...

  9. Go语言实战 - 我需要站内搜索

    山坡网的用户抱怨"为什么搜索'二鬼子李富贵'找不到'二鬼子汉奸李富贵'?我用百度搜都能找到." 当时我就滴汗了,用户说的有道理,应该要能搜索到. 之前的方案很简单,用户输入的字串会 ...

  10. Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询

    问题 你想用LINQ写一个搜索查询,能被转换成更有效率的SQL.另外,你想用EF的CodeFirst方式实现. 解决方案 假设你有如下Figure 13-6所示的模型 Figure 13-6. A s ...

随机推荐

  1. thunkify和co的邂逅

    上一篇说到thunkify的作用,这一篇说一下thunkify和co的集合 co中有一块代码 /** * Convert a thunk to a promise. * * @param {Funct ...

  2. XP 安装不了framework 4.0 的解决方法

    第一步: 如果是XP系统: 1.开始——运行——输入cmd——回车——在打开的窗口中输入net stop WuAuServ 2.开始——运行——输入%windir% 3.在打开的窗口中有个文件夹叫So ...

  3. 制造行业流程管理的“IPO”思维

    流程管理是企业从流程角度出发,关注流程是否增值的一套管理体系.从认识流程.到建立流程.到管理流程.再到优化流程,企业管理人员要去除不增值和低价值的流程,减少员工犯错误的机会,建立一套卓越的流程体系. ...

  4. React Native 一个组件styles BUG

    'use strict'; var React = require('react-native'); var { StyleSheet, PanResponder, View, Text } = Re ...

  5. C#文件流读写文件的简单winform实现

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. VS 工程的 输出路径和工作路径的区别

    输出路径,是vs编译项目生成可执行文件的路径:工作路径是环境变量,比如我们在程序中写相对路径,就是以这个路径为基础的.在默认情况下,输出路径和工作路径都不写的话,默认是程序的bin下面的debug或者 ...

  7. MYSQL单双向同步

    Master:192.168.1.101 Slave  :192.168.1.102 单向同步(一) 进入Master启动MYSQL [root@localhost ~]# service mysql ...

  8. jQuery原生框架-----------------核心框架

    // 1.自调防止全局变量污染(function( window ) { var version = '1.0.0'; var document = window.document; var arr ...

  9. 华为手机调试显示log日志

    华为手机默认状态手机log为关闭状态,所以看不到详细错误信息. 手机拨号*#*#2846579#*#*,进入projectmenu--后台设置--LOG设置--LOG开关--打开 勾选AP日志   C ...

  10. git pull错误

    1. Pull is not possible because you have unmerged files. 症状:pull的时候 $ git pull Pull is not possible ...