相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒。关于康托展开可以参考,其可用数学归纳法证明:http://www.cnblogs.com/1-2-3/archive/2011/04/25/generate-permutation-part2.html

 #include <bits/stdc++.h>
using namespace std;
map<int, int>visit;
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 = ;//初始的魔板
visit[front.num] = ;
q.push(front);
if(front.num == n)return 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 if(visit.find(tmp1.num) == visit.end()){
visit[tmp1.num] = ;
q.push(tmp1);
} Node tmp2 = front;
tmp2.num = op_b(front.num);
tmp2.path.push_back('B');
if (tmp2.num == n) {
return tmp2;
}
else if(visit.find(tmp2.num) == visit.end()){
visit[tmp2.num] = ;
q.push(tmp2);
} Node tmp3 = front;
tmp3.num = op_c(front.num);
tmp3.path.push_back('C');
if (tmp3.num == n) {
return tmp3;
}
else if(visit.find(tmp3.num) == visit.end()){
visit[tmp3.num] = ;
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;
} visit.clear();
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 ;
}

Sicily 1051: 魔板(BFS+排重)的更多相关文章

  1. hdu1430魔板(BFS+康托展开)

    做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...

  2. hdu.1430.魔板(bfs + 康托展开)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  3. Sicily 1151 魔板

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不 ...

  4. HDU - 1430 魔板 (bfs预处理 + 康托)

    对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...

  5. hdu 1430 魔板 (BFS+预处理)

    Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...

  6. Sicily 1150: 简单魔板(BFS)

    此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...

  7. HDU 1430 魔板(康托展开+BFS+预处理)

    魔板 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  8. 哈希+Bfs【P2730】 魔板 Magic Squares

    没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...

  9. 【搜索】魔板问题(BFS)

    [搜索]魔板问题 时间限制: 1 Sec  内存限制: 64 MB提交: 5  解决: 3[提交][状态][讨论版] 题目描述 据说能使持有者成为世界之主的上古神器隐藏在魔板空间,魔板由8个同样大小的 ...

随机推荐

  1. BZOJ3527[ZJOI]力

    无题面神题 原题意: 求所有的Ei=Fi/qi. 题解: qi被除掉了,则原式中的qj可以忽略. 用a[i]表示q[i],用b[j-i]来表示±1/((j-i)^2)(j>i时为正,j<i ...

  2. CMakeList.txt/Clion中添加头文件和库

    cmake_minimum_required(VERSION 3.6) project(capi_lua) include_directories(/usr/include) find_library ...

  3. [LeetCode] Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  4. [开源].NET高性能框架Chloe.ORM-完美支持.NET Core

    扯淡 这是一款轻量.高效的.NET C#数据库访问框架(ORM).查询接口借鉴 Linq(但不支持 Linq).借助 lambda 表达式,可以完全用面向对象的方式就能轻松执行多表连接查询.分组查询. ...

  5. WPF实现三星手机充电界面

    GitHub地址:https://github.com/ptddqr/wpf-samsung-phone-s5-charging-ui/tree/master 先上效果图 这个效果来自于三星S5的充电 ...

  6. 【WPF】释放图像资源, [删除时报另一进程占用]

    源:zhantianyou CODE private BitmapImage ReturnBitmap(string destFile) { // Read byte[] from png file ...

  7. TCP进制转换

    /// <summary> /// 将十六进制字符串转化为字节数组 /// </summary> /// <param name="src">& ...

  8. EntityFramework之监听者判断SQL性能指标

    前言 当我们利用EF这个ORM框架时,我们可能会利用LINQ或者原生的SQL语句来进行数据操作,此时我们无法确定我们的代码是否会给数据库带来一定的负载,当给数据库带来一定的压力时,由于项目中对数据进行 ...

  9. Android Hook技术

    原文:http://blog.csdn.net/u011068702/article/details/53208825 附:Android Hook 全面入侵监听器 第一步.先爆项目demo照片,代码 ...

  10. OpenCV图像的全局阈值二值化函数(OTSU)

    cv::threshold(GrayImg, Bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);//灰度图像二值化 CV_THRESH_OTSU是提取图像最 ...