通过本文,希望可以达到以下目标,当遇到任意问题时,可以:
1、很快建立状态空间;
2、提出一个合理算法;
3、简单估计时空性能;
按照预定的控制策略进行搜索,在搜索过程中获得的中间信息不用来改进控制策略;
常见算法:
1、广度优先搜索(Breadth First Search);
2、深度优先搜索(Depth First Search);
3、纯随机搜索、重复式搜索、迭代加深搜索、迭代加宽搜索、柱形搜索;
在搜索中加入了与问题有关的启发性信息,用以指导搜索朝着最有希望的方向发展,加速问题的求解过程并达到最优解。
常见算法:
1、A*算法;
2、IDA*算法;
问题在某一时刻进展状况的数学描述;
问题从一种状态转移到另一种或几种状态的操作;
一个“图”,图结点对应于状态,点之间的边对应于状态转移;
寻找一种可行的操作序列,从起始状态经过一系列状态转移,达到目标状态;
某人带一条狗、一只鸡、一筐米过河,但小船除了需要人划动外,最多可以带一个物品,而人不在场时,狗要吃鸡,鸡要吃米。问此人应该如何过河?
状态:建立四元组(人,狗,鸡,米)。0表示在左岸,1表示在右岸。
起始状态:(0,0,0,0)
终止状态:(1,1,1,1)
状态转移规则(操作,用1-x来表示过一次河,无论是到左岸还是到右岸都可以):
(a,b,c,d)
→(1-a,1-b,c,d)(当a=b)【人带狗过河】
→(1-a,b,1-c,d)(当a=c)【人带鸡过河】
→(1-a,b,c,1-d)(当a=d)【人带米过河】
→(1-a,b,c,d) 【人自己过河】
约束条件:
(a,b,c,d)中,a≠b时b≠c,a≠c时c≠d
首先从初始状态开始:
然后再对两个可能状态进行进一步搜索:
搜索在“图”中进行,但是图不需要事先建立(“隐式图”);
搜索过程就是对图的遍历过程,可以得到一课“搜索树”;
搜索树的结点个数、分支数、深度,决定着搜索的效率;
存储和算法
普通状态可以用4个整数表示;
压缩状态可以用4个bit表示;
用BFS或DFS;
BFS搜索过程没有回溯,是一种牺牲空间换取时间的方法。时间复杂度:O(V+E)
1 定义一个队列; 2 起始点加入队列; 3 while(队列不空){ 4 取出队头结点; 5 若它是所求的目标状态,跳出循环; 6 否则,从它扩展出子节点,全都添加到队尾; 7 } 8 若循环中找到目标,输出结果; 9 否则输出无解;Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
First line contains two integers stand for N and M.Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend. Process to the end of the file.
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
1、82行代码之后应该判断是否到了目标点,达到目标则return,此时就是花销最少的路径,而不需要全部遍历结束;
2、96行的代码中“<”判断是多余的,因为如果之前已经访问过了,那么现在的一定比之前的路径长;
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part. Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
The input file will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input:
1 e2 e4 2 a1 b2 3 b2 c3 4 a1 h8 5 a1 h7 6 h8 a1 7 b1 c3 8 f6 f6Sample Output:
1 To get from e2 to e4 takes 2 knight moves. 2 To get from a1 to b2 takes 4 knight moves. 3 To get from b2 to c3 takes 2 knight moves. 4 To get from a1 to h8 takes 6 knight moves. 5 To get from a1 to h7 takes 5 knight moves. 6 To get from h8 to a1 takes 6 knight moves. 7 To get from b1 to c3 takes 1 knight moves. 8 To get from f6 to f6 takes 0 knight moves.Code:
1 #include<iostream> 2 #include<cstdio> 3 #include<queue> 4 using namespace std; 5 6 //定义node结构体 7 typedef struct{ 8 int x,y,len; 9 }node; 10 //八个方向 11 int step[8][2] = { 12 -2,-1, //left2-up1 13 -1,-2, //left1-up2 14 1,-2, //right1-up2 15 2,-1, //right2-up1 16 2,1, //right2-down1 17 1,2, //right2-down2 18 -1,2, //left1-down2 19 -2,1 //left2-down1 20 }; 21 #define M 10 22 int Map[M][M];//chessboard 23 int bx,by,ex,ey;//begin point,end point 24 char a[3],b[3]; 25 26 void bfs(int x,int y); 27 28 void main(){ 29 while(scanf("%s%s",a,b)!=EOF){ 30 bx = a[0]-'a'+1; //the range of index is from 1 to 8 31 by = a[1]-'0'; //the range of index is from 1 to 8 32 ex = b[0]-'a'+1; //the range of index is from 1 to 8 33 ey = b[1]-'0'; //the range of index is from 1 to 8 34 bfs(bx,by); 35 } 36 return 0; 37 } 38 39 void bfs(int x,int y){ 40 node t,p; 41 queue<node> q; 42 //BFS Step1:push first node t into queue q 43 t.x = x; 44 t.y = y; 45 t.len = 0; 46 q.push(t); 47 //while(!q.empty()) 48 while(q.size()){ 49 //BFS Step2:get the head of queue 50 t = q.front(); 51 q.pop(); 52 //BFS Step3:arrive the end point,return. first time arriving is the minimum. 53 if(t.x == ex && t.y == ey){ 54 printf("To get from %s to %s takes %d knight moves.\n",a,b,t.num); 55 return; 56 } 57 //BFS Step4:push the next steps into queue q 58 for(int i=0;i<8;i++){ 59 node next; 60 next.x = t.x + step[i][0]; 61 next.y = t.y + step[i][1]; 62 //no out of range and not been visited 63 if(!(next.x>0 && next.x<9 && next.y>0 && next.y<9) && !Map[next.x][next.y]){ 64 next.len = t.len+1; 65 q.push(next); 66 } 67 } 68 } 69 }
转载于:https://www.cnblogs.com/CheeseZH/p/4550602.html
相关资源:python基础编程:Python数据结构与算法之图的广度优先与深度优先搜索算法示例