poj2488
来源:互联网 发布:mysql分布式事务 编辑:程序博客网 时间:2024/06/06 19:14
以下是转载内容 侵删 只是学习记录一下
#include<iostream>
#include <cstdio>
#include <cstring>
using namespacestd;
int cases, p, q, i;
void dfs(int x,int y, int num);//num用来记录是否全部走过即路径长度
const int MAX_N =27;//字典序最多多少
const int dx[8] = {-1,1, -2,2, -2,2, -1,1};
const int dy[8] = {-2, -2, -1, -1,1, 1,2, 2};//只能左右1上下2or左右2上下1
bool visited[MAX_N][MAX_N];
struct Step{
char x, y;
} path[MAX_N];
bool success; //是否成功遍历的标记
int main(){
cin >>cases;
for(int c =0; c < cases; c ++){
success =false;//先全部标记失败
scanf("%d%d", &p, &q);//边界范围
memset(visited,false, sizeof(visited));
visited[1][1] =true; //起点
dfs(1,1, 1);//从a1开始搜索
printf("Scenario #%d:\n", c);
if (success)
{
for (int i =1; i <= p *q; i++)
printf("%c%c",path[i].y,path[i].x);
printf("\n");
}
else
printf("impossible\n");
if (c !=cases)
printf("\n"); //注意该题的换行
}
return0;
}
void dfs(int x,int y, int num)
{
path[num].y = y +'A' - 1; //int 转为 char
path[num].x = x +'0';
if (num ==p * q)//套路如果路径查找到最后一个说明成功
{
success =true;
return;
}
for (int i =0; i < 8; i++)
{
int nx = x +dx[i];
int ny = y +dy[i];//循环遍历各种走法 nx ny是坐标
if (0 < nx && nx <=p && 0 < ny && ny <=q && !visited[nx][ny] && !success)//合法范围内,既未走过也没有成功
{
visited[nx][ny] =true;
dfs(nx, ny, num+1);
visited[nx][ny] =false; //如果没有成功,撤销该步
}
}
}
- poj2488
- POJ2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- Poj2488
- poj2488
- poj2488
- poj2488
- poj2488
- POJ2488
- Scala中的映射
- 分数段
- ubantu16.04TLS fcitx和搜狗拼音安装后无法正常启动的解决方法
- 《锋利的jQuery》读书笔记第一章(jQuery对象)
- 第三方登录之数据库设计
- poj2488
- 网页爬虫《二》之动态网页
- Django 快速搭建博客 第四节(hello world视图)
- Linux下编译OpenJDK源码以及在Eclipse中调试Hotspot虚拟机
- 抽象类练习
- 用GDB调试程序(六)
- 整合ssh——spring容器
- 两个数的最大公约数
- 基于文件操作的留言系统