hdu 1430
魔板
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1649 Accepted Submission(s):
348
1
2 3 4
8 7 6 5
对于魔板,可施加三种不同的操作,具体操作方法如下:
A:
上下两行互换,如上图可变换为状态87654321
B: 每行同时循环右移一格,如上图可变换为41236785
C:
中间4个方块顺时针旋转一格,如上图可变换为17245368
给你魔板的初始状态与目标状态,请给出由初态到目态变换数最少的变换步骤,若有多种变换方案则取字典序最小的那种。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cstdlib>
#include<algorithm>
#include<queue>
using namespace std; string start,ans[];
int hash[]={};
bool vis[];
struct node
{
string c,step;
int val;
};
queue<node>Q; int ktzk(string s)
{
int i,j,sum = ;
for(i = ; i<; i++)
{
int cnt = ;
for(j = i+; j<; j++)
if(s[i]>s[j])
cnt++;
sum+=cnt*hash[-i];
}
return sum;
}
void bfs()
{
node cur,now;
char t[];
int i,k;
memset(vis,false,sizeof(vis));
cur.c=start;
cur.step="";
cur.val=ktzk(start);
ans[cur.val]="";
vis[cur.val]=true;
Q.push(cur);
while(!Q.empty())
{
cur=Q.front();
Q.pop();
now=cur;//A
for(i=;i<=;i++) swap(now.c[i],now.c[-i]); k=ktzk(now.c);
if(vis[k]==false)
{
vis[k]=true;
now.step+='A';
now.val=k;
ans[k]=now.step;
Q.push(now);
}
now=cur;//B
for(i=;i<;i++) t[i]=now.c[i];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[]; k=ktzk(now.c);
if(vis[k]==false)
{
vis[k]=true;
now.step+='B';
now.val=k;
ans[k]=now.step;
Q.push(now);
}
now=cur;
for(i=;i<;i++) t[i]=now.c[i];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
now.c[]=t[];
k=ktzk(now.c);
if(vis[k]==false)
{
vis[k]=true;
now.step+='C';
now.val=k;
ans[k]=now.step;
Q.push(now);
}
}
}
int main()
{
char a[],b[],cur[];
string hxl;
int i,j,k;
for(i=;i<=;i++)
hash[i]=hash[i-]*i;
start="";
bfs(); while(scanf("%s%s",a,b)>)
{
for(i=;i<;i++)
{
for(j=;j<;j++)
{
if(a[i]==b[j])
{
cur[j]=''+i;
}
}
}
cur[]='\0';
hxl="";
for(i=;i<;i++)
hxl+=cur[i];
k=ktzk(hxl);
cout<< ans[k] <<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+hdu 3567(预处理)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1430 思路:由于只是8种颜色,所以标号就无所谓了,对起始状态重新修改标号为 12345678,对目标状 ...
- 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 关系映射 + 打表 .
题意是中文的不解释.(http://acm.hdu.edu.cn/showproblem.php?pid=1430) 思路: 这个题目直接BFS会超时的(我一开始超时了) ,如果 ...
- hdu 1430 (BFS 康托展开 或 map )
第一眼看到这题就直接BFS爆搜,第一发爆了内存,傻逼了忘标记了,然后就改,咋标记呢. 然后想到用map函数,就8!个不同的排列,换成字符串用map标记.然后又交一发果断超时,伤心,最恨超时,还不如来个 ...
- hdu 1430 魔板 (BFS+预处理)
Problem - 1430 跟八数码相似的一题搜索题.做法可以是双向BFS或者预处理从"12345678"开始可以到达的所有状态,然后等价转换过去直接回溯路径即可. 代码如下: ...
- hdu.1430.魔板(bfs + 康托展开)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- [HDU 1430] 魔板
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...
- HDU - 1430 魔板 (bfs预处理 + 康托)
对于该题可以直接预处理初始状态[0, 1, 2, 3, 4, 5, 6, 7]所有可以到达的状态,保存到达的路径,直接打印答案即可. 关于此处的状态转换:假设有初始状态为2,3,4,5,0,6,7,1 ...
随机推荐
- 673. Number of Longest Increasing Subsequence
Given an unsorted array of integers, find the number of longest increasing subsequence. Example 1: I ...
- python del 方法的使用
在Python 的自带函数中 del 函数是一个非常特殊但是又非常使用的函数 my_list = [1,2,3] my_dict = {"name":"lowman&qu ...
- Union Find-547. Friend Circles
There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...
- 转载-浅谈Ddos攻击攻击与防御
EMail: jianxin#80sec.comSite: http://www.80sec.comDate: 2011-2-10From: http://www.80sec.com/ [ 目录 ]一 ...
- Zookeeper原理分析之存储结构ZkDatabase
ZKDatabase在内存中维护了zookeeper的sessions, datatree和commit logs集合. 当zookeeper server启动的时候会将txnlogs和snapsho ...
- 怎么在eclipse中访问webservice
在eclipse创建webservice的方法: 1.在Eclipse的菜单栏中,Window --> Preferences --> Web Service --> Axis2 P ...
- AFNetworking 报3840
工作中遇到前后台交互,前端解析不了后端返回的数据格式 ,原因在于没有标准统一的请求格式 这是个坑,但是还是有办法修复 错误提示: Error Domain=NSCocoaErrorDomain Cod ...
- 扩展中国剩余定理(扩展CRT)详解
今天在$xsy$上翻题翻到了一道扩展CRT的题,就顺便重温了下(扩展CRT模板也在里面) 中国剩余定理是用于求一个最小的$x$,满足$x\equiv c_i \pmod{m_i}$. 正常的$CRT$ ...
- if嵌套语句 shell脚本实例 测试是否闰年
在 if 语句里面,你可以使用另外一个 if 语句.只要你能逻辑管理 你就可以使用多层嵌套. 以下是一个测试闰年的例子: #!/bin/bash # This script will test if ...
- docker - 修改镜像/容器文件或者 "Docker root dir" 的在宿主机上的存储位置
背景 之前在使用docker的时候,由于启动container的时候用的是默认的mount(路径为 /var/lib/docker),这个目录对应的硬盘空间有限,只有200G左右.现在随着程序运行,有 ...