这题数据大容易TLE

优化:预处理, 可以先枚举出5^3的状态然后判断合不合法,但是由于题目说了有很多墙壁,实际上没有那么多要转移的状态那么可以把底图抽出来,然后3个ghost在上面跑到时候就不必判断了,减少了两次无用的枚举。

减少代码的方法:1.结点没有3个时增加冗余点,2.把位置坐标二元组编号成一个数,这点在预处理时可以顺便完成(坐标范围0~15),3.把三个ghost的位置状态压缩成一个数字,方便push,但是注意重时不能直接用Hash掉的值来判断vis,因为Hash以后数字范围很大。

这题为什么不适合Astar。因为Astar的估计用Manhattan距离需要知道坐标,因此不适合把二元组hash成数字,而且不能用一个数来压缩状态了,因为要加上估价值,代码很长,容易写错。

学习点:

1.减少代码量的方法,减少分类,提取不同的问题的相同部分

2.建图的方法。

poj不支持#include<bits/stdc++.h>

//Rey
#include<bits/stdc++.h>
using namespace std;
const int maxw = ;
const int maxn = ;//14*14*3/4+2 149 int s[];
int t[];
int w,h,n;
char maze[maxw][maxw+]; int deg[maxn],G[maxn][]; inline bool conflict(int a1,int b1,int a2,int b2){
return a1 == a2 || (a1 == b2 && a2 == b1);
}
int vis1[maxn][maxn][maxn];
int vis2[maxn][maxn][maxn];
typedef vector<int> VINT;
VINT v1;
VINT v2;
VINT v3;
typedef VINT * PV; inline int Hash(int a,int b,int c) {return a<<|b<<|c;}
int dBfs()
{
v1.clear();v2.clear();v3.clear();
memset(vis1,-,sizeof(vis1));
memset(vis2,-,sizeof(vis2));
PV q1 = &v1,q2 = &v2,nxt = &v3;
int (*d1)[maxn][maxn] = vis1, (*d2)[maxn][maxn] = vis2;
d1[s[]][s[]][s[]] = ;
d2[t[]][t[]][t[]] = ;
q1->push_back(Hash(s[],s[],s[]));
q2->push_back(Hash(t[],t[],t[]));
while(q1->size()&&q2->size()){
if(q1->size()>q2->size()) swap(q1,q2),swap(d1,d2);
for(int ii = ,sz = q1->size(); ii < sz; ii++){
int u = (*q1)[ii];
int a = u>>, b = (u>>)&0xff, c = u&0xff;
for(int i = ; i < deg[a]; i++){
int a1 = G[a][i];
for(int j = ; j < deg[b]; j++){
int b1 = G[b][j];
if(conflict(a1,a,b1,b)) continue;
for(int k = ; k < deg[c]; k++){
int c1 = G[c][k];
if(conflict(c1,c,a1,a)||conflict(c1,c,b1,b)||~d1[a1][b1][c1]) continue;
d1[a1][b1][c1] = d1[a][b][c]+;
if(~d2[a1][b1][c1]) { return d1[a1][b1][c1]+d2[a1][b1][c1]; }
nxt->push_back(Hash(a1,b1,c1));
}
}
}
}
q1->clear();
swap(nxt,q1);
} return -;
} void init()
{
int cnt = ;
int id[maxw][maxw];
const int dx[] = {-, , , , };
const int dy[] = { , ,-, , };
int x[maxn];
int y[maxn]; for(int i = ; i < h; i++)
for(int j = ; j < w; j++) {
char ch = maze[i][j];
if(ch != '#'){
x[cnt] = i; y[cnt] = j; id[i][j] = cnt;
if('A'<= ch && ch <= 'C') {t[ch-'A'] = cnt;}
else if('a' <= ch && ch <= 'c') {s[ch-'a'] = cnt; }
cnt++;
}
} for(int i = ; i < cnt; i++){
deg[i] = ;
for(int k = ; k < ; k++) {
int nx = x[i]+dx[k], ny = y[i]+dy[k];
if(maze[nx][ny] != '#')
G[i][deg[i]++] = id[nx][ny];
}
}
if(n<=) {deg[cnt] = ; G[cnt][] = cnt; s[] = t[] = cnt++; }
if(n<=) {deg[cnt] = ; G[cnt][] = cnt; s[] = t[] = cnt++; }
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&w)&&w) {
scanf("%d%d\n",&h,&n);
for(int i = ; i < h; i++)
gets(maze[i]);//G[i-1]
init();
int ans = dBfs();
printf("%d\n",ans);
}
return ;
}

最新文章

  1. 文件缓存(配合JSON数组)
  2. IOS开发基础知识--碎片47
  3. Guava学习笔记:简化异常处理的Throwables类
  4. JS 判断图片尺寸大小,以便页面resize时,动态调整页面元素位置
  5. Tableview的更新和删除某一行
  6. 大型网站技术架构介绍--squid
  7. Vi编辑器下使用分屏显示【已自己验证所有】
  8. php基础知识(3)(文件加载include)
  9. 13.mariadb-rhce考试解题思路
  10. Servlet中Service方法
  11. linux共享文件samba安装与java读取外部文件夹方法
  12. 第二章:开始开发mod前你需要知道的一些事情
  13. google在线測试练习题1
  14. Nokia N9开启开发者模式
  15. Oralce 按分隔符把一列转成多行
  16. POJ-1003&amp;1004
  17. 一点解决版本冲突的应急思路、怎样在所有jar包文件中搜索冲突的方法?
  18. idea创建Maven多模块项目
  19. Spring Boot面试题
  20. 【一天一道LeetCode】#202. Happy Number

热门文章

  1. 性能压测,SQL查询异常
  2. 深度卷积网络-Inception系列
  3. unity coroutine
  4. vector详解
  5. 基于canvas绘图 缩放 做标记
  6. MySQL下载与安装配置
  7. 用IDEA写出第一个java web
  8. ESQL 查询数据报 参数类型“Edm.Decimal”和“Edm.Double”不兼容
  9. CC18:二叉树平衡检查
  10. SocLib的安装