[HDU 1430] 魔板
魔板
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1988 Accepted Submission(s): 407
1 2 3 4
8 7 6 5
对于魔板,可施加三种不同的操作,具体操作方法如下:
A: 上下两行互换,如上图可变换为状态87654321
B: 每行同时循环右移一格,如上图可变换为41236785
C: 中间4个方块顺时针旋转一格,如上图可变换为17245368
给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define N 326888 bool vis[N];
string way[N];
int fac[]={,,,,,,,,,}; struct node
{
string s;
string op;
}; int Find(string &s)
{
int i,j,res=;
bool has[]={};
for(i=;i<;i++)
{
int x=s[i]-'',y=;
for(j=;j<x;j++)
{
if(!has[j]) y++;
}
res+=y*fac[-i];
has[x]=;
}
return res+;
} void bfs()
{
int i,j;
node now,next;
queue<node> q;
memset(vis,,sizeof(vis));
now.s="";
now.op="";
q.push(now);
vis[Find(now.s)]=;
while(!q.empty())
{
now=q.front();
q.pop();
way[Find(now.s)]=now.op;
for(int i=;i<=;i++)
{
next=now;
if(i==)
{
next.op+='A';
swap(next.s[],next.s[]);
swap(next.s[],next.s[]);
swap(next.s[],next.s[]);
swap(next.s[],next.s[]);
}
else if(i==)
{
next.op+='B';
char ch=next.s[];
for(j=;j>=;j--) next.s[j]=next.s[j-];
next.s[]=ch;
ch=next.s[];
for(j=;j<;j++) next.s[j]=next.s[j+];
next.s[]=ch;
}
else
{
next.op+='C';
char ch=next.s[];
next.s[]=next.s[];
next.s[]=next.s[];
next.s[]=next.s[];
next.s[]=ch;
}
if(!vis[Find(next.s)])
{
vis[Find(next.s)]=;
q.push(next);
}
}
}
}
int main()
{
bfs();
string s1,s2;
while(cin>>s1>>s2)
{
map<char,char> mp;
for(int i=;i<;i++) //好吧、这儿写反了,WA- -
{
mp[s1[i]]=i++'';
}
for(int i=;i<;i++)
{
s2[i]=mp[s2[i]];
}
cout<<way[Find(s2)]<<endl;
}
return ;
}
[HDU 1430] 魔板的更多相关文章
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- HDU - 1430 魔板 【BFS + 康托展开 + 哈希】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路 我刚开始 想到的 就是 康托展开 但是这个题目是 多组输入 即使用 康托展开 也是会T的 ...
- hdu 1430 魔板 康托展开 + 很好的映射
http://acm.hdu.edu.cn/showproblem.php?pid=1430 如果从start ---> end,每一次都bfs进行,那么就肯定会超时. 考虑到先把start映射 ...
- hdu 1430 魔板 (BFS+预处理)
Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
- hdu 1430+hdu 3567(预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...
- ACM-康托展开+预处理BFS之魔板——hdu1430
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- Sicily 1051: 魔板(BFS+排重)
相对1150题来说,这道题的N可能超过10,所以需要进行排重,即相同状态的魔板不要重复压倒队列里,这里我用map储存操作过的状态,也可以用康托编码来储存状态,这样时间缩短为0.03秒.关于康托展开可以 ...
随机推荐
- Poj 1017 / OpenJudge 1017 Packets/装箱问题
1.链接地址: http://poj.org/problem?id=1017 http://bailian.openjudge.cn/practice/1017 2.题目: 总时间限制: 1000ms ...
- mysql中的sql时间格式转换
from_unixtime(unix_timestamp, format) 把时间戳转化为指定的格式 as: select from_unixtime(addTime, '%Y-%m-%d %h:%i ...
- sae-多个file_put_contents('saestor://public/text.txt',$data);只写第一次
多个file_put_contents('saestor://public/text.txt',$data); 只执行第一个文件的写入,永久存储也只需要一次写入 如果需要用户中间缓存文件,用tmpfs ...
- div重叠不变形
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- CentOS下shell显示-bash-4.1#不显示用户名路径的解决方法
CentOS下shell显示-bash-4.1$不显示用户名路径的解决方法 问题描述: CentOS下新增一个用户,登录进去之后shell脚本的信息如下: 而不是我们经常看到的username@hos ...
- extern "C"的作用
一.概述 在C语言的头文件中,经常可以看到如下的代码,那这个是什么作用呢? #ifdef __cplusplus extern "C" { #endif /*...*/ #ifde ...
- 【ElasticSearch】
ElasticSearch是基于Lucene开发的分布式搜索框架,包含如下特性: 分布式索引.搜索 索引自动分片.负载均衡 自动发现机器.组建集群 支持Restful 风格接口 配置简单等.
- .net中的Array,ArrayList和List
Array:任意类型,定长 ArrayList:任意类型,不定长 List:特定类型,不定长 Array和ArrayList是通过存储object类型实现可以存储任意类型数据,使用时需要拆箱和装箱
- Appdelegate 导航操作
隐藏返回按钮 self.navigationItem.hidesBackButton = YES; 设置导航的透明度 self.navigationController.navigationBar.t ...
- 【Selenium】自动化调试后C盘越来越大
在本机调试了一段时间自动化脚本后发现C盘占用越来越大,增长速度比较明显 通过360等工具清理系统垃圾表明并不好使 最后在系统临时文件中看到大量因为调试或不正常结束而Driver而产生的临时文件 具体方 ...