POJ 2286 The Rotation Game(IDA*)

The Rotation Game

Time Limit: 15000MS   Memory Limit: 150000K
Total Submissions: 6396   Accepted: 2153

Description

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.

Input

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.

Output

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.

Sample Input

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

Sample Output

AC
2
DDHH
2

题目链接:POJ 2286

第一道IDA*题目,由于用的是递归的写法,代码量实际上不会很大,只要在dfs入口处做好各种判断就可以了 ,能用IDA*的前提至少答案要存在,如果不存在的话搜索深度会无限加深就没有意义了,然后每一次都用估价函数剪枝即可,再加一个防止来回的剪枝速度可以快一倍

大致伪代码如下:

void dfs(state, dep)

{

  计算此时状态下的估价函数值h(state)

  if(h(state)==0)

    已到终点,返回true

  else if(h(state)+dep>Maxdep)

    在规定的Maxdep内一定走不到终点,返回false

  else

  {

    for....

    {  

       得到新状态n_state

       if(dfs(n_state,dep))

         return 1;

    }

  }

  return 0;

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 25;

int arr[N], cnt[4];
int Max_dep, Num;
char ans[1000];
int top,Back[8]={5,4,7,6,1,0,3,2};//Back数组,减掉正拉动后又马上反向拉动的无意义搜索

int pos[8][7] =
{
	{1, 3, 7, 12, 16, 21, 23}, //A0
	{2, 4, 9, 13, 18, 22, 24}, //B1
	{11, 10, 9, 8, 7, 6, 5}, //C2
	{20, 19, 18, 17, 16, 15, 14}, //D3
	{24, 22, 18, 13, 9, 4, 2},//E4
	{23, 21, 16, 12, 7, 3, 1},//F5
	{14, 15, 16, 17, 18, 19, 20},//G6
	{5, 6, 7, 8, 9, 10, 11},//H7
};
int Need(int cur[])
{
	int i;
	cnt[1] = cnt[2] = cnt[3] = 0;
	for (i = 7; i <= 9; ++i)
		++cnt[cur[i]];
	for (i = 12; i <= 13; ++i)
		++cnt[cur[i]];
	for (i = 16; i <= 18; ++i)
		++cnt[cur[i]];
	int Need_1 = 8 - cnt[1], Need_2 = 8 - cnt[2], Need_3 = 8 - cnt[3];
	int Min_Need = min(Need_1, min(Need_2, Need_3));
	return Min_Need;
}
int IDA_star(int dep, int Arr[],int pre)
{
	int Need_cur = Need(Arr);
	if (Need_cur == 0)
	{
		Num = Arr[7];
		return 1;
	}
	if (Need_cur + dep > Max_dep)
		return 0;
	int Temp_arr[N];

	for (int Opsid = 0; Opsid < 8; ++Opsid)
	{
	    if(Opsid==pre)
            continue;
		ans[top++] = ‘A‘ + Opsid;

		for (int i = 1; i <= 24; ++i)
			Temp_arr[i] = Arr[i];
		for (int i = 0; i < 7; ++i)
			Temp_arr[pos[Opsid][i]] = Arr[pos[Opsid][(i + 1) % 7]];

		if (IDA_star(dep + 1, Temp_arr, Back[Opsid]))
			return 1;
		else
			--top;
	}
	return 0;
}
int main(void)
{
	while (~scanf("%d", &arr[1]) && arr[1])
	{
		for (int i = 2; i <= 24; ++i)
			scanf("%d", &arr[i]);
		if (Need(arr) == 0)
		{
			puts("No moves needed");
			printf("%d\n", arr[7]);
		}
		else
		{
			top = 0;
			Max_dep = 1;
			Num = -1;
			while (!IDA_star(0, arr, -1))
				++Max_dep;
			ans[top] = ‘\0‘;
			puts(ans);
			printf("%d\n", Num);
		}
	}
	return 0;
}
时间: 2024-10-06 12:15:06

POJ 2286 The Rotation Game(IDA*)的相关文章

POJ - 2286 - The Rotation Game (IDA*)

IDA*算法,即迭代加深的A*算法,实际上就是迭代加深+DFS+估价函数 题目传送:The Rotation Game AC代码: #include <map> #include <set> #include <list> #include <cmath> #include <deque> #include <queue> #include <stack> #include <bitset> #include

UVA-1343 The Rotation Game (IDA*)

题目大意:数字1,2,3都有八个,求出最少的旋转次数使得图形中间八个数相同.旋转规则:对于每一长行或每一长列,每次旋转就是将数据向头的位置移动一位,头上的数放置到尾部.若次数相同,则找出字典序最小旋转次序. 题目分析:IDA*,若当前在第cur层,中间八个数中1,2,3的个数分别为a,b,c.则d=8-max(a,b,c)便是达到目标还需要的理想次数,若cur+d>maxd,则剪枝.<入门经典>上提供了一种BFS的思路,分别以1,2,3为目标广搜3次,不过自己的码力还是太弱,并没有用那种

POJ 2286 The Rotation Game 迭代搜索深度 + A* == IDA*

感觉这种算法还是比较局限的吧,重复搜索是一个不好的地方,而且需要高效的估值函数来进行强剪枝,这点比较困难. 迭代搜索深度是一个比较炫酷的搜索方式,不过有点拿时间换空间的感觉. 首先迭代深度比较搓的写法是,首先设置一个阀值MaxH,初始为最小值. 当在搜索深度Depth <= MaxH时找到解则此时为最优解,否则MaxH++,继续深搜. 另外一种比较吊的写法是二分搜索深度,若搜到则减小阀值,否则增大阀值. 总之,迭代深度搜索就是通过改变深搜的深度来寻找最优解,这样做的好处是省掉了BFS中状态标记所

POJ 3087 Shuffle&#39;m Up (DFS)

题目链接:Shuffle'm Up 题意:有a和b两个长度为n的字符序列,现定义操作: 将a.b的字符交叉合并到一个序列c,再将c最上面的n个归为a,最下面n个归为b 给出a,b和目标序列c,问最少多少次操作a.b转化为c 解析:将a.b放入哈希表,然后模拟操作过程直接dfs即可. AC代码: #include <cstdio> #include <iostream> #include <cstring> #include <map> using names

POJ 3069 Saruman&#39;s Army (贪心)

题目大意:直线上有N个点,点i的位置是Xi,从这N个点中选取若干,给他们加上标记,对每一个点,其距离为R以内的区域内必须有被标记的点.求至少需要多少个点被标记. 题目思路:设最左边的点:点p的坐标为x,那么离其距离为R的点的坐标为(x+R),我们应该标记的点应为坐标最接近且小于等于(x+R)的点p,则此时[x,p+R]范围内点点均被标记.依次进行下去,直到包含所有点为止. #include<stdio.h> #include<queue> #include<iostream&

POJ 1068--Parencodings--括号逆匹配(模拟)

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19655   Accepted: 11870 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

POJ 1422:Air Raid(最大独立集)

Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 3896 Description Consider a town where all the streets are one-way and each street leads from one intersection to another. It is also known that starting from an i

POJ 1775 sum of Factorial (数论)

链接:http://poj.org/problem?id=1775 Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics,meteorology, science,

POJ 1979 Red and Black (红与黑)

POJ 1979 Red and Black (红与黑) Time Limit: 1000MS    Memory Limit: 30000K Description 题目描述 There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A man is standing on a black tile. From a tile, he can move to