随笔分类 - 搜索
摘要:http://poj.org/problem?id=1077#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<queue>using namespace std;/* 把1..n的排列映射为数字 0..(n!-1) */int fac[] = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880 };//...int order(const char *s, int n) {//康托展开 i
阅读全文
摘要:http://poj.org/problem?id=2286今天又做了个迭代加深,不错继续努力!! 移动八根线中任一根,移法为头的数移到线的尾,其它的数向头进一位。求最少要移多少次才能使中间的八个数相等。本题若用广搜,空间需求量非常大,空间不足。深搜的话,深度很难控制,容易陷入死循环。在这个时候就要用到迭代加深的深搜方法。所谓迭代加深,就是在深度无上限的情况下,先预估一个深度(尽量小)进行搜索,如果没有找到解,再逐步放大深度搜索。这种方法虽然会导致重复的遍历 某些结点,但是由于搜索的复杂度是呈指数级别增加的,所以对于下一层搜索,前面的工作可以忽略不计,因而不会导致时间上的亏空。这种方法,可以算
阅读全文
摘要:http://poj.org/problem?id=2312 相信坦克大战大家都玩过吧,本题就是根据这个游戏设计的。坦克要从起点(Y),到目的地(T),坦克不能通过钢墙(S),河(R),可以在空地在行走(E),射击破坏砖墙(B),射击砖墙时不行走且花费一个单位的时间。求坦克从起点到目的地最少花多少时间,不可达输出-1; 很好的一道搜索题。因为考虑到通过砖墙时和空地所花的时间不同,所以不能简单的用BFS广搜来做。用DFS深搜,你会发现时间复杂非常高,必然会超时(最大是300*300的图)。本题可以能过优先队列+bfs 或 记忆化广搜两种方法来解决。一、优先队列+BFS法: 也是用到了广搜的思想,
阅读全文
摘要:http://poj.org/problem?id=3083入口S,出口E,分别求由入口 到出口靠左走,靠右走,和最短路三种走法各自的步数。入口和出口在边界处,并且不会在四个角上,入口和出口至少隔着一个阻碍。本来是想找一个广搜和深搜结合的题目,百度一下找到的这题,后来发现这题所谓的广搜加深搜只是用深搜找一条路径,而用广搜找另外一条路径,。根本不是我希望的广搜和深搜的结合。 不过既然题目已经看了,还是做一做吧。求最短路的没什么好说的,用一下广搜就可以解决问题了。主要是靠左走和靠右走的部分,这里用到了两个数组,dirx[],diry[],共同来确定方向。dirx[],diry[],数组的下标增1.
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1983转自:http://hi.baidu.com/song19870626/blog/item/b44a8110d9341376ca80c48d.html思路:封锁出口或者入口周围的格子. 最多需要4个封锁点. 所以我们可以采取这样的策略: 1.寻找一条盗贼的可行路线,如果没有,返回0. 2.计算封锁出口和入口四周需要的封锁点数量,取小的一个,假设是k,k <=4 3.从少到多,遍历所有封锁点个数小于k的方案,验证是否是一条有效的覆盖方案(可以通过是否阻止了1中的盗贼线路进行快速验证). 如果有有效覆
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1800soldiers学习使用魔法扫帚,水平高的可以教水平在他之下的,每个老师有而只有一个学生,并且可以共用一个魔法扫帚练习,要求每个soldier都要有练习魔法扫帚的机会,求最少需要多少个魔法扫帚。如士兵A B C D E水平分别为2 4 5 6 4方法可以是:C教B,B教A,D教E,这样ABC共用一把扫帚,DE共用一把扫帚;只用两把扫帚即可。而结果也只需要两把就可以了。先对数列排序,然后筛选出相等元素,这样循环筛选,每筛选一次代表扫帚增一,直到无可筛选的元素。C could teach B; B coul
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2102本题又是一道BSF搜索求最短路,只是在遇到‘#’时空传输时要注意一下,如果‘#’所对应的另一个迷宫位置也是‘#’时是没有意思的。#include <iostream>#include <stdio.h>#include<stdlib.h>#include<string.h>#include <queue>#include <algorithm>using namespace std;char map[25][15];int T,co
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2141三个整数数列A,B,C。给若干个数X,若能找到Ai+Bj+Ck = X(Ai是数列A中的某一个数,Bj,Ck同理),输出YES,否则输出NO。先是做A和B数列各元素的和并排序,接下来只要二查找X-C,就可以了。#include <iostream>#include <stdio.h>#include<stdlib.h>#include<string.h>#include <queue>#include <algorithm>usin
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2614#include<stdio.h>#include<stdlib.h>int p[105][105],n,max,flag[105];void search(int a,int b,int c){ int i,f=1; for(i=0;i<n;i++) { if(p[a][i]<b||flag[i]) continue; flag[i]=1;f=0; search(i,p[a][i],c+1); flag[i]=0; } if(f) { if(c>m...
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1401转自:http://www.cppblog.com/IronOxide/archive/2010/08/19/123928.html在一个 8 * 8 的棋盘上,有四个棋子,每颗棋子可在上,下,左,右,四个方向进行四种操作,四种操作是一下的某一种: 1. 若相邻格有棋子,则可像跳棋一样,跳过去,到达对称的一格。 2.若相邻格为空,则可直接移过去。问能否从一种状态在8步之内变成另一种状态?题目分析: 明显的一道搜索题,但是应该选取怎样的策略去搜索呢? 估计一下普通广度优先搜索的复杂度:有4颗棋子,每个棋子
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=2553//1.非递归:#include <cstdio>#include <cmath>using namespace std;#define N 13int x[N];bool isvalid(int k){ int i; for(i=0; i<k; i++) if(x[i]==x[k] || abs(x[i]-x[k])==abs(i-k)) return false; return true;}int queen(int n){ int k=0,count=0; x[k]=
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1016输入n。从1到n的数组成一个环,环相邻的两个数之和为质数。输出所有的这种环。本人用深搜递归水过250ms#include <stdio.h>#include<stdlib.h>#include<string.h>#include <iostream>using namespace std;int n;int ans[25],p[50],flag[25];void dfs(int step){ int i; if(step==n) { if(p[ans[0]
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1015题目输入多组数据第组包括:一个整数n和一个字符串,要求从字符串中,选五个字串且满足v - w^2 + x^3 - y^4 + z^5 = n;c++#include <stdio.h>#include<string.h>#include<math.h>#include <iostream>#include <deque>using namespace std;double n,q[28],ans;char p[15];int num,len;b
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1239题意:a.给定整数m,a,b(4 < m <= 100000 and 1 <= a <= b <= 1000)b.需要找到两个数(不妨设为p,q)满足以下条件: l p,q均为质数; l p*q<=m; l a/b <= p/q <= 1; c.输出所有满足以上条件的p,q中乘积最大的一对p,q 分析:考虑大于10000的某个质数,不妨设为Q,另一个质数为P,则:l如果P<10,P/Q<0.001 l如果P>10,P*Q>10000
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1372给定棋盘上的两个点,求马从起点走到终点所需的最小步数,马的走法如下(和象棋差不多),本题也是简单的BFS搜索。#include <stdio.h>#include <iostream>#include <queue>using namespace std;int xf,yf,xt,yt,ans;int dir[8][2]={{-2,1},{2,1},{-2,-1},{2,-1},{-1,2},{1,2},{-1,-2},{1,-2}};bool visited[9][
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1312二维图:.:黑砖#:红砖@:起点求由起点最多能找到多少个黑砖,当然红砖是不能走的(上下左右四个方向);基础的搜索题,深搜或广搜都可。深搜:#include <stdio.h>#include <iostream>using namespace std;char map[21][21];int co,ro,ans;int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};bool judge(int x,int y){ if(x<0||y<0|
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1072二维图:0:墙1:路2:起点3:终点4:可重设炸弹定时器的地方,时间又重设为6。要在6个单位时间内从起点走到终点,不过当走到“ 4”时时间又可设为6;输出可行的最短步数。#include<stdio.h>#include<stdlib.h>#include<queue>#include<string.h>#include <iostream>using namespace std;int map[10][10],tim[10][10];int
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1242一个二维表:.:路#:障碍a:要救的人r:去找的人(有多个)x:门卫找人中若遇到门卫要多花一个单位的时间本题可以反过来做,用a去找最近的r; 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<queue> 4 #include<string.h> 5 #include <iostream> 6 using namespace std; 7 char map[201][201]; 8 int
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1241给一个二维图*:缺乏油的地方;@:有油的地方;当@与@之间相邻(水平,垂直,对角线八个方向都可),算同一个油田。求最多有多少个油田;#include <stdio.h>#include <string.h>#include<stdlib.h>#include <cmath>#include <iostream>using namespace std;#define MAXSIZE 103int dir[8][3]={{1,0},{-1,0},{
阅读全文
摘要:http://acm.hdu.edu.cn/showproblem.php?pid=1240N表示三维图的大小(x,y,z为零到N-1),然后是一个三维图,最后是起点和终点的座标。答案输出N和从起点到终点走的步数。和hdu1253差不多的题目,并且本题不用剪技,直接BFS搜一遍就可以了。#include<stdio.h>#include<stdlib.h>#include<queue>#include<string.h>#include <iostream>using namespace std;char map[11][11][11
阅读全文

浙公网安备 33010602011771号