这道题嘛,直接使用DFS搜索,然后莫名其妙地AC了。后来看了题解,说是move的顺序不同的话可能会导致超时,这时便需要剪枝,真是有趣。原来自己是误打误撞AC了,hhh。题解还有另一种解法是先把一条完整的路储存在数组里,输入i的时候,就从i位置起把数组循环输出一遍,真是666的解法呀,果然不能被传统的思路所局限了呀!

 #include<bits/stdc++.h>
using namespace std;
int _move[][] ={{, -}, {, -}, {, }, {, },{-, }, {-, }, {-, -}, {-, -}}; struct Node{
int x, y;
vector<int>path;
};
Node node; bool dfs(int x, int y){ node.x = x;
node.y = y;
for(int i = ; i < ; i++){
int x = node.x + _move[i][];
int y = node.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){
node.path.push_back(x* + y);//走下一步 if(node.path.size() >= ){//当path的size等于30,则说明已经走遍
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][];
node.y -= _move[i][];
}
} }
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);
}
} /*
#include<iostream>
using namespace std;
int a[]={1,14,25,21,29,18,5,16,12,4,8,19,27,23,10,6,17,30,22,26,13,2,15,7,3,11,24,28,20,9};
int main()
{
int n;
while(cin>>n)
{
int tmp;
for(int i=0;i<30;i++)
if(a[i]==n)tmp=i;
for(int i=0;i<30;i++)
{
cout<<a[(tmp+i)%30];
if(i==29)cout<<endl;
else cout<<" ";
}
}
}
*/

Sicily 1151: 简单的马周游问题(DFS)的更多相关文章

  1. 图论 --- 骑士周游问题,DFS

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

  2. 基于Java实现简单亚马逊爬虫

    前言:最近博主买了台Kindle,感觉亚马逊上的图书资源质量挺好,还时不时地会有价格低但质量高的书出售,但限于亚马逊并没有很好的优惠提醒功能,自己天天盯着又很累.于是,我自己写了一个基于Java的亚马 ...

  3. 马踏棋盘--dfs

    [问题描述]关于马踏棋盘的基本过程:国际象棋的棋盘为 8*8 的方格棋盘.现将"马"放在任意指定的方格中,按照"马"走棋的规则将"马"进行移 ...

  4. [sdut] 1400 马的走法 dfs

    Problem Description 在一个4*5的棋盘上,马的初始位置坐标(纵 横)位置由键盘输入,求马能返回初始位置的所有不同走法的总数(马走过的位置不能重复,马走“日”字).如果马的初始位置坐 ...

  5. sicily 无路可逃?(图的DFS)

    题意:在矩阵数组中搜索两点是否可达 解法:DFS #include<iostream> #include<memory.h> using namespace std; stru ...

  6. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

  7. Sicily 1151 魔板

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不 ...

  8. sicily 4699. 简单哈希

    Description 使用线性探测法(Linear Probing)可以解决哈希中的冲突问题,其基本思想是:设哈希函数为h(key) = d, 并且假定哈希的存储结构是循环数组, 则当冲突发生时,  ...

  9. sicily 1004. 简单哈希

    Description 使用线性探测法(Linear Probing)可以解决哈希中的冲突问题,其基本思想是:设哈希函数为h(key) = d, 并且假定哈希的存储结构是循环数组, 则当冲突发生时,  ...

随机推荐

  1. json文件

    json为什么会火 参考链接 http://www.jb51.net/article/32830.htm

  2. [LeetCode] Valid Perfect Square 检验完全平方数

    Given a positive integer num, write a function which returns True if num is a perfect square else Fa ...

  3. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  4. [LeetCode] Partition List 划分链表

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  5. [LeetCode] Remove Nth Node From End of List 移除链表倒数第N个节点

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  6. Java学习笔记之JNDI(六)

    JNDI 是什么 JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的 ...

  7. 添加webservice调用日志

    之前想用spring的AOP给webservice添加切面的,但是使用around切面后,居然调用端得不到webservice的返回结果,而且报文的详细情况也不得而知,很是尴尬,所以偷了个懒.但是该做 ...

  8. 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 ...

  9. PowerShell自动部署IIS站点(Windows Server 2008 R2)

    1.功能描述 1. 连接软件源服务器下载.NET Framework 4.0..NET Framework 4.5. 2. 检测并判断当前.NET Framework版本是否小于v4.0,如果小于则进 ...

  10. 解决Linux下启动Tomcat遇到Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

    找到启动路径所在的目录: cd bin/ vi catalina.sh 加入以下信息: export JAVA_HOME=/home/gongzi/http/jdk1.6.0_26 export JR ...