Sicily 1150: 简单魔板(BFS)
2024-08-25 15:53:58
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可
#include <bits/stdc++.h>
using namespace std; int op_a(int n) {//操作A(上下行互换)
int low = n % ;
int high = n / ;
return low * + high;
} int op_b(int n) {//操作B(每次以行循环右移一个)
int low = n % ;
int high = n / ;
int h = high % ;
high = h * + high / ;
int l = low % ;
low = l * + low / ;
return high * + low;
} int op_c(int n) {//操作C(中间四小块顺时针转一格)
int a[];
for (int i = ; i < ; i++) {
a[-i] = n % ;
n = n / ;
}
int ans = a[] * +
a[] * +
a[] * +
a[] * +
a[] * +
a[] * +
a[] * +
a[];
return ans;
}
struct Node {
int num;//num储存状态,用8位的十进制数来储存状态
vector<char> path;//path储存操作
};
Node bfs(int step, int n) {
queue<Node> q;
Node front;
front.num = ;//初始的魔板
q.push(front);
while (!q.empty()) {
front = q.front();
q.pop();
if (front.path.size() > step) {
return front;//如果操作的次数大于step数,返回
}
//依次进行三种操作
Node tmp1 = front;
tmp1.num = op_a(front.num);
tmp1.path.push_back('A');
if (tmp1.num == n) {
return tmp1;//如果得到目标状态则返回
}
else {
q.push(tmp1);
} Node tmp2 = front;
tmp2.num = op_b(front.num);
tmp2.path.push_back('B');
if (tmp2.num == n) {
return tmp2;//如果得到目标状态则返回
}
else {
q.push(tmp2);
} Node tmp3 = front;
tmp3.num = op_c(front.num);
tmp3.path.push_back('C');
if (tmp3.num == n) {
return tmp3;//如果得到目标状态则返回
}
else {
q.push(tmp3);
} }
}
int main(){
int step;
while (cin >> step && step != -) {
int ans = ;
int a;
for (int i = ; i < ; i++) {//将魔板转换成数字
cin >> a;
ans = * ans + a;
}
Node node = bfs(step, ans);
if (node.path.size() > step) cout << "-1" << endl;//如失败则输出-1,否则输出path
else {
cout << node.path.size() << ' ';
for (int i = ; i < node.path.size(); i++) {
cout << node.path[i];
}
cout << endl;
} }
return ;
}
最新文章
- GPRS 接入外网的过程
- C#模拟键盘输入(一)
- Mysql查询阻塞初探
- iOS 7.0获取iphone UDID 【转】
- python15-day1课堂随机
- Spring基础知识及bean的配置
- mac linux netstat -n
- 简单的实现QQ通信功能(三)
- GDI画验证码
- Android Volley 之自定义Request
- shuffle一个简单的过程叙述性说明
- ASP.NET Web安装程序
- markdown常用语法教程
- 【scikit-learn 0.19 中文文档 】安装 scikit-learn | ApacheCN
- C#后台生成验证码
- 互相关(cross-correlation)及其在Python中的实现
- MSChart的研究(转)
- linux下i2c的驱动架构分析和应用
- vijos 1128 N个数选K个数 (DFS )
- Python+OpenCV图像处理(一)——读取显示一张图片
热门文章
- [个人翻译]Redis 集群教程(中)
- 两年来的core折腾之路几点总结,附上nginx启用http2拿来即用的配置
- Android中关于cpu/cpuset/schedtune的应用
- AAU
- javascript正则表达式(RegExp)简述
- 详解三种缓存过期策略LFU,FIFO,LRU(附带实现代码)
- 简单的maven配置
- NTFS交换数据流隐写的应用
- Codeforces 710F String Set Quries
- HTML精确定位:scrollLeft,scrollWidth,clientWidth,offsetWidth