迭代加深搜索(IDDFS)的原理与练习

目录

一、什么是迭代加深搜索?

二、优势和劣势

三、练习:POJ 2286 - The Rotation Game 

 AC代码以及注释


一、什么是迭代加深搜索?

       首先,它是深度优先搜索,其次它与普通深度优先搜索不同的是,每次深搜都会有搜索的最大深度限制,如果没有找到解,那么就增大深度,再进行深搜,如此循环直到找到解为止,这样可以找到最浅层的解。

标志性的结构如下:

for(idt=0;; idt++)
      if(dfs(0)) break;

或者 

 while(!dfs(idt))
     idt++;

二、优势和劣势

      了解了原理,大家也许会有疑问,那为啥不不直接用广度优先搜索呢?那是因为IDDFS有如下几个优势:

1.时间复杂度只比BFS稍差一点(虽然搜索k+1层时会重复搜索k层,但是整体而言并不比广搜慢很多)。

2.空间复杂度与深搜相同,却比广搜小很多。

3.利于剪枝。

另外,值得注意的是:

1.IDDfs的前提:一定要有解。 

2.注意DFS是bool型,而不是void型。

三、练习:POJ 2286 - The Rotation Game 

描述The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The blocks are marked with symbols 1, 2 and 3, with exactly 8 pieces of each kind.

Initially, the blocks are placed on the board randomly. Your task is to move the blocks so that the eight blocks placed in the center square have the same symbol marked. There is only one type of valid move, which is to rotate one of the four lines, each consisting of seven blocks. That is, six blocks in the line are moved towards the head by one block and the head block is moved to the end of the line. The eight possible moves are marked with capital letters A to H. Figure 1 illustrates two consecutive moves, move A and move C from some initial configuration.

输入The input consists of no more than 30 test cases. Each test case has only one line that contains 24 numbers, which are the symbols of the blocks in the initial configuration. The rows of blocks are listed from top to bottom. For each row the blocks are listed from left to right. The numbers are separated by spaces. For example, the first test case in the sample input corresponds to the initial configuration in Fig.1. There are no blank lines between cases. There is a line containing a single `0' after the last test case that ends the input.

输出For each test case, you must output two lines. The first line contains all the moves needed to reach the final configuration. Each move is a letter, ranging from `A' to `H', and there should not be any spaces between the letters in the line. If no moves are needed, output `No moves needed' instead. In the second line, you must output the symbol of the blocks in the center square after these moves. If there are several possible solutions, you must output the one that uses the least number of moves. If there is still more than one possible solution, you must output the solution that is smallest in dictionary order for the letters of the moves. There is no need to output blank lines between cases.

样例输入

1 1 1 1 3 2 3 2 3 1 3 2 2 3 1 2 2 2 3 1 2 1 3 3
1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3
0

样例输出

AC
2
DDHH
2

题目大概意思是通过最少的操作,将下面的图形的中间八个数字变成一样的。

题目链接;https://link.zhihu.com/?target=http%3A//poj.org/problem%3Fid%3D2286 

 AC代码以及注释

#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define ll long long
#define inf 0x7fffffff
const int lu[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
int idt;
char op[]="ABCDEFGH";
int pak[10000]= {0},a[100分];
int mid[8]= {6,7,8,11,12,15,16,17}; //中间位置
int fan[8]= {5,4,7,6,1,0,3,2}; //反方向移动,回归
int xh[8][7]= { //8种操作,每次移动7位
	0,2,6,11,15,20,22, //A 
	1,3,8,12,17,21,23,  //B
	10,9,8,7,6,5,4,      //C
	19,18,17,16,15,14,13,//D
	23,21,17,12,8,3,1,    //E
	22,20,15,11,6,2,0,     //F
	13,14,15,16,17,18,19,   //G
	4,5,6,7,8,9,10         //H
}; 
int max3(int a,int b,int c) {
	return a>b?(a>c?a:c):(b>c?b:c);
}
int gwt() {          //中间八个数中的众数数量与8的差值
	int cot[4]= {0};
	for(int i=0; i<8; i++) {
		cot[a[mid[i]]]++;
	}
	return 8-max3(cot[1],cot[2],cot[3]);
}
void move(int k) {               //ABCDEFGH move
	int t=a[xh[k][0]];
	for(int i=0; i<6; i++) {
		a[xh[k][i]]=a[xh[k][i+1]];
	}
	a[xh[k][6]]=t;
}
bool dfs(int k) {
	if(k>idt) return false;  //提前剪枝
	for(int i=0; i<8; i++) {
		move(i);
		pak[k]=i;
		int h=gwt();
		if(h==0)
			return true;
		if(h+k<=idt&&dfs(k+1))   //提前剪枝
			return true;
		move(fan[i]);
	}
	return false;
}
int main() {
	int n;
	while(1) {
		cin>>a[0];
		if(a[0]==0)
			break;
		for(int i=1; i<24; i++)
			cin>>a[i];
		int h=gwt();
		if(h==0) {
			printf("No moves needed\n");
			cout<<a[6]<<endl;
			continue;
		}
		for(idt=0;; idt++)            //迭代加深搜索
			if(dfs(0)) break;
		for(int i=0; i<=idt; i++)
			cout<<op[pak[i]];
		cout<<endl;
		cout<<a[6]<<endl;
	}
	return 0;
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值