[poj 2286] The Rotation Game IDA*(dfs)
2016-04-08 14:20
405 查看
The Rotation Game
Time Limit: 15000MS Memory Limit: 150000K
Total Submissions: 5852 Accepted: 1968
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
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
题目链接:http://poj.org/problem?id=2286
题意:给你上图,只有3数,用最小的步数把图中中间8点变为同一数字
思路:
dfs搜索考虑迭代加深控制dfs深度;
剪支:
1.只用把(上,下)等无用工减去;
2.A*计算最小步数;
代码:
Time Limit: 15000MS Memory Limit: 150000K
Total Submissions: 5852 Accepted: 1968
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' toH’, 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
题目链接:http://poj.org/problem?id=2286
题意:给你上图,只有3数,用最小的步数把图中中间8点变为同一数字
思路:
dfs搜索考虑迭代加深控制dfs深度;
剪支:
1.只用把(上,下)等无用工减去;
if(fa!=ver[i])
2.A*计算最小步数;
if(dep+h(tmp)>ans) return 0;
代码:
#include<iostream> #include<stdio.h> #include<string.h> using namespace std; const int b[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 ver[]={5,4,7,6,1,0,3,2}; int ans; int a[30]; int h(int *tmp) 计算最最短长度 { int tt[4]={0,0,0,0}; for(int i=0;i<8;i++) { tt[tmp[b[i][2]]]++; } tt[1]/=2; tt[2]/=2; tt[3]/=2; tt[tmp[7]]++; tt[tmp[11]]++; tt[tmp[12]]++; tt[tmp[16]]++; return 8-max(tt[1],max(tt[2],tt[3])); } bool check(int *tmp) { int tt=tmp[7]; for(int i=0;i<8;i++) if(tmp[b[i][2]]!=tt) return 0; if(tmp[11]!=tt) return 0; if(tmp[12]!=tt) return 0; if(tmp[16]!=tt) return 0; return 1; } void rot(int *tmp,int x) //拉编号为i的带子 { int tt=tmp[b[x][0]]; for(int i=0;i<6;i++) tmp[b[x][i]]=tmp[b[x][i+1]]; tmp[b[x][6]]=tt; } int pre[30]; bool dfs(int dep,int *tmp,int fa) { if(dep+h(tmp)>ans) return 0; if(check(tmp)) { for(int i=1;i<=dep;i++) printf("%c",pre[i]+65); printf("\n"); printf("%d\n",tmp[6]); return 1; } for(int i=0;i<8;i++) if(fa!=ver[i]) { rot(tmp,i); pre[dep+1]=i; if(dfs(dep+1,tmp,i)) return 1; rot(tmp,ver[i]); } return 0; } void id_star() { for(ans=1;ans<=24;ans++) if(dfs(0,a,-1)) return ; } int main() { int x; while(scanf("%d",&x)) { if(!x) break; a[0]=x; for(int i=1;i<24;i++) scanf("%d",&a[i]); if(h(a)==0) { printf("No moves needed\n"); printf("%d\n",a[6]); continue; } id_star(); } return 0; }
相关文章推荐
- C++的<unordered_set>
- win7常用快捷键
- 持之以恒,贵在坚持
- 动态元素的事件必须在元素存在后添加的事件才是有效的
- 在页面中添加Token防止越权访问
- 第十次课作业(风险管理、项目收尾、知识产权)
- 微信红包算法和微信抽奖
- cf324B. Kolya and Tanya
- 【学习笔记】3D图形核心基础精炼版-1:入门概念
- fork与vfork区别及用exit于return返回时的区别
- 设计模式 原型模式 Prototype
- 关于tomcat内存查看
- Codeforces Beta Round #92 (Div. 1 Only) A. Prime Permutation 暴力
- 关键词匹配(Ac自动机模板题)
- Ubuntux下QT编写 linux嵌入式开发板的程序
- 关于matlab用export_fig去掉白边、保存图像等问题
- Javascript中escape(), encodeURI()和encodeURIComponent()之精析与比较
- ArcGIS for Android地图控件的5大常见操作
- MTK的Flashtool 加载scatter-loading是报错
- Sqlite3数据块以BLOB字段存储二进制数据块