UESTC 485 Game(康托展开,bfs打表)
Game
Time Limit: 4000/2000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
Submit Status
title
Today I want to introduce an interesting game to you. Like eight puzzle, it is a square board with 99 positions, but it filled by 99 numbered tiles. There is only one type of valid move, which is to rotate one row or column. That is, three tiles in a row or column are moved towards the head by one tile and the head tile is moved to the end of the row or column. So it has 1212 different moves just as the picture left. The objective in the game is to begin with an arbitrary configuration of tiles, and move them so as to get the numbered tiles arranged as the target configuration.
title
Now the question is to calculate the minimum steps required from the initial configuration to the final configuration. Note that the initial configuration is filled with a permutation of 11 to 99, but the final configuration is filled with numbers and * (which can be any number).
Input
The first line of input contains an integer TT (T≤1000T≤1000), which is the number of data sets that follow.
There are 66 lines in each data set. The first three lines give the initial configuration and the next three lines give the final configuration.
Output
For every test case, you should output Case #k: first, where kk indicates the case number and starts at 11. Then the fewest steps needed. If he can’t move to the end, just output No Solution! (without quotes).
Sample input and output
Sample Input Sample Output
2
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 9 8
1 2 3
4 5 6
7 8 9
8 * 9
5 3 7
2 * *
Case #1: No Solution!
Case #2: 7
利用康托展开进行bfs预处理。题目给的一个起始的九宫格,和一个目标的九宫格。 不能直接用目标的九宫格去找起始的九宫格,会超时,应该根据把起始九宫格当作
1 2 3
4 5 6
7 8 9
然后确定目标九宫格是怎么样的,这样就可以直接用之前打的表了。预处理就是处理1 2 3 4 5 6 7 8 9到每种九宫格的步数
关于康托展开,给出一篇博文吧
http://blog.csdn.net/dacc123/article/details/50952079
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue>
using namespace std;
struct Node
{
int a[5][5];
int sta;
};
queue<Node> q;
int b[10];
int fac[10];
int vis[400000];
int pre[400000];
int ans;
int f1[10];
int f2[10];
int tran[10];
char ch[10];
bool used[10];
Node cyk;
void facfun()
{
fac[0]=1;
for(int i=1;i<=9;i++)
{
fac[i]=i*fac[i-1];
}
}
int kt(Node q)
{
int cnt=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
b[++cnt]=q.a[i][j];
int sum=0,num=0;
for(int i=1;i<=9;i++)
{
num=0;
for(int j=i+1;j<=9;j++)
{
if(b[i]>b[j])
num++;
}
sum+=num*fac[9-i];
}
return sum;
}
void bfs(Node t)
{
q.push(t);
vis[t.sta]=1;
pre[t.sta]=0;
while(!q.empty())
{
Node term=q.front();
q.pop();
for(int i=1;i<=12;i++)
{
Node temp=term;
if(i<=3)
{
temp.a[i][1]=term.a[i][3];
temp.a[i][2]=term.a[i][1];
temp.a[i][3]=term.a[i][2];
}
else if(i>3&&i<=6)
{
temp.a[i-3][1]=term.a[i-3][2];
temp.a[i-3][2]=term.a[i-3][3];
temp.a[i-3][3]=term.a[i-3][1];
}
else if(i>6&&i<=9)
{
temp.a[1][i-6]=term.a[3][i-6];
temp.a[2][i-6]=term.a[1][i-6];
temp.a[3][i-6]=term.a[2][i-6];
}
else if(i>9&&i<=12)
{
temp.a[1][i-9]=term.a[2][i-9];
temp.a[2][i-9]=term.a[3][i-9];
temp.a[3][i-9]=term.a[1][i-9];
}
int state=kt(temp);
if(vis[state])
continue;
temp.sta=state;
vis[state]=1;
pre[state]=pre[term.sta]+1;
q.push(temp);
}
}
}
void init()
{
memset(vis,0,sizeof(vis));
memset(pre,-1,sizeof(pre));
facfun();
Node st;int cnt=0;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
st.a[i][j]=++cnt;
st.sta=0;
bfs(st);
}
int anspos;
void dfs(int i)
{
if(i==10)
{
/*for(int p=1;p<=3;p++)
{
for(int k=1;k<=3;k++)
{
cout<<cyk.a[p][k]<<" ";
}
cout<<endl;
}*/
int c=pre[kt(cyk)];
if(c==-1) return;
ans=min(ans,c);return;
}
if(f2[i]==0)
{
for(int j=1;j<=9;j++)
{
if(!used[j])
{
used[j]=true;
int y=i%3,x;
if(y==0){x=i/3;y=3;}
else {x=i/3+1;}
cyk.a[x][y]=j;
dfs(i+1);
used[j]=false;
}
}
}
else
{
int y=i%3,x;
if(y==0){x=i/3;y=3;}
else {x=i/3+1;}
cyk.a[x][y]=f2[i];
dfs(i+1);
}
}
int main()
{
int t;
scanf("%d",&t);
init();
int cas=0;
while(t--)
{
memset(used,0,sizeof(used));
for(int i=1;i<=9;i++)
{
scanf("%d",&f1[i]);
tran[f1[i]]=i;
}
for(int i=1;i<=9;i++)
{
scanf("%s",ch);
f2[i]=ch[0]-'0';
if(f2[i]>=1&&f2[i]<=9)
f2[i]=tran[f2[i]],used[f2[i]]=true;
else
f2[i]=0;
}
ans=1000000;
dfs(1);
if(ans>=1000000)
printf("Case #%d: No Solution!\n",++cas);
else
printf("Case #%d: %d\n",++cas,ans);
}
return 0;
}
UESTC 485 Game(康托展开,bfs打表)的更多相关文章
- [HDOJ1043]Eight(康托展开 BFS 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 八数码问题,因为固定了位置所以以目标位置开始搜索,把所有情况(相当于一个排列)都记录下来,用康托 ...
- HDU 3567 Eight II 打表,康托展开,bfs,g++提交可过c++不可过 难度:3
http://acm.hdu.edu.cn/showproblem.php?pid=3567 相比Eight,似乎只是把目标状态由确定的改成不确定的,但是康托展开+曼哈顿为h值的A*和IDA*都不过, ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS+预处理)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1043 & POJ 1077 Eight(康托展开+BFS | IDA*)
Eight Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30176 Accepted: 13119 Special ...
- HDU 1430 魔板(康托展开+BFS+预处理)
魔板 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- hdu1043Eight (经典的八数码)(康托展开+BFS)
建议先学会用康托展开:http://blog.csdn.net/u010372095/article/details/9904497 Problem Description The 15-puzzle ...
- poj1077(康托展开+bfs+记忆路径)
题意:就是说,给出一个三行三列的数组,其中元素为1--8和x,例如: 1 2 3 现在,需要你把它变成:1 2 3 要的最少步数的移动方案.可以右移r,左移l,上移u,下移dx 4 6 4 5 67 ...
- HDU1043 八数码(BFS + 打表)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1043 , 康托展开 + BFS + 打表. 经典八数码问题,传说此题不做人生不完整,关于八数码的八境界 ...
- 转换地图 (康托展开+预处理+BFS)
Problem Description 在小白成功的通过了第一轮面试后,他来到了第二轮面试.面试的题目有点难度了,为了考核你的思维能量,面试官给你一副(2x4)的初态地图,然后在给你一副(2x4)的终 ...
随机推荐
- (转)YUV420存储格式
YUV格式有两大类:planar和packed.对于planar的YUV格式,先连续存储所有像素点的Y,紧接着存储所有像素点的U,随后是所有像素点的V.对于packed的YUV格式,每个像素点的Y,U ...
- (转)Invalidate、RedrawWindow与UpdateWindow的区别
一:什么时候才会发生重绘窗口的消息? 当需要更新或重新绘制窗口的外观时,应用程序就会发送WM_PAINT消息.对窗口进行重新绘制. 二:Invalidate() -- RedrawWindow() ...
- thinkphp 解析带html标签的内容
1.实例一 <?php echo htmlspecialchars_decode($goodsinfo['Specification']);?> 2.实例二 {$show.article| ...
- 清理SYSAUX表空间的WRH$_LATCH_CHILDREN表
周六 被突然起来的短信 轰醒. 一看有63条短信. 都是来之与监控中的.有关表空间大小超过某个警戒值. 发现 SYSAUX表空间超过了15GB 通过以下代码查看SYSAUX表空间的功能占用情况 SEL ...
- this总结
this总结,mark一下: Object中的this: Object方法中的this,指向的就是该对象,即谁调用this就指向谁,与C#等服务器语言的思想比较一致. let demo = { nam ...
- 父div高度不能自适应子div高度的解决方案
<div id="parent"><div id="content"> </div></div> 当conten ...
- SVN版控系统的安装和使用
SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subversion ...
- 棒谷科技java岗笔试题与初试题
选择题 1.银行家算法的作用 2.docker镜像结构 3.docker的默认网络模式 4.spring的动态代理效率比较 5.springboot默认的json 6.springboot多profi ...
- Redis(四)-- 集群
一.Redis适合做企业级分布式缓存集群的条件 1.Redis内置哈希槽,有16384个哈希槽(0~16383),根据CRC16算法来确定这个集群中属于哪一个服务器来处理这个请求. 2.Redis提供 ...
- docker images 详解
docker images 用于查看本地已下载的镜像 [root@localhost ~]$ docker images REPOSITORY TAG IMAGE ID CREATED SIZE ce ...