POJ 2286 HDU 1667 ZOJ 2396 The Rotation Game IDA*迭代加深搜索
2011-10-15 21:38
585 查看
The Rotation Game
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
Sample Output
Source
Shanghai 2004
IDA*入门
代码:
Time Limit: 15000MS | Memory Limit: 150000K | |
Total Submissions: 3819 | Accepted: 1262 |
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
Source
Shanghai 2004
IDA*入门
代码:
#include<cstdio> #include<algorithm> using namespace std; int deep,s[25],b[]={5,4,7,6,1,0,3,2}; int p[9][7]= { 0,2,6,11,15,20,22, 1,3,8,12,17,21,23, 10,9,8,7,6,5,4, 19,18,17,16,15,14,13, 23,21,17,12,8,3,1, 22,20,15,11,6,2,0, 13,14,15,16,17,18,19, 4,5,6,7,8,9,10, 7,8,11,12,15,16,17 }; char path[105],t; int ok(int *s) { for(int i=0;i<7;i++) if(s[p[8][i]]!=s[6]) return 0; return 1; } int cal() { int i,j,a[4]={0}; for(i=1;i<=3;i++) { a[i]+=s[6]==i; for(j=0;j<7;j++) a[i]+=s[p[8][j]]==i; } return 8-max(a[1],max(a[2],a[3])); } int dfs(int k) { int i,j,h=cal(); if(h==0) return 1; if(h+k>deep) return 0; for(i=0;i<8;i++) { t=s[p[i][0]]; for(j=0;j<6;j++) s[p[i][j]]=s[p[i][(j+1)%8]]; s[p[i][6]]=t; path[k]=i+'A'; if(dfs(k+1)) return 1; t=s[p[b[i]][0]]; for(j=0;j<6;j++) s[p[b[i]][j]]=s[p[b[i]][(j+1)%8]]; s[p[b[i]][6]]=t; } return 0; } int main() { int i; while(scanf("%d",s),s[0]) { for(i=1;i<24;i++) scanf("%d",&s[i]); if(ok(s)) { printf("No moves needed\n%d\n",s[6]); continue; } deep=0; while(1) { if(dfs(0)) break; deep++; } for(i=0;i<deep;i++) putchar(path[i]); printf("\n%d\n",s[6]); } }
相关文章推荐
- POJ 2286/HDU 1667 The Rotation Game 搜索-IDA*+迭代加深
- HDU 1667 百练 1288 POJ 2286
- hdu 1667 /poj 2286 The Rotation Game(经典迭代加深)
- poj 2286 The Rotation Game 迭代加深搜索
- POJ 2286 The Rotation Game 搜索-IDA*+迭代加深
- POJ 2286 - The Rotation Game IDA(迭代加深搜索,DFSID)
- HDU - 1560 DNA sequence 迭代加深搜索
- 【搜索进阶】HDU 1667 The Rotation Game
- hdu 1560 DNA sequence(迭代加深搜索)
- 博弈小结(POJ ,HDU,ZOJ博弈问题小结)
- poj 2396 zoj 1994 Budget(有源汇上下界的可行流)
- hdu 2616 Kill the monster(dfs+最优化剪枝+迭代加深搜索)
- UVa 10183/EOJ 1297/POJ 2413/HDU 1316/ZOJ 1962 How many Fibs?【Java大整数类应用】
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
- HDU 1043 && POJ 1077 Eight bfs || 双向bfs || A*搜索
- POJ 1325、ZOJ 1364、HDU 1150 Machine Schedule - from lanshui_Yang
- UVa 439/HDU 1372/POJ 2243/ZOJ 1091 Knight Moves(BFS&纯数学方法)
- [poj] 2396 [zoj] 1994 budget || 有源汇的上下界可行流
- zoj 1284||poj 1528||hdu 1323(筛选法求因子和)
- uva 10129 poj 1386 hdu 1116 zoj 2016 play on words