poj 2286 The Rotation Game
来源:互联网 发布:java缓存过程面试 编辑:程序博客网 时间:2024/05/29 19:01
萌萌哒的ID_A_STAR
题目大意:
给出你24个数,如图所示分布在这个优美的棋盘中,然后给出你8个操作ABCDEFGH,这个操作很好实现,就是拽一下字母所代表的这一列或这一行的7个数字,就是讲第一个数扔到最后一个,其他数依次向前进一位(很简单是不是~(≧▽≦)/~),然后要达到的目标很beautiful,使得中间8个数字相同,请输出操作过程和中间数字。
很明显,dfs是过不了的,要用ID_DFS+A_STAR
代码如下:
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>using namespace std;int chess[8+1][7+1]={ {0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 }, {0 ,1 ,3 ,7 ,12,16,21,23},//A {0 ,2 ,4 ,9 ,13,18,22,24},//B {0 ,11,10,9 ,8 ,7 ,6 ,5 },//C {0 ,20,19,18,17,16,15,14},//D {0 ,24,22,18,13,9 ,4 ,2 },//E {0 ,23,21,16,12,7 ,3 ,1 },//F {0 ,14,15,16,17,18,19,20},//G {0 ,5 ,6 ,7 ,8 ,9 ,10,11},//H };int reciprocal[8+1]={0,6,5,8,7,2,1,4,3};int rotation[24+1],maxdep,ans[10000005];bool check(){ for(int i=1;i<=3;i++) if(rotation[i+6]!=rotation[7]||rotation[i+15]!=rotation[7]) return false; return rotation[7]==rotation[12]&&rotation[7]==rotation[13];}bool a_star(int dep){ int temporary[3+1],temp=10; memset(temporary,0,sizeof(temporary)); for(int i=1;i<=3;i++) temporary[rotation[i+6]]++,temporary[rotation[i+15]]++; temporary[rotation[12]]++,temporary[rotation[13]]++; for(int i=1;i<=3;i++) temp=min(8-temporary[i],temp); if(temp+dep>maxdep) return false; return true;//剪枝1:如果当前这8个数字中与最多的数字不同的数字加上当前已走步数大于maxdep,那么此方案一定不成立 }bool id_dfs(int dep,int previous){ if(dep>maxdep) return false; if(!a_star(dep-1)) return false; for(int i=1;i<=8;i++){ if(i==reciprocal[previous])//剪枝2:两个操作是互逆的 continue; int temp=rotation[chess[i][1]]; for(int j=1;j<7;j++) rotation[chess[i][j]]=rotation[chess[i][j+1]]; rotation[chess[i][7]]=temp,ans[dep]=i; if(check()) return true; if(id_dfs(dep+1,i)) return true;//一定要记得return true!!!!!! for(int j=7;j>1;j--) rotation[chess[i][j]]=rotation[chess[i][j-1]]; rotation[chess[i][1]]=temp; } return false;}int main(){ while(scanf("%d",&rotation[1])&&rotation[1]){ for(int i=2;i<=24;i++) scanf("%d",&rotation[i]); if(check()){ printf("No moves needed\n%d\n",rotation[7]); continue; } for(int i=1;i;i++){ maxdep=i; if(id_dfs(1,0)){ for(int j=1;j<=i;j++) cout<<(char)(ans[j]+'A'-1); printf("\n%d\n",rotation[7]); break; } } } return 0;}
by >o< neighthorn
1 0
- POJ 2286 The Rotation Game
- POJ 2286 The Rotation Game
- POJ 2286 The Rotation Game
- POJ 2286 - The Rotation Game
- poj 2286 The Rotation Game
- POJ 2286 The Rotation Game
- poj 2286 The Rotation Game
- POJ 2286 The Rotation Game
- POJ--2286[The Rotation Game] IDA*
- POJ 2286 The Rotation Game IDA*
- POJ 2286 The Rotation Game (IDA*)
- POJ 2286 The Rotation Game (IDA*)
- POJ - 2286 - The Rotation Game (IDA*)
- poj 2286 The Rotation Game IDA*算法
- The Rotation Game (POJ 2286) 题解
- [poj 2286] The Rotation Game IDA*(dfs)
- POJ 2286 The Rotation Game IDA*
- POJ 2286 The Rotation Game(IDA*)
- unity路径跟随的算法
- AndroidStudio 2.0下怎么处理自定义View不显示而且预览界面看不到的问题以及报错问题
- 路由算法之——传统SPF路由
- android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)
- SpringMVC处理静态资源请求
- poj 2286 The Rotation Game
- 设计模式之装饰模式
- srand((unsigned)time(NULL))详解
- UVA1658
- unity控制摄像机视图大小的算法
- 12. SpringMVC_使用POJO作为参数
- 2.3项目编码注意事项
- 采用Div布局元素位置确定(float、clear、position、z-index)
- butterknife控件绑定