C - 搜索
Description
BackgroundThe 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
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
#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 - 搜索的更多相关文章
- SQLSERVER走起微信公众帐号已经开通搜狗微信搜索
SQLSERVER走起微信公众帐号已经开通搜狗微信搜索 请打开下面链接 http://weixin.sogou.com/gzh?openid=oIWsFt-hiIb_oYqQHaBMoNwRB2wM ...
- solr_架构案例【京东站内搜索】(附程序源代码)
注意事项:首先要保证部署solr服务的Tomcat容器和检索solr服务中数据的Tomcat容器,它们的端口号不能发生冲突,否则web程序是不可能运行起来的. 一:solr服务的端口号.我这里的sol ...
- SQLServer地址搜索性能优化例子
这是一个很久以前的例子,现在在整理资料时无意发现,就拿出来再改写分享. 1.需求 1.1 基本需求: 根据输入的地址关键字,搜索出完整的地址路径,耗时要控制在几十毫秒内. 1.2 数据库地址表结构和数 ...
- HTML5轻松实现搜索框提示文字点击消失---及placeholder颜色的设置
在做搜索框的时候无意间发现html5的input里有个placeholder属性能轻松实现提示文字点击消失功能,之前还傻傻的在用js来实现类似功能... 示例 <form action=&quo ...
- bzoj1079--记忆化搜索
题目大意:有n个木块排成一行,从左到右依次编号为1~n.你有k种颜色的油漆,其中第i种颜色的油漆足够涂ci个木块.所有油漆刚好足够涂满所有木块,即c1+c2+...+ck=n.相邻两个木块涂相同色显得 ...
- bzoj3208--记忆化搜索
题目大意: 花花山峰峦起伏,峰顶常年被雪,Memphis打算帮花花山风景区的人员开发一个滑雪项目. 我们可以把风景区看作一个n*n的地图,每个点有它的初始高度,滑雪只能从高处往低处滑[严格大于] ...
- Android中通过ActionBar为标题栏添加搜索以及分享视窗
在Android3.0之后,Google对UI导航设计上进行了一系列的改革,其中有一个非常好用的新功能就是引入的ActionBar,他用于取代3.0之前的标题栏,并提供更为丰富的导航效果.Action ...
- 一步步开发自己的博客 .NET版(5、Lucenne.Net 和 必应站内搜索)
前言 这次开发的博客主要功能或特点: 第一:可以兼容各终端,特别是手机端. 第二:到时会用到大量html5,炫啊. 第三:导入博客园的精华文章,并做分类.(不要封我) 第四:做 ...
- Go语言实战 - 我需要站内搜索
山坡网的用户抱怨"为什么搜索'二鬼子李富贵'找不到'二鬼子汉奸李富贵'?我用百度搜都能找到." 当时我就滴汗了,用户说的有道理,应该要能搜索到. 之前的方案很简单,用户输入的字串会 ...
- Entity Framework 6 Recipes 2nd Edition(13-4)译 -> 有效地创建一个搜索查询
问题 你想用LINQ写一个搜索查询,能被转换成更有效率的SQL.另外,你想用EF的CodeFirst方式实现. 解决方案 假设你有如下Figure 13-6所示的模型 Figure 13-6. A s ...
随机推荐
- blade and soul Personal Combos
Personal Combos Since Blade and Soul is mainly based on skills, the game is more interesting after y ...
- spark示例
1)java(App.java) package com.ejiajie.bi.hello; import org.apache.spark.api.java.JavaSparkContext; im ...
- 总结4点对学习Linux有帮助的建议(纯干货)
学习需要足够的毅力和耐心 有些人把Linux运维看作一项冗长而枯燥的工作:有些人把linux运维看作一项得力的工具.如果是前者建议还是改变一下认识,不然不建议入门这行.毕竟linux运维工作是对人的毅 ...
- BaseHttpListActivity,几行代码搞定Android Http列表请求、加载和缓存
Android开发中,向服务器请求一个列表并显示是非常常见的需求,但实现起来比较麻烦,代码繁杂. 随着应用的更新迭代,这种需求越来越多,我渐渐发现了实现这种需求的代码的共同点. 于是我将Activit ...
- WireShark网络性能分析
最近生产上出现一个性能问题,表现为:行情延时5s左右.从log一路追查下去,发现是我们自己写的一个行情网关(部署在xx.xx.xx.132)<->第三方的中转网关(部署在xx.xx.xx. ...
- php发送邮件
<?php header("content-Type: text/html; charset=utf-8"); require("class.phpmailer.p ...
- wordpress及其simple_press插件完成 的中文论坛
wordpress 及 其simple_press插件的论坛功能 开发准备: 一直从事java及as3方面的开发,首次听同事提起PHP的wordpress,于是上网查了下, 才知道这款软件的强大,转而 ...
- Uart、SPI和I2C的区别
串口通信:UART.SPI.I2C区别[引用] 1.UART就是两线,一根发送一根接收,可以全双工通信,线数也比较少.数据是异步传输的,对双方的时序要求比较严格,通信速度也不是很快.在多机通信上面 ...
- mysql连接查询和子查询
一.连接查询 1.交叉连接 就是从一张表的一条记录去连接另一张表中的所有记录,并且保存所有的记录,其中包括两个表的所有的字段! 从结果上看,就是对两张表做笛卡尔积! 笛卡尔积也就是两个表中所有可能的连 ...
- XCode修改公司名称和作者名称
新建的文件最上方都会有一段类似如下的版权声明 // // ViewController.m // CBDemoProject001 // // Created by CB on 16/3/17. ...