这道题没有找到一条回路,所以不能跟1152一样用数组储存后输出。我采用的方法是DFS加剪枝,直接DFS搜索会超时,优化的方法是在搜索是优先走出度小的路径,比如move1和move2都可以走,但是如走了move1后下一步有7种方向可以走,而走了move2后有2种方向可以走,那我们就优先走move2,具体实现参考代码:

 #include<bits/stdc++.h>
using namespace std;
int _move[][] ={{, -}, {, -}, {, }, {, },{-, }, {-, }, {-, -}, {-, -}}; struct Node{
int x, y;
vector<int>path;
};
Node node;
struct Move{
int x, y;
int degree;
}; void getDegree(Move &move){
int ans = ;
int cur_x = node.x+ move.x;//下一步的位置
int cur_y = node.y + move.y; for(int i = ; i < ; i++){//以cur_x, cur_y为起点,检查其出度 int x = cur_x + _move[i][];
int y = cur_y + _move[i][];
if( <= x && x < && <= y && y < ){//如果坐标没有越界并且没有重复走过,则出度加一
int flag = ;
for(int j = ; j < node.path.size(); j++){
if(node.path[j] == x* + y){
flag = ;
break;
}
} if(flag)ans++;
} }
move.degree = ans;
} bool cmp(Move m1, Move m2){
return m1.degree < m2.degree;
}
bool dfs(int x, int y){ node.x = x;
node.y = y;
Move move[];
for(int i = ; i < ; i++){
move[i].x = _move[i][];
move[i].y = _move[i][];
if( <= node.x + _move[i][] && node.x + _move[i][] < &&
<= node.y + _move[i][] && node.y + _move[i][] < )
getDegree(move[i]); //如果下一步没有越界,则获取它的出度
else move[i].degree = ; //若下一步越界,出度设为100
}
sort(move, move+, cmp);//对出度从小到大排序
for(int i = ; i < ; i++){
if(move[i].degree == ) continue;//若出度为100,跳过
int x = node.x + move[i].x;//下一步位置
int y = node.y + move[i].y;
if( <= x && x < && <= y && y < ){
int flag = ;
for(int j = ; j < node.path.size(); j++){
if(node.path[j] == x* + y){
flag = ;
break;
}
}
if(flag){
node.path.push_back(x* + y);//走下一步 if(node.path.size() >= ){//当path的size等于64,则说明已经走遍
for(int i = ; i < node.path.size(); i++){
cout << node.path[i] + << ' ';
}
cout << endl;
return true;
}
if(dfs(x, y))return true;
node.path.pop_back();//还原到原来
node.x -= move[i].x;
node.y -= move[i].y;
}
} }
return false;
} int main(){
int n;
while(cin >> n && n != -){
node.path.clear();
n = n-;
int a = n / ;
int b = n % ;
node.path.push_back(a* + b);
dfs(a, b);
}
}

Sicily 1153: 马的周游问题(DFS+剪枝)的更多相关文章

  1. sicily 1153. 马的周游问题

    一.题目描述 在一个8 * 8的棋盘中的某个位置有一只马,如果它走29步正好经过除起点外的其他位置各一次,这样一种走法则称马的周游路线,试设计一个算法,从给定的起点出发,找出它的一条周游路线. 为了便 ...

  2. *HDU1455 DFS剪枝

    Sticks Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  3. POJ 3009 DFS+剪枝

    POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...

  4. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  5. DFS(剪枝) POJ 1011 Sticks

    题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...

  6. DFS+剪枝 HDOJ 5323 Solve this interesting problem

    题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...

  7. HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)

    Counting Cliques Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  8. HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))

    Equation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  9. LA 6476 Outpost Navigation (DFS+剪枝)

    题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...

随机推荐

  1. U3D 打包时找不到tag的问题

    在公司改完一个功能,把工程拷回家打开后,编辑器模式下运行正常,打包PC平台和安卓平台时都报错,找不到chatContent这个tag,察看了下下拉列表中明明有这个tag,并且勾选上了,但是点击add ...

  2. 原生js实现fadein 和 fadeout

    js里面设置DOM节点透明度的函数属性:filter= "alpha(opacity=" + value+ ")"(兼容ie)和opacity=value/10 ...

  3. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  4. <form:select>的使用

    最近在学习springMVC,用到了<form:select>标签,使用发过程中遇到了些问题,现在记录下,以防忘记. 我jsp页面是这样的: <%@ page language=&q ...

  5. Leetcode 60. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  6. phpunit 测试框架安装

    PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...

  7. 通过WebStorm上传代码至github

    首先需要注册github帐号,具体方法自行百度/谷歌. 打开WebStorm,我用的是2016.2.4版本(如果你有edu邮箱的话,可以免费使用一年,不只是Webstorm,JetBrains全家桶都 ...

  8. lightbox使用

    使用方法: 1.在页面头部包含 lightbox.js 文件并加载 lightbox.css 样式表文件 <script type="text/javascript" src ...

  9. ReactNative 分享解决listView的一个郁闷BUG

    用ListView的时候,会出现一个非常傻bi的情况,就是render的时候,listView不显示,需要碰/滑一下才会显示. 一开始我在怀疑自己是不是布局哪里有冲突,改到哭都没发现布局有什么问题,直 ...

  10. 安装jhipster

    基础软件安装 安装JDK,需要配置环境变量.暂时使用1.8版本 安装maven,需要配置环境变量.  http://maven.apache.org/ 安装Node.js ,https://nodej ...