[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秒.关于康托展开可以 ...
随机推荐
- Update files embedded inside CAB file.
References: https://community.flexerasoftware.com/showthread.php?182791-Replace-a-single-file-embedd ...
- C++里的int 和string类型相互转换
C++不像Java和C#一样在进行数据类型转换时直接调用一些类方法就可以了,使用起来很简单. 一个很简单的例子就是string str=“D:\\”+1+“.txt”;这在Java或者C#里面是可以自 ...
- Redis与Memcached的比较
网络IO模型 Memcached 是多线程,非阻塞IO复用的网络模型,分为监听主线程和worker子线程,监听线程监听网络连接,接受请求后,将连接描述字pipe 传递给worker线程,进行读写IO, ...
- Ambry: LinkedIn’s Scalable Geo-Distributed Object Store
https://github.com/linkedin/ambry http://www.open-open.com/lib/view/open1464828607502.html
- lamp 中基本配置常识
// apache// 禁止访问目录// 开启 url重写// 重写定义错误页面// 日志分页// 增加并发连接数// 设置连接连接的时间 // threadsPerChild // 每个进程的线程数 ...
- Yii通过控制台命令创建定时任务
假设Yii项目路径为 /home/apps/ 1. 创建文件 /home/apps/protected/commands/crons.php <?php $yii = '/home/apps/f ...
- UIExtendedEdge
在IOS7以后 ViewController 开始使用全屏布局的,而且是默认的行为通常涉及到布局.就离不开这个属性 edgesForExtendedLayout,它是一个类型为UIExtendedEd ...
- 【存储器相关】RAM、SRAM、SDRAM、ROM、EPROM、EEPROM、Flash存储器区别
常见存储器概念:RAM.SRAM.SDRAM.ROM.EPROM.EEPROM.Flash存储器可以分为很多种类,其中根据掉电数据是否丢失可以分为RAM(随机存取存储器)和ROM(只读存储器),其中R ...
- Grails的redirect无法跳转时的一个可能原因
由于controller的命名一般首字母大写,如Login 此时如 class LoginController { def index = { redirect(action:Login, param ...
- webkit私有css3属性 -webkit-overflow-scrolling:touch;
-webkit-overflow-scrolling:touch;/*允许独立的滚动区域和触摸回弹*/ 这个属性可以提高滚动的平滑度