题目传送门

解题思路:

一道bfs,本题最难的一点就是如何储存已经被访问过的状态,如果直接开一个bool数组,空间肯定会炸,所以我们要用另一个数据结构存,STL大法好,用map来存,直接AC.

AC代码:

 #include<cstdio>
#include<iostream>
#include<map>
#include<queue> using namespace std; int a[][],n;
int ans = ;
const int dx[]={-,,,},dy[]={,-,,};
map<int,int> m; inline void bfs() {
queue<long long> q;
q.push(n);
m[n] = ;
while(!q.empty()) {
int u = q.front();
q.pop();
int x = ,y= ,n = u;
if(u == ans) break;
for(int i = ;i >= ; i--)
for(int j = ;j >= ; j--) {
a[i][j] = n % ;
n /= ;
if(!a[i][j]) x = i,y = j;
}
for(int i = ;i < ; i++) {
int nx = x + dx[i],ny = y + dy[i],ns = ;
if(nx < || ny < || nx > || ny > ) continue;
swap(a[nx][ny],a[x][y]);
for(int i = ;i <= ; i++)
for(int j = ;j <= ; j++)
ns = ns * + a[i][j];
if(!m.count(ns)) {
m[ns] = m[u] + ;
q.push(ns);
}
swap(a[nx][ny],a[x][y]);
}
}
} int main()
{
scanf("%d",&n);
bfs();
printf("%d",m[]);
return ;
}

普通bfs

emmm,虽然普通bfs能A,但还是感觉太慢,所以试了一下双向bfs.

 #include<cstdio>
#include<iostream>
#include<queue>
#include<map> using namespace std; int a[][],n,n1 = ;
int x,y;
int wayx[] = {,-,,},wayy[] = {,,,-};
map<int,int> p,sum; inline void juzhen(int o) {
for(int i = ;i >= ; i--)
for(int j = ;j >= ; j--) {
a[i][j] = o % ;
o /= ;
if(a[i][j] == ) x = i,y = j;
}
} inline void double_bfs() {
if(n1 == ) return ;
queue<int> step[];
step[].push(n);
step[].push(n1);
sum[n] = ;
sum[n1] = ;
p[n] = ;p[n1] = ;
int deep = ,s;
while(!step[].empty() || !step[].empty()) {
int i = ;
while(i < ) {
if(sum[step[i].front()] != deep) {
i++;
continue;
}
s = step[i].front();
step[i].pop();
juzhen(s);
for(int w = ;w < ; w++) {
int xx = x + wayx[w];
int yy = y + wayy[w];
if(xx < || xx > || yy < || yy > ) continue;
swap(a[x][y],a[xx][yy]);
int pp = ;
for(int j = ;j <= ; j++)
for(int u = ;u <= ; u++)
pp = pp * + a[j][u];
if(p.count(pp)) {
if(p[pp] == - i) {
if(p[pp] == )
printf("%d",(deep + ) * );
else
printf("%d",deep * + );
return ;
}
}
else {
step[i].push(pp);
sum[pp] = sum[s] + ;
p[pp] = i;
}
swap(a[x][y],a[xx][yy]);
}
}
deep++;
}
} inline void tepan() {
if(n == n1) printf("");
if(n == n1) n = n1 = ;
} int main()
{
scanf("%d",&n);
tepan();
double_bfs();
return ;
}

双向bfs

双向bfs确实快很多很多,普通bfs耗时7.38s,而双向bfs只耗时291ms,快了25倍!!!!!!

最新文章

  1. SQl SGA 整理
  2. ahjesus在asp.net中还可以通过设置HttpCookie对象的过期时间为DateTime.MinValue来指定此Cookies为跟随浏览器生效
  3. postgresql利用pg_upgrade升级数据库(从8.4升级到9.5)
  4. PHP图像裁剪为任意大小的图像,图像不变形,不留下空白
  5. ASP.NET MVC 过滤器详解
  6. IntelliJ IDEA Community Edition 14.1.4下 javafx scenebuilder的使用
  7. OO之策略模式
  8. K-means聚类
  9. Sublime Text 2 - There are no packages available for installation
  10. POJ1050(dp)
  11. unity3D插件开发——前篇
  12. ASP.NET Core API 版本控制
  13. sql server replace的替换字符,replace的使用
  14. Linux 安装多个版本JDK并设置默认版本
  15. 三类设计模式UML图
  16. 读取CSV到DataTable
  17. Python生成语音
  18. VMware部署ubuntu后开机提示piix4_smbus: Host SMBus controller not enabled!
  19. wordpress必装的插件 wp最常用的十个插件
  20. CSS动画实现菜单栏从左边滑出

热门文章

  1. Linux每日练习-批量删除用户,非脚本运行方式 20200225
  2. UVA - 1608 Non-boring sequences (分治)
  3. 微服务框架中springboot启动的一个问题
  4. xargs详细
  5. 云时代架构阅读笔记五——Java内存模型详解(一)
  6. 小程序Promise
  7. JavaScript的函数和对象介绍
  8. UVA - 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)
  9. POJ1338 &amp; POJ2545 &amp; POJ2591 &amp; POJ2247
  10. mailx发送邮件