题目链接

题目大意:

给定一个矩阵,马的初始位置在(0,0),要求给出一个方案,使马走遍所有的点。

列为数字,行为字母,搜索按字典序。

分析:

用 vis[x][y] 标记是否已经访问。因为要搜索所有的可能,所以没搜索完一次要把vis[x][y]标记为未访问。详情见代码。

用 p[x][y] 表示 (x,y)点的祖先,以便输出路径。

dfs搜索即可。

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#include <cstring> using namespace std; const int maxn = ;
const int VAL = ; int dx[] = {-, , -, , -, , -, }; //搜索顺序是字典序
int dy[] = {-, -, -, -, , , , }; bool vis[maxn][maxn];
int p[maxn][maxn], ex, ey;
int cnt, total, n, m; bool dfs(int x, int y, int px, int py) {
p[x][y] = px*VAL+py; cnt++; //如果已经走遍的点等于总点数,表示已经全部访问完
if(cnt == total) {
ex = x; ey = y;
return true;
} for(int d=; d<; d++) {
int nx = x+dx[d];
int ny = y+dy[d]; if(nx < || ny < || nx >= n || ny >= m) continue;
if(vis[nx][ny]) continue; vis[nx][ny] = true; //标记已访问 if(dfs(nx, ny, x, y)) return true; vis[nx][ny] = false; //标记未访问
cnt--; //已访问的数要减一
} return false;
} void print(int x, int y) {
if(x == && y == ) return;
int e = p[x][y];
int px = e/VAL, py = e%VAL;
print(px, py);
printf("%c%d", y+'A', x+); //y对应字母,x对应数字
} int main(){
int T;
scanf("%d", &T);
for(int kase=; kase<=T; kase++) {
scanf("%d%d", &n, &m);
cnt = ; total = n*m; memset(vis, , sizeof(vis)); vis[][] = true; printf("Scenario #%d:\n", kase); if(dfs(, , , )) { //有方案,输出
printf("A1");
print(ex, ey);
putchar('\n');
}
else printf("impossible\n");
putchar('\n');
} return ;
}

POJ2248 A Knight's Journey(DFS)的更多相关文章

  1. POJ2488A Knight's Journey[DFS]

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

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

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

  3. A Knight's Journey(dfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 25950   Accepted: 8853 Description Back ...

  4. POJ2488:A Knight's Journey(dfs)

    http://poj.org/problem?id=2488 Description Background The knight is getting bored of seeing the same ...

  5. [poj]2488 A Knight's Journey dfs+路径打印

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45941   Accepted: 15637 Description Bac ...

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

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

  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裸dfs

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

随机推荐

  1. Shiro Quartz之Junit測试Session管理

    Shiro的quartz主要API上提供了org.apache.shiro.session.mgt.quartz下session管理的两个类:QuartzSessionValidationJob和Qu ...

  2. Android Touch系统简介(二):实例详解onInterceptTouchEvent与onTouchEvent的调用过程

    上一篇文章主要讲述了Android的TouchEvent的分发过程,其中有两个重要的函数:onInterceptTouchEvent和onTouchEvent,这两个函数可被重装以完成特定的逻辑.on ...

  3. 基于SSH的数据库中图片的读写

    近期项目中遇到了这个问题,网上查了一些资料所谓是零零散散,这里写篇博文做个笔记. 注:这篇博文中部分类的属性声明未列出,应该不算难,基本都是以private 类型 名称 格式声明,然后配getter ...

  4. Java Math 类中的新功能--浮点数

    Java™语言规范第 5 版向 java.lang.Math和 java.lang.StrictMath添加了 10 种新方法,Java 6 又添加了 10 种.这个共两部分的系列文章的 第 1 部分 ...

  5. Java基础知识强化之集合框架笔记12:Collection集合存储字符串并遍历

    1.  Collection集合存储字符串并遍历 分析: (1)创建集合对象 (2)创建字符串对象 (3)把字符串对象添加到集合中 (4)遍历集合 2. 代码示例: package cn.itcast ...

  6. python练习程序_员工信息表_基本实例

    python实现增删改查操作员工信息文件,可进行模糊查询: http://edu.51cto.com/lesson/id-13276.html http://edu.51cto.com/lesson/ ...

  7. C#中的占位符

    当我们需要在屏幕上输出一句话的时候,如果不断的使用+来连接各个字符串,一是容易出错,二是代码显示的非常乱.这时候,占位符就能够发挥作用! 占位符: string name="张三" ...

  8. http方法

    http method(方法):1.get 从服务器获取资源2.post 向服务器发送资源3.put 向服务器推送资源4.delete 告诉服务器删除某个资源5.head 告诉服务器返回数据时不需要返 ...

  9. PHP 实现无限极栏目分类

    首先,创建一个DB CREATE TABLE IF NOT EXISTS `class` ( `id` mediumint(6) NOT NULL AUTO_INCREMENT, `title` va ...

  10. singleTask TaskAffinity allowTaskReparenting

    关于singleTask TaskAffinity allowTaskReparenting 一.Activity的LaunchMode 1.standard 2.singleTop:FLAG_ACT ...