寒假训练 A - A Knight's 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
dfs
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int p,q;
int flag=0;
bool vis[100][100];
int path[100][100];
int tx[8]={-1,1,-2,2,-2,2,-1,1};
int ty[8]={-2,-2,-1,-1,1,1,2,2};
bool judge(int x,int y)
{
if(x>=1&&y>=1&&x<=p&&y<=q&&!flag&&!vis[x][y]) return true;
return false;
}
void dfs(int x,int y,int step)//这里不知道你们有没有问题,反正我碰到了,就是,这里的x和y,与坐标系不一样
{
path[step][0]=x;//x代表行
path[step][1]=y;//y代表列
if(step==p*q)//直角坐标系中x虽然是横轴,但是x的改变则是列的变化
{
flag=1;
return ;
}
for(int i=0;i<8;i++)
{
int sx=x+tx[i];
int sy=y+ty[i];
if(judge(sx,sy))
{
vis[sx][sy]=1;
dfs(sx,sy,step+1);
vis[sx][sy]=0;
}
}
} int main()
{
int n,c=0;
cin>>n;
while(n--)
{
scanf("%d%d",&p,&q);
printf("Scenario #%d:\n",++c);
memset(vis,0,sizeof(vis));
flag=0;
vis[1][1]=1;
dfs(1,1,1);
if(flag==1)
{
for(int i=1;i<=p*q;i++)
{
printf("%c%d",path[i][1]-1+'A',path[i][0]);
}
printf("\n");
}
else printf("impossible\n");
if(n!=0) printf("\n");
}
return 0;
}
ow many different square letters exist. These are the first q letters of the Latin alphabet: A, . . .
Output
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 题目大意:就是给你p行q列,求马是否可以走完,可以求出路径,不可以输出-1
意思很明确,不过毕竟是英文题,有点难读
寒假训练 A - A Knight's Journey 搜索的更多相关文章
- Poj 2488 A Knight's Journey(搜索)
Background The knight is getting bored of seeing the same black and white squares again and again an ...
- 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 ...
- HDOJ-三部曲一(搜索、数学)- A Knight's Journey
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- 广大暑假训练1(poj 2488) A Knight's Journey 解题报告
题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A (A - Children of the Candy Corn) ht ...
- POJ2488-A Knight's Journey(DFS+回溯)
题目链接:http://poj.org/problem?id=2488 A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Tot ...
- POJ 2488 A Knight's Journey(深搜+回溯)
A Knight's Journey Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- 迷宫问题bfs, A Knight's Journey(dfs)
迷宫问题(bfs) POJ - 3984 #include <iostream> #include <queue> #include <stack> #incl ...
- poj2488 A Knight's Journey裸dfs
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 35868 Accepted: 12 ...
- POJ2488A Knight's Journey[DFS]
A Knight's Journey Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 41936 Accepted: 14 ...
随机推荐
- CEF C++环境搭建
第一步:下载CEF 到这里下载最新版本的CEF http://cefbuilds.com/ 下载解压之后,大概会看到如下图所示的文件 cefclient:是一个比较复杂的示例代码cefsimple:是 ...
- CSS Font文字样式
font-style: /* 文字样式 italic(倾斜) | normal */ font-weight: /* 文字是否加粗 bold | normal(正常) */ font-size: /* ...
- mysql中的data下的数据文件(.FRM、.MYD、.MYI)恢复为数据
记一次mysql中的data文件操作经历 想拿到一个项目的最新的数据,做功能升级使用,备份一份数据同时也作为本地测试数据,文件有些大,我直接通过远程的phpmyadmin程序导出,不能愉快的玩耍,直接 ...
- Java中的集合迭代器
集合的迭代器 任何集合都有迭代器. 任何集合类,都必须能以某种方式存取元素,否则这个集合容器就没有任何意义. 迭代器,也是一种模式(也叫迭代器模式).在java中它是一个对象,其目的是遍历并选中其中的 ...
- MySQL学习(一) SQL基本操作
SQL的基本操作无非就是增删改查(CRUD),根据对操作的对象分类,又可以分为三类,库操作,表操作与数据操作. 库操作 新增数据库 CREATE DATABASE [IF NOT EXISTS] db ...
- 如何把string转换char*类型
需要调用string头文件 ( #include<string> ) 用string里的函数c_str()可以把string转换为char* 例如 char * c_str2=str1. ...
- python爬虫入门---第三篇:自动下载图片
适用的图片网站:美桌 源代码: import requests import re import urllib from bs4 import BeautifulSoup def get_html_t ...
- ES6模块化与常用功能
目前开发环境已经普及使用,如vue,react等,但浏览器环境却支持不好,所以需要开发环境编译,下面介绍下开发环境的使用和常用语法: 一,ES6模块化 1,模块化的基本语法 ES6 的模块自动采用严格 ...
- Centos 6.8 系统升级默认的Python版本
1.编译安装python2.7 # wget https://www.python.org/ftp/python/2.7.12/Python-2.7.12.tgz # Python-2.7.12.tg ...
- Clumsy 利用无线网卡结合Clumsy软件模拟弱网络测试
利用无线网卡结合Clumsy软件模拟弱网络测试 by:授客 QQ:1033553122 实践环境 Clumsy 0.2 下载地址:http://jagt.github.io/clumsy/downlo ...