Sicily 1153: 马的周游问题(DFS+剪枝)
这道题没有找到一条回路,所以不能跟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+剪枝)的更多相关文章
- sicily 1153. 马的周游问题
一.题目描述 在一个8 * 8的棋盘中的某个位置有一只马,如果它走29步正好经过除起点外的其他位置各一次,这样一种走法则称马的周游路线,试设计一个算法,从给定的起点出发,找出它的一条周游路线. 为了便 ...
- *HDU1455 DFS剪枝
Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- POJ 3009 DFS+剪枝
POJ3009 DFS+剪枝 原题: Curling 2.0 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16280 Acce ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- DFS(剪枝) POJ 1011 Sticks
题目传送门 /* 题意:若干小木棍,是由多条相同长度的长木棍分割而成,问最小的原来长木棍的长度: DFS剪枝:剪枝搜索的好题!TLE好几次,终于剪枝完全! 剪枝主要在4和5:4 相同长度的木棍不再搜索 ...
- DFS+剪枝 HDOJ 5323 Solve this interesting problem
题目传送门 /* 题意:告诉一个区间[L,R],问根节点的n是多少 DFS+剪枝:父亲节点有四种情况:[l, r + len],[l, r + len - 1],[l - len, r],[l - l ...
- HDU 5952 Counting Cliques 【DFS+剪枝】 (2016ACM/ICPC亚洲区沈阳站)
Counting Cliques Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5937 Equation 【DFS+剪枝】 (2016年中国大学生程序设计竞赛(杭州))
Equation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- LA 6476 Outpost Navigation (DFS+剪枝)
题目链接 Solution DFS+剪枝 对于一个走过点k,如果有必要再走一次,那么一定是走过k后在k点的最大弹药数增加了.否则一定没有必要再走. 记录经过每个点的最大弹药数,对dfs进行剪枝. #i ...
随机推荐
- [LeetCode] Integer to English Words 整数转为英文单词
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 理解ASP.NET MVC的DependencyResolver组件
一.前言 DependencyResolver是MVC中一个重要的组件,从名字可以看出,它负责依赖对象的解析,可以说它是MVC框架内部使用的一个IOC容器.MVC内部很多对象的创建都是通过它完成的,或 ...
- 支持同步滚动的RichTextbox控件
using System.Windows.Forms; public class SynchronizedScrollRichTextBox : System.Windows.Forms.RichTe ...
- ajax处理缓冲问题
1.禁止页面缓存 header("Cache-Control:no-cache"); header("Pragma:no-cache"); header(&qu ...
- 关于label的点击事件(原创)
通常做网页时不会用radio和checkbox的原有样式,通常会进行样式美化,手机端我用的jqurey weui框架,他的原理是这样的: <label class="check_lab ...
- iOS常用开发技巧
iOS开发过程中,总有那么一些个小问题让人纠结,它们不会让程序崩溃,但是会让人崩溃.除此之外,还将分享一些细节现在我通过自己的总结以及从其他地方的引用,来总结一下一些常见小问题. 本篇长期更新,多积累 ...
- Android源码——Activity进程内启动
进程内启动Activity MainActivity组件向ActivityManagerService发送一个启动SubActivityInProcess组件的进程间通信请求: ActivityMan ...
- linux安装maven
一.下载maven 最新地址在:http://maven.apache.org/download.cgi 我下载的是:apache-maven-3.3.9-bin.tar.gz,是已经编译好的包 解压 ...
- Nginx 1.10.1 编译、配置文档(支持http_v2,TLSv1.2,openssl v1.0.2)
1.安装常用工具及基础包: [root@localhost /]# yum -y install wget git vim make gcc gcc-c++ openssl-devel [root@l ...