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 ...
随机推荐
- h5+mui
参考链接http://blog.csdn.net/tbwood/article/details/42081861 待补充
- [LeetCode] Maximal Square 最大正方形
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- [Python基础知识]正则
import re str4 = r"^http://qy.chinahr.com/cvm/preview\?cvid=\w{24,25}&from=sou>id=\w{ ...
- SQL Server 2012附加数据库时,错误提示如下:尝试打开或创建物理时,CREATE FILE 遇到操作系统错误 5(拒绝访问。)
错误提示:Create File遇到操作系统错误5(拒绝访问) 解决方案: 在所有程序-SQL Server 2012-"SQL Server 配置管理器",点击"SQL ...
- table表框去掉相邻的间隔
HTML <table> <tr> <th>名称</th> <th>描述</th> </tr> <tr> ...
- 【转】JAVA自学之路
JAVA自学之路 一: 学会选择 为了就业,不少同学参加各种各样的培训. 决心做软件的,大多数人选的是java,或是.net,也有一些选择了手机.嵌入式.游戏.3G.测试等. 那么究竟应该选择什么方向 ...
- SignalR实现网页实时聊天功能
SignalR是利用html5 sokit方式实现网页的实时性,在客户端不支持html5的情况下通过轮询实现 实现原理是客户端发送的消息先去服务器,然后服务器根据需要将消息广播到需要接收信息的客户群. ...
- 1 python大数据挖掘系列之基础知识入门
preface Python在大数据行业非常火爆近两年,as a pythonic,所以也得涉足下大数据分析,下面就聊聊它们. Python数据分析与挖掘技术概述 所谓数据分析,即对已知的数据进行分析 ...
- SolrQuery的使用
查询的总结:一般查询分为以下几个步骤: 1.创建查询对象 2.创建查询条件 3.执行查询,这里注意要有返回值 4.对查询结果进行取出 public void queryDocument() throw ...
- web网页中使用vlc插件播放相机rtsp流视频
可参考: 使用vlc播放器做rtsp服务器 使用vlc播放器播放rtsp视频 使用vlc进行二次开发做自己的播放器 vlc功能还是很强大的,有很多的现成的二次开发接口,不需配置太多即可轻松做客户端播放 ...