这道题嘛,直接使用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. [LeetCode] Paint House II 粉刷房子之二

    There are a row of n houses, each house can be painted with one of the k colors. The cost of paintin ...

  2. [LeetCode] One Edit Distance 一个编辑距离

    Given two strings S and T, determine if they are both one edit distance apart. 这道题是之前那道Edit Distance ...

  3. [LeetCode] Reverse Words in a String 翻转字符串中的单词

    Given an input string, reverse the string word by word. For example, Given s = "the sky is blue ...

  4. charing animation

    FHD : full high definition,1920 x 1080,全高清 vendor/mediatek/proprietary/bootable/bootloader/lk/dev/lo ...

  5. shell及脚本3——正则表达式

    一.正则表达式 1.1. 什么是正则表达式 正则表达式是处理字符串的方法,以行为单位,通过一些特殊符号的辅助,让用户可以轻易进行查找.删除.替换某特定字符串的操作. 1.2. 正则表达式与通配符的区别 ...

  6. neo4j关闭和开启密码访问权限

    本例:neo4j-enterprise-2.3.1版本 neo4j默认安装是开启访问密码验证 可以发现,在conf/下的neo4j-server.properties配置文件 # Require (o ...

  7. 页面局部跳转(iframe)

    <script type="text/javascript"> function show() { document.getElementById("ff&q ...

  8. 四.Android adb命令(持续更新...)

    1.安装:甭管从哪里下载下来的apk,放在指定的目录下,不一定非要是sdk的目录下:adb install "d:\hxcjaz.apk"(指定的一个目录)2.卸载:adb uni ...

  9. Redis之个人简单理解

    1.什么是redis? 在过去的几年中,NoSQL数据库一度成为高并发.海量数据存储解决方案的代名词,与之相应的产品也呈现出雨后春笋般的生机.然而在众多产品中能够脱颖而出的却屈指可数,如Redis.M ...

  10. NoSQL和MongoDB

     NoSQL(NoSQL=Not Only SQL),意即“不仅仅是SQL”.关系数据库关注在关系上,NoSQL关注在存储上. 发展背景 (1)传统关系型数据库遇到了性能瓶颈. 高并发读写(High ...