UVA 1343 The Rotation Game
题意:
给出图,往A-H方向旋转,使中间8个格子数字相同。要求旋转次数最少,操作序列字典序尽量小。
分析:
用一维数组存24个方格。二维数组代表每个方向对应的7个方格。IDA*剪枝是当8-8个方格中重复字母最多的那个字母数量>maxd。
代码:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
int a[24];
int d[8][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},
};
int g[8]={6,7,8,11,12,15,16,17};
int ans;
string p;
bool dfs(int now,int maxd,string path)
{
//cout<<1<<endl;
if(now==maxd)
{
//cout<<maxd<<endl;
int ok=1;
for(int i=1;i<8;i++)
{
if(a[g[i]]!=a[g[i-1]])
{
ok=0;
break;
}
}
if(!ok)
return false;
ans=a[g[0]];
p=path;
return true;
}
int b[3]={0,0,0};
for(int i=0;i<8;i++)
++b[a[g[i]]-1];
int dd=8-max(max(b[0],b[1]),b[2]);
if(now+dd>maxd)
return false;
for(int i=0;i<8;i++)
{
int v=a[d[i][0]];
for(int j=0;j<6;j++)
a[d[i][j]]=a[d[i][j+1]];
a[d[i][6]]=v;
if(dfs(now+1,maxd,path+char(i+'A')))
return true;
v=a[d[i][6]];
for(int j=6;j>0;j--)
a[d[i][j]]=a[d[i][j-1]];
a[d[i][0]]=v;
}
return false;
}
int main()
{
while(scanf("%d",&a[0])&&a[0])
{
for(int i=1;i<24;i++)
scanf("%d",&a[i]);
//cout<<1<<endl;
ans=-1;
for(int maxd=0;;++maxd)
{
if(dfs(0,maxd,""))
{
if(maxd==0)
printf("No moves needed\n");
else
cout<<p<<endl;
printf("%d\n",ans);
break;
}
}
}
}
UVA 1343 The Rotation Game的更多相关文章
- UVa 1343 The Rotation Game (状态空间搜索 && IDA*)
题意:有个#字型的棋盘,2行2列,一共24个格. 如图:每个格子是1或2或3,一共8个1,8个2,8个3. 有A~H一共8种合法操作,比如A代表把A这一列向上移动一个,最上面的格会补到最下面. 求:使 ...
- UVA - 1343 The Rotation Game (BFS/IDA*)
题目链接 紫书例题. 首先附上我第一次bfs+剪枝TLE的版本: #include<bits/stdc++.h> using namespace std; typedef long lon ...
- UVA 1343 - The Rotation Game-[IDA*迭代加深搜索]
解题思路: 这是紫书上的一道题,一开始笔者按照书上的思路采用状态空间搜索,想了很多办法优化可是仍然超时,时间消耗大的原因是主要是: 1)状态转移代价很大,一次需要向八个方向寻找: 2)哈希表更新频繁: ...
- 【UVa】1343 The Rotation Game(IDA*)
题目 题目 分析 lrj代码.... 还有is_final是保留字,害的我CE了好几发. 代码 #include <cstdio> #include <algorit ...
- 【例 7-12 UVA - 1343】The Rotation Game
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 每次抽动操作最多只会让中间那一块的区域离目标的"距离"减少1. 以这个作为剪枝. 枚举最大深度. ...
- UVA_Rotation Game<旋转游戏> UVA 1343
The rotation game uses a # shaped board, which can hold 24 pieces of square blocks (see Fig.1). The ...
- UVA 1379 - Pitcher Rotation(DP + 贪心)
题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=4125" rel="nofo ...
- uva 1343 非原创
uva1343 原作者 题目题意是:给你的棋盘,在A-H方向上可以拨动,问你最少拨动几次可以是中心图案的数字一致 解题思路:回溯法,剪枝 其中要把每次拨动的字母所代表的位置提前用数组表示: 然后在如果 ...
- UVa 1343 旋转游戏(dfs+IDA*)
https://vjudge.net/problem/UVA-1343 题意:如图所示,一共有8个1,8个2和8个3,如何以最少的移动来使得中间8个格子都为同一个数. 思路:状态空间搜索问题. 用ID ...
随机推荐
- ibatis错误汇总
1) 错误:The prefix "context" for element "context:property-placeholder" is not bou ...
- Android 四大组件之 BroadcastReceiver
0 简介 BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的 广播. 在Android系统中,广播体现在方方面面,例 ...
- Identity 验证,Authorize 特性
多类型角色访问 //[Authorize] //[Authorize(Roles = "User")] //[Authorize(Roles="Administrator ...
- android如何调用显示和隐藏系统默认的输入法(一)
1.调用显示系统默认的输入法 方法一. InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_MET ...
- 查看并设置oracle并发连接数
1.Sql代码1.select count(*) from v$process select count(*) from v$process --当前的数据库连接数2.Sql代码1.select v ...
- (转载) socket:10038错误{winSock的一个bug:当closesocket多次错误使用时会导致问题}
这几天想在一个开源的代码上进行修改,以期研发出一个产品出来. 程序原来是单线程网络程序,需要修改为多线程,修改之后,总是出问题,辅助线程中的recv函数总是运行一阵子之后收到长度为-1的数 ...
- C++程序设计实践指导1.4正整数转换为字符串改写要求实现
改写要求1:改为适合处理超长整数 #include <cstdlib> #include <iostream> #include <string> using na ...
- view import symbols
nm -D [file]|grep -w U or objdump -T [file]|grep "*UND*"
- sublime远程连接到linux主机
sublime远程连接到linux主机 sublime远程连接到linux主机 微信开发,直接使用sublime的sftp功能修改wx_sample.php 1.为sublime安装安装包管理插件Pa ...
- windows 数据类型转换为 dotnet 数据类型
Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...