[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秒.关于康托展开可以 ...
随机推荐
- About 'atoi'
在很多地方我们看到的都是:atoi 是一个 C Reference function atoi() convert a string to an integer. This function of s ...
- C# 调用load事件
在一个函数或者事件中调用另外的事件,例如调用Load事件 private void EventForm_Load(object sender, EventArgs e) { //相关内容 } priv ...
- Project Israfil -- 支持多个音乐平台的开源音乐服务
Israfil |关于|当前进度|开发日志|下载软件|编译|参与项目|编译状态|版权|协议| LER0ever Project Israfil Provide Unified Music Se ...
- OpenJudge 2773 2726 2727 采药
1.链接地址: http://bailian.openjudge.cn/practice/2773/ http://bailian.openjudge.cn/practice/2726/ http:/ ...
- Openjudge/Poj 1183 反正切函数的应用
1.链接地址: http://bailian.openjudge.cn/practice/1183 http://poj.org/problem?id=1183 2.题目: 总时间限制: 1000ms ...
- OpenJudge 2810(1543) 完美立方 / Poj 1543 Perfect Cubes
1.链接地址: http://bailian.openjudge.cn/practice/2810/ http://bailian.openjudge.cn/practice/1543/ http:/ ...
- CentOS7 systemctl的使用
# 查看正在运行的服务 systemctl # 查看某个服务的状态 systemctl status httpd.service # 启动服务 systemctl start foo.service ...
- mysql的1045解决方法
mysql的连接方式有两种: UNIX域套接字连接,如: mysql -u root -p mysql -h localhost -u root -p TCP/IP套接字连接,如: mysql -h ...
- jQuery—一些常见方法(3)【width(),innerWidth(),outerWidth()】
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【pano2vr】网页Flash中简单实现炫酷的3D模型制作
花了两天时间学习如何能够高效的实现3D模型效果,毕竟是从0开始学习,感觉pano2vr这款软件挺容易上手,并且可以很容易实现简单的热点交互,可以根据交互需求设置皮肤,故将这一款软件推荐给大家: 1.简 ...