随笔分类 -  搜索

POJ 1077 八数码难题
摘要:终于自己AC了这道八数码难题,用的方法是最最普通的单项BFS+Hash,这里主要的难点在于如何保存状态,其实,在八数码里所有的状态共有9!种=362880种,所以在每个转台节点,我需要一个char state[3][3]来记录当前棋盘的状态,用一个int x,y来记录当前x的位置,用char operation来记录从父状态到这个状态需要进行的操作,当然,为了记录路径,我需要记录它的父节点,然而这个父节点要怎么去记录呢,刚开始的时候对于这个问题纠结了很久,曾经直接把结构体存在队列里面,然后用指针去引用它的父节点,殊不知,当出队操作后,声明的节点就是去了意义,所以这种做法是错误的。看了别人的思路 阅读全文

posted @ 2012-01-06 22:33 lonelycatcher 阅读(513) 评论(0) 推荐(0)

HDU 2351 catch him 广度优先搜索
摘要:不得不说这道题木让人很蛋疼,本来是一道很简单的广搜题目,但是由于人的位置处于一块连续的区域,所以难度就出来了,关键是防守人的位置怎么存储,这里我是把它存储在一个node类型的vector数组里面,为什么用vector呢,因为之前总是莫名其妙的MLE,不过到后来证明用数组也是不超内存的,应该是我的程序进入了死循环。View Code 1 #include<iostream> 2 #include<string.h> 3 #include<string> 4 #include<stdio.h> 5 #include<queue> 6 #i 阅读全文

posted @ 2011-09-23 21:28 lonelycatcher 阅读(310) 评论(0) 推荐(0)

poj 1020 DFS +回溯
摘要:Source CodeProblem:1020User:sunyanfeiMemory:700KTime:32MSLanguage:G++Result:AcceptedSource Code/* * Problem:poj 1020 zoj 1411 * Type:DFS+回溯 */#include <iostream>#include<stdio.h>#include<string.h>using namespace std;int cake_size,cake_number,cuted_cakes;//cute_cakes是已经放好的蛋糕数int pie 阅读全文

posted @ 2011-08-15 20:42 lonelycatcher 阅读(307) 评论(0) 推荐(0)

poj 1564
摘要:Source Code/* * Author:lonelycatcher * problem:ZOJ 1711 * Type: DFS */#include<iostream>#include<stdio.h>#include<queue>#include<string>#include<string.h>using namespace std;int t,n;struct node{ int data; int preindex;};node lis[15];int OK,count;void printpath(int index 阅读全文

posted @ 2011-08-02 23:00 lonelycatcher 阅读(351) 评论(0) 推荐(0)

HDU 1026 广度优先搜索,BFS+路径的记录
摘要:哎~~花了半天的时间写了一个DFS的程序,结果无情的limit time exceed,然后到网上搜了一下,居然用的是BFS一般来说,广搜常用于找单一的最短路线,或者是规模小的路径搜索,它的特点是"搜到就是最优解", 而深搜用于找多个解或者是"步数已知(比如3步就必须达到条件)"的问题,它的空间效率高,但是找到的不一定是最优解,必须记录并完成整个搜索,故一般情况下,深搜需要非常高效的剪枝(优化).像搜索最短路径这些的很明显要是用广搜,因为广搜的特性就是一层一层往下搜的,保证当前搜到的都是最优解,当然,最短路径只是一方面的应用,像什么最少状态转换也是可以应 阅读全文

posted @ 2011-07-29 10:09 lonelycatcher 阅读(4816) 评论(0) 推荐(0)

HDU 1016 Prime Ring Problem 搜索题
摘要:#include<stdio.h>#include<string.h>#include <iostream>using namespace std;int N;int prime[12]={2,3,5,7,11,13,17,19,23,29,31,37};int ring[25];int visited[25];int search(int low ,int high,int x){ if(low>high)return 0; int mid=(low+high)>>1; if(x==prime[mid])return mid; else 阅读全文

posted @ 2011-07-25 09:59 lonelycatcher 阅读(264) 评论(0) 推荐(0)

HDU 1015 Safecracker
摘要:与其说这是一道搜索题,倒不如说是一道枚举题更加合适,反正就是纯暴力。。。。注意读懂题意#include<iostream>#include<stdio.h>#include<string.h>#include<math.h>#include<string>#include<algorithm>using namespace std;char s[12];int cmp(const void*a ,const void*b){ return *(int*)b-*(int*)a;}__int64 target;int main 阅读全文

posted @ 2011-07-21 10:59 lonelycatcher 阅读(218) 评论(0) 推荐(0)

hdu 1010 Tempter of the Bone
摘要:这是一道关于迷宫的搜索题,注意看懂题意:必须在time=T的时候门才开,这个时候必须准时到达door才能survive,既不能早了,也不能晚了,有一个细节用到了奇偶剪枝原理#include<iostream>#include<cstdio>#include<cmath>#include<string.h>using namespace std;int N,M,T,OK;int startx,starty,endx,endy;char maze[8][8];int mark[8][8];int X[4]={1,-1,0,0};int Y[4]={0 阅读全文

posted @ 2011-07-19 13:31 lonelycatcher 阅读(192) 评论(0) 推荐(0)

导航