题目传送门

本题知识点:深度优先搜索 + 回溯 + 剪枝 + 字典序

题意是给你一个由 p,q 组成一个矩形的棋盘,让你用马棋在这上面走,是否能一线走完这 p * q 个格子。

关于这条路线是怎么走的,自己动手在纸上模拟一下样例3棋子行走的过程就可以了。

所以这种一线走完的题意可以很清楚地想到是深搜

我第一次写的时候是没有回溯的,没有回溯的话,就会走回路,提交了一遍WA了,所以这里是不能走回路的,必须要用回溯。

如果都能走到的话,那所走的步数肯定是 p * q,所以这里是判断是否已走完的一个判断。当已达成的话,所得到的路径肯定是答案的路径(至于为什么,我也说不出个好证明来tclquq),得到这个路径后,就要进行剪枝,即中断搜索。

题目输出要求是路径输出要按照字典序输出,所以深搜时的方向一定先要按照字典序方向去走(这里也请大家自己思考一下,怎样走才是最小的字典序)。因为这个字典序差点搞崩我心态,所以大家一定要耐心看清楚题目啊,当思路都没问题时,重新读下题目是很重要的。另外,输出时候还要多一个换行符。

下面请看下代码吧

// POJ 2488
#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std; bool take[10][10];
int T, H, W;
int ans_size, temp_size;
string ans[30], temp[30];
bool ok;
//vector<string> temp, ans; // WA 的行走模式
//int rh[] = { 2, 2, 1, -1, -2, -2, -1, 1 };
//int rw[] = { -1, 1, 2, 2, 1, -1, -2, -2 };
// AC 的行走模式
int rh[] = { -1, 1, -2, 2, -2, 2, -1, 1 };
int rw[] = { -2, -2, -1, -1, 1, 1, 2, 2 }; void dfs(int h, int w){
take[h][w] = true; if(temp_size == H * W){
ok = true;
ans_size = 0;
for(int i = 0; i < temp_size; i++){
// ans.push_back(temp[i]);
ans[ans_size++] = temp[i];
}
return ;
} for(int i = 0; i < 8; i++){
int nh = h + rh[i], nw = w + rw[i];
if(1 <= nh && nh <= H && 1 <= nw && nw <= W && !take[nh][nw]){
string a = "";
a += (char)(nw - 1 + 'A');
a += (char)(nh + '0');
temp[temp_size++] = a;
// temp.push_back(a);
dfs(nh, nw);
temp_size--; // 回溯
if(ok) return ; // 剪枝
// temp.pop_back();
}
}
take[h][w] = false;
} int main()
{
// freopen("test.txt", "r", stdin);
scanf("%d", &T);
for(int k = 1; k <= T; k++) {
ok = false;
memset(take, false, sizeof(take));
// ans.clear();
// temp.clear();
ans_size = temp_size = 0;
scanf("%d %d", &H, &W);
string a = "A1";
temp[temp_size++] = a;
// temp.push_back(a);
dfs(1, 1); printf("Scenario #%d:\n", k);
if(ans_size == H * W){
for(int i = 0; i < ans_size; i++){
cout << ans[i];
} cout << endl;
}
else cout << "impossible\n";
cout << endl;
}
return 0;
}

【POJ2488】A Knight's Journey的更多相关文章

  1. 【leetcode】688. Knight Probability in Chessboard

    题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...

  2. 【UVa】439 Knight Moves(dfs)

    题目 题目     分析 没有估价函数的IDA......     代码 #include <cstdio> #include <cstring> #include <a ...

  3. 【leetcode】935. Knight Dialer

    题目如下: A chess knight can move as indicated in the chess diagram below:  .            This time, we p ...

  4. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  5. 【LeetCode】688. Knight Probability in Chessboard 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/knight-pr ...

  6. 【BZOJ】2657: [Zjoi2012]旅游(journey)(树的直径)

    题目 传送门:QWQ 分析 在任意两个不相邻的点连一条线,求这条线能穿过几个三角形. 建图比较讲究(详见代码) 求树的直径. 代码 #include <bits/stdc++.h> usi ...

  7. 【题解】CF356A Knight Tournament

    题面传送门 本蒟蒻想练习一下并查集,所以是找并查集标签来这里的.写题解加深理解. 解决思路 自然,看到区间修改之类很容易想到线段树,但本蒟蒻线段树会写挂,所以这里就讲比较简单的并查集思路. 并查集的核 ...

  8. CDOJ 92 – Journey 【LCA】

    [题意]给出一棵树,有n个点(2≤N≤105),每条边有权值,现在打算新修一条路径,给出新路径u的起点v,终点和权值,下面给出Q(1≤Q≤105)个询问(a,b)问如果都按照最短路径走,从a到b节省了 ...

  9. 【bfs】Knight Moves

    [题目描述] 输入nn代表有个n×nn×n的棋盘,输入开始位置的坐标和结束位置的坐标,问一个骑士朝棋盘的八个方向走马字步,从开始坐标到结束坐标可以经过多少步. [输入] 首先输入一个nn,表示测试样例 ...

随机推荐

  1. 学习笔记之Vim

    Vim (text editor) - Wikipedia https://en.wikipedia.org/wiki/Vim_(text_editor) Vim (/vɪm/;[4] a contr ...

  2. CI框架结合jQuery实现上传多张图片即时显示

    一.Html代码如下: <tr> <td class="txt_r"><span class="orange">* < ...

  3. unity shader入门(四):高光

    高光反射计算公式(phong模型)Cspecular=(Clight*Mspecular)max(0,v*r)mgloss mgloss为材质的官泽度,也成反射度,控制高光区域亮点有多大 Mspecu ...

  4. CentOS 7 使用 firewalld 打开关闭防火墙与端口

    1.firewalld的基本使用启动: systemctl start firewalld关闭: systemctl stop firewalld查看状态: systemctl status fire ...

  5. redis启动异常处理一例

    rm -rf /var/log/redis/redis.log echo "net.core.somaxconn= 1024" >> /etc/sysctl.conf ...

  6. Codeforces D. Little Elephant and Interval(思维找规律数位dp)

    题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...

  7. POI不同浏览器导出名称处理

    /** * * @Title: encodeFileName * @Description: 导出文件转换文件名称编码 * @param @param fileNames * @param @para ...

  8. MSDS 596 Homework

    MSDS 596 Homework 10 Due November 28 2017Notes. The lowest grade among all eleven homework will be d ...

  9. Java 14 周作业

    题目:编写一个应用程序,输入一个目录和一个文件类型,显示该目录下符合该类型的所有文件.之后,将这些文件中的某一个文件剪切到另外一个目录中. 代码: package ccut.cn; import ja ...

  10. php读取外部txt文件内容并打印在页面|fopen()函数

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...