首页 技术 正文
技术 2022年11月15日
0 收藏 624 点赞 3,147 浏览 4450 个字

自己之前的不见了。。这题是双向广搜即可过。。

 // 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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,024
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,514
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,362
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,776
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,854