自己之前的不见了。。
这题是双向广搜即可过。。
 // Colour Hash (色彩缤纷游戏)
// PC/UVa IDs: 110807/704, Popularity: B, Success rate: average Level: 3
// Verdict: Accepted
// Submission Date: 2011-08-28
// UVa Run Time: 0.048s
//
// 版权所有(C)2011,邱秋。metaphysis # yeah dot net
//
// 若从给定状态进行单向搜索,由于状态较多,容易 TLE,故采用双向搜索的办法,逆向搜索:从目标状态
// 搜索 8 步,把所有得到的结果记录在集合 A 中;正向搜索:从给定状态搜索 9 步,若在搜索过程中生成
// 的某个状态在集合 A 中,则表明在 16 步内能找到解,否则无法找到解。
//
// 这里较为关键的是如何表示游戏的当前状态,以避免在集合 A 中添加重复的状态,可以使用字符串来表示
// 当前的滑块状态。集合 A 可以使用 map 来判断是否已经有重复的状态产生。 #include <iostream>
#include <queue>
#include <map> using namespace std; #define LEFT_CLOCKWISE 1 // 左侧顺时针。
#define RIGHT_CLOCKWISE 2 // 右侧顺时针。
#define LEFT_COUNTERCLOCKWISE 3 // 左侧逆时针。
#define RIGHT_COUNTERCLOCKWISE 4 // 右侧逆时针。 #define NWHEEL 24 // 滑块总数目。
#define HALF_WHEEL 9 // 左侧滑块的数目。
#define MIDDLE_WHEEL 3 // 中间滑块的数目。
#define BACKWARD_DEPTH 8 // 逆向搜索深度。 // 目标状态。存储方式为左侧滑块-右侧滑块-中间滑块,因为编号为 10 的滑块占两个字符,故用 10 的英
// 文(ten)首字母 T 来表示。
string target = "034305650078709T90121"; // 逆向搜索的缓存,使用字符串来表示状态和旋转序列。
map < string, string > cache; // 旋转操作的逆。1 的逆为 3,2 的逆为 4,依此类推。
int reverse[] = { , , , }; // 表示滑块当前状态的结构。
struct node
{
string config; // 滑块状态。
string sequences; // 到达此位置的旋转序列。
}; // 按指定的方向旋转滑块。
void rotate(string &config, int direction)
{
// 获取中间滑块部分。
string middle = config.substr(HALF_WHEEL * ); switch (direction)
{
// 左侧滑块顺时针旋转。
case LEFT_CLOCKWISE: config[HALF_WHEEL * ] = config[HALF_WHEEL - ];
config[HALF_WHEEL * + ] = config[HALF_WHEEL - ];
config[HALF_WHEEL * + ] = middle[]; for (int i = HALF_WHEEL - ; i >= ; i--)
config[i] = config[i - ];
config[] = middle[];
config[] = middle[]; break; // 右侧滑块顺时针旋转。
case RIGHT_CLOCKWISE: config[HALF_WHEEL * ] = middle[];
config[HALF_WHEEL * + ] = config[HALF_WHEEL];
config[HALF_WHEEL * + ] = config[HALF_WHEEL + ]; for (int i = HALF_WHEEL; i <= (HALF_WHEEL * - ); i++)
config[i] = config[i + ];
config[HALF_WHEEL * - ] = middle[];
config[HALF_WHEEL * - ] = middle[]; break; // 左侧滑块逆时针旋转。
case LEFT_COUNTERCLOCKWISE: config[HALF_WHEEL * ] = middle[];
config[HALF_WHEEL * + ] = config[];
config[HALF_WHEEL * + ] = config[]; for (int i = ; i <= HALF_WHEEL - ; i++)
config[i] = config[i + ];
config[HALF_WHEEL - ] = middle[];
config[HALF_WHEEL - ] = middle[]; break; // 右侧滑块逆时针旋转。
case RIGHT_COUNTERCLOCKWISE: config[HALF_WHEEL * ] = config[HALF_WHEEL * - ];
config[HALF_WHEEL * + ] = config[HALF_WHEEL * - ];
config[HALF_WHEEL * + ] = middle[]; for (int i = * HALF_WHEEL - ; i >= HALF_WHEEL + ; i--)
config[i] = config[i - ];
config[HALF_WHEEL + ] = middle[];
config[HALF_WHEEL] = middle[]; break;
}
} // 从目标状态生成 8 步内所有可能产生的状态,使用宽度优先搜索的方法,用 map 存储生成的状态和相应
// 的旋转序列。
void backward_search(string config)
{
queue <node> open; node tmp;
tmp.config = config;
tmp.sequences = ""; open.push(tmp); while (!open.empty())
{
node copy = open.front();
open.pop(); // 当扩展的深度达到 8 层后停止在此状态上继续扩展。
if (copy.sequences.length() >= BACKWARD_DEPTH)
continue; for (int i = LEFT_CLOCKWISE; i <= RIGHT_COUNTERCLOCKWISE; i++)
{
// 跳过无效的移动,例如前一步采用了左侧顺时针旋转,则当前若使用
// 左侧逆时针旋转会回到上一步的状态。
if (copy.sequences.length() > )
{
// 注意使用的是旋转操作的逆,故需还原后判断。
int last_rotate = reverse[copy.sequences[] - '' - ];
if (last_rotate != i && ((last_rotate + i) == ||
(last_rotate + i) == ))
continue;
} string t = copy.config;
rotate(t, i); if (cache.find(t) == cache.end())
{
node successor;
successor.config = t;
// 记录逆向搜索的旋转序列时,使用当前旋转的逆。
successor.sequences = (char)('' + reverse[i - ]) + copy.sequences;
open.push(successor); cache.insert(make_pair<string, string>(t, successor.sequences));
}
}
}
} // 进行正向搜索,采用宽度优先搜索模式。
bool forward_search(string config)
{
queue <node> open; node tmp;
tmp.config = config;
tmp.sequences = ""; open.push(tmp); while (!open.empty())
{
node copy = open.front();
open.pop(); // 已经找到在缓存中的状态,输出旋转序列。
if (cache.find(copy.config) != cache.end())
{
cout << copy.sequences;
map <string, string>::iterator it = cache.find(copy.config);
cout << (*it).second << endl; return true;
} // 搜索深度为 9。
if (copy.sequences.length() >= (BACKWARD_DEPTH + ))
continue; for (int i = LEFT_CLOCKWISE; i <= RIGHT_COUNTERCLOCKWISE; i++)
{
// 若前后两步构成互补状态则跳过。
if (copy.sequences.length() > )
{
int size = copy.sequences.length();
int last_rotate = copy.sequences[size - ] - '';
if (last_rotate != i && ((last_rotate + i) == ||
(last_rotate + i) == ))
continue;
} string t = copy.config;
rotate(t, i); node successor;
successor.config = t;
successor.sequences = copy.sequences + (char)('' + i); open.push(successor);
}
} return false;
} // 和目标状态比较,确定是否为已解决状态。
bool solved(string config)
{
for (int i = ; i < target.length(); i++)
if (config[i] != target[i])
return false; return true;
} int main(int ac, char *av[])
{
string config;
int c;
int cases; // 先生成逆向搜索的结果以备查。
backward_search(target); cin >> cases;
while (cases--)
{
// 读入初始状态。
config.clear();
for (int i = ; i < NWHEEL; i++)
{
cin >> c;
if (c == )
config.append(, 'T');
else
config.append(, c + '');
} // 调整表示形式。
config = config.substr(, HALF_WHEEL) +
config.substr(HALF_WHEEL + MIDDLE_WHEEL, HALF_WHEEL) +
config.substr( * HALF_WHEEL + MIDDLE_WHEEL); // 先判断是否已经为解决状态。
if (solved(config))
{
cout << "PUZZLE ALREADY SOLVED" << endl;
continue;
} // 进行正向搜索查找。
if (!forward_search(config))
cout << "NO SOLUTION WAS FOUND IN 16 STEPS" << endl;
} return ;
}

uva 704的更多相关文章

  1. [UVA] 704 Colour Hash

    所谓"周界搜索",练习搜索的好题,双向宽搜/迭代加深均可,还有很多细节有待完善,判重有比set更优的结构,宽搜还没写,先存一下. //Writer:GhostCai &&a ...

  2. UVa 10012 - How Big Is It? 堆球问题 全排列+坐标模拟 数据

    题意:给出几个圆的半径,贴着底下排放在一个长方形里面,求出如何摆放能使长方形底下长度最短. 由于球的个数不会超过8, 所以用全排列一个一个计算底下的长度,然后记录最短就行了. 全排列用next_per ...

  3. 1Z0-053 争议题目解析704

    1Z0-053 争议题目解析704 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 704.View the Exhibit and examine the data manipul ...

  4. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  5. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  6. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  7. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  8. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  9. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

随机推荐

  1. 数据结构C++,线性表的实现

    #include <iostream>#include <sstream>#include <fstream>#include <cmath>#incl ...

  2. iOS版本、iPhone版本、Xcode版本比对

    iOS版本 iPhone版本 Xcode版本 其他 2003年 Xcode1.0 2005年4月29日 Xcode2.0 2007年1月9日 iPhone OS(iOS1): 虚拟键盘.谷歌地图 第一 ...

  3. E20170911-hm

    specification n.     规格; 说明书; 详述;

  4. 关于我们ajax异步请求的方法与知识

      做前端开发的朋友对于ajax异步更新一定印象深刻,作为刚入坑的小白,今天就和大家一起聊聊关于ajax异步请求的那点事.既然是ajax就少不了jQuery的知识,推荐大家访问www.w3school ...

  5. Django day04 路由控制

    Django请求的整个的生命周期 Django中路由控制的作用: 一: 简单配置 url 是一个函数 -第一个参数是正则表达式(如果要精确匹配:'^publish'/$ 以^开头,以$结尾) -第二个 ...

  6. CMake之CMakeLists.txt编写入门

    自定义变量 主要有隐式定义和显式定义两种. 隐式定义的一个例子是PROJECT指令,它会隐式的定义< projectname >_BINARY_DIR和< projectname & ...

  7. ACM_来自不给标题的菜鸟出题组(巴什博弈+素数判定)

    来自不给标题的菜鸟出题组 Time Limit: 2000/1000ms (Java/Others) Problem Description: 大B和小b合作出一道程序设计月赛的题,他们的想法是给定一 ...

  8. RabbitMQ 官方NET教程(五)【Topic】

    在上一个教程中,我们改进了我们的日志记录系统.我们使用direct类型转发器,使得接收者有能力进行选择性的接收日志,,而非fanout那样,只能够无脑的转发 虽然使用direct类型改进了我们的系统, ...

  9. 【技术累积】【点】【sql】【17】了解索引

    先上结论 数据库数据以平衡树进行聚合索引--主键的作用: 数据每行都存在叶子节点: 单独字段的索引,单独存在,且将该字段值取出: 单独字段的索引,查到对应的主键id,再通过聚合索引查到数据: 多字段索 ...

  10. 浅谈Web缓存-缓存的实现过程详解

    在前端开发中,性能一直都是被大家所重视的一点,然而判断一个网站的性能最直观的就是看网页打开的速度.其中提高网页反应速度的一个方式就是使用缓存.一个优秀的缓存策略可以缩短网页请求资源的距离,减少延迟,并 ...