Sicily 1051: 魔板(BFS+排重)
相对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+排重)的更多相关文章
- hdu1430魔板(BFS+康托展开)
做这题先看:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description 在魔方风靡全球之后不久,Rubik先 ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- Sicily 1151 魔板
Constraints Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge Description 魔板由8个大小相同方块组成,分别用涂上不 ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- hdu 1430 魔板 (BFS+预处理)
Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...
- Sicily 1150: 简单魔板(BFS)
此题可以使用BFS进行解答,使用8位的十进制数来储存魔板的状态,用BFS进行搜索即可 #include <bits/stdc++.h> using namespace std; int o ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- 哈希+Bfs【P2730】 魔板 Magic Squares
没看过题的童鞋请去看一下题-->P2730 魔板 Magic Squares 不了解康托展开的请来这里-->我这里 至于这题为什么可以用康托展开?(瞎说时间到. 因为只有8个数字,且只有1 ...
- 【搜索】魔板问题(BFS)
[搜索]魔板问题 时间限制: 1 Sec 内存限制: 64 MB提交: 5 解决: 3[提交][状态][讨论版] 题目描述 据说能使持有者成为世界之主的上古神器隐藏在魔板空间,魔板由8个同样大小的 ...
随机推荐
- [LeetCode] Russian Doll Envelopes 俄罗斯娃娃信封
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [LeetCode] Moving Average from Data Stream 从数据流中移动平均值
Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
- solr.net的使用
引子 最近在做一个日志系统,用普通关系型数据库做数据查询遇到了查询的瓶颈,想到了用成熟的搜索应用服务,我所知道的比较成熟的搜索应用服务有solr和es(elasticsearch),由于时间比较仓促, ...
- Ceph RGW服务 使用s3 java sdk 分片文件上传API 报‘SignatureDoesNotMatch’ 异常的定位及规避方案
import java.io.File; import com.amazonaws.AmazonClientException; import com.amazonaws.auth.profile ...
- 常用的shell脚本
[root@WEB1-live sh]# cat licai_fabu.sh #!/bin/bash pid=` ps -ef | grep java | grep '8011' | awk '{pr ...
- 关于CAJViewer 无法获取document路径问题
修改注册表 进入注册表编辑器: win+R >>输入 regedit 如下图: 修改关键注册表项(两项相同值应同时修改) 1.HKEY_CURRENT_USER\Software\Mic ...
- bzoj1251
1251: 序列终结者 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 3776 Solved: 1581[Submit][Status][Discu ...
- perl
introduction: http://www.yiibai.com/perl/perl_introduction.html functions: http://www.yiibai.com/per ...
- AndroidStudio 1.4配置NDK
AndroidStudio(AS) 1.3之后已经支持NDK,这为NDK开发提供了极大的便利,不在需要配置各种头疼的MK文件,简单的九步就可完成配置.要说明的是,第一次配置AS一定要有耐心. 0,下载 ...