UVALive 2520 Holedox Moving(BFS+状态压缩)
这个题目在比赛的时候我们是没有做出来的,但是听到他们说进制哈希的时候,感觉真的是挺高端的,于是赛后开始补题,本着我的习惯在看题解之前自己再试着写一遍,我当时存储状态的方法是string + map,我用string将蛇的各个位置都存下来,用map记录这个状态有没有出现过,当时是过了题目中给的样例,我就开始担心STL会不会超时,后来发现想多了,这个方法WA了,至于为什么,我还没明白,或许是状态的处理错误。总之我自己也觉得这个状态有点不靠谱。。于是开始换用题解的方式。发现所谓的进制哈希,其实就是状态压缩,我们的方向一共有4个,数组下标为0,1,2,3.二进制表示00,01,10,11.
发现每一个方向都占用了两位,蛇身的长度(不包含蛇头)最大是7,所以方向状态最多有(1<<14)个,就可以使用vis[x][y][state]表示蛇头的位置和相对位置的状态压缩数值,在这里需要注意几个问题,vis为了节省空间和时间使用bool型的,state存储状态的时候让靠近蛇头的位置存储在低位。这样做的原因就是容易得到蛇移动以后的下一个状态,让它左移两位就可以。
我自己也敲出了代码,但是出现了迷之bug,两个样例都没有过,所以我在别人的代码上加了注释(我的改对了估计就跟他的一模一样了),这是原博客地址,代码及注释如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int MAX_S = ( << ) + ;
const int MAX_N = + ;
const int INF = ( << );
struct State
{
int x, y, dis, s;
State(int x = , int y = , int dis = , int s = ) : x(x), y(y), dis(dis), s(s) {};
};
struct POS
{
int x,y;
};
POS pos[MAX_N];
int N, M, res, L;
int vis[MAX_N][MAX_N][MAX_S];
int fx[] = {-, , , };
int fy[] = {, , , -};
bool _map[MAX_N][MAX_N];
queue <State> Q;
int get_start()
{
int dir, dx, dy, s = ;
for(int i = L - ; i > ; i--)///记录i如何走到i+1,便于回溯
{
dx = pos[i].x - pos[i - ].x, dy = pos[i].y - pos[i - ].y;
if(dx == && dy == )
dir = ;
else if(dx == && dy == -)
dir = ;
else if(dx == - && dy == )
dir = ;
else if(dx == && dy == )
dir = ;
s = s << ;
s = s | dir;
}
return s;
}
int get_next_state(int i, int s)
{
int dir;
int k = ( << ((L - ) << )) - ;///让(l-1)*2都是1,更高位为0
int dx = , dy = ;
dx = dx - fx[i], dy = dy - fy[i];///这里不要忘记将方向取反
if(dx == && dy == )
dir = ;
else if(dx == && dy == -)
dir = ;
else if(dx == - && dy == )
dir = ;
else if(dx == && dy == )
dir = ;
s = s << ;
s = s | dir;
s = s & k; /// 去除高位部分
return s;
} bool judge_code(int x, int y, int pre_x, int pre_y, int s)///判断会不会咬到自己
{
int dir;
for(int i = ; i < L - ; i++)
{
dir = ;
dir = dir & s;
s = s >> ;
if(x == pre_x + fx[dir] && y == pre_y + fy[dir])
return false;
pre_x = pre_x + fx[dir], pre_y = pre_y + fy[dir];
}
return true;
} void BFS()
{
State a;
int dx, dy, s;
while(!Q.empty())
{
a = Q.front();
Q.pop();
for(int i = ; i < ; i++)
{
dx = a.x + fx[i], dy = a.y + fy[i];
s = get_next_state(i, a.s);
if(dx > && dy > && dx <= N && dy <= M && !vis[dx][dy][s] && !_map[dx][dy] && judge_code(dx, dy, a.x, a.y, a.s))
{
if(dx == && dy == )
{
res = a.dis + ;
return ;
}
vis[dx][dy][s] = ;
Q.push(State(dx, dy, a.dis + , s));
}
}
}
} int main()
{
int s = , _case = ;
State _start;
while(scanf("%d%d%d", &N, &M, &L), N + M + L)
{
res = INF;
memset(_map, , sizeof(_map));
memset(vis, , sizeof(vis));
for(int i = ; i < L; i++)
scanf("%d%d", &pos[i].x, &pos[i].y);
int K, u, v;
scanf("%d", &K);
for(int i = ; i < K; i++)
{
scanf("%d%d", &u, &v);
_map[u][v] = ;
}
if(pos[].x == && pos[].y == )
{
printf("Case %d: 0\n", ++_case);
continue;
}
s = get_start();
Q.push(State(pos[].x, pos[].y, , s));
vis[pos[].x][pos[].y][s] = ;
BFS();
if(res == INF)
printf("Case %d: -1\n", ++_case);
else
printf("Case %d: %d\n", ++_case, res);
while(!Q.empty())
Q.pop();
}
return ;
}
UVALive 2520 Holedox Moving(BFS+状态压缩)的更多相关文章
- ACM/ICPC 之 BFS+状态压缩(POJ1324(ZOJ1361))
求一条蛇到(1,1)的最短路长,题目不简单,状态较多,需要考虑状态压缩,ZOJ的数据似乎比POj弱一些 POJ1324(ZOJ1361)-Holedox Moving 题意:一条已知初始状态的蛇,求其 ...
- HDU1429+bfs+状态压缩
bfs+状态压缩思路:用2进制表示每个钥匙是否已经被找到.. /* bfs+状态压缩 思路:用2进制表示每个钥匙是否已经被找到. */ #include<algorithm> #inclu ...
- BFS+状态压缩 hdu-1885-Key Task
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1885 题目意思: 给一个矩阵,给一个起点多个终点,有些点有墙不能通过,有些点的位置有门,需要拿到相应 ...
- poj 1753 Flip Game(bfs状态压缩 或 dfs枚举)
Description Flip game squares. One side of each piece is white and the other one is black and each p ...
- BFS+状态压缩 HDU1429
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】
Maze Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others) Total Sub ...
- HDU 3247 Resource Archiver (AC自己主动机 + BFS + 状态压缩DP)
题目链接:Resource Archiver 解析:n个正常的串.m个病毒串,问包括全部正常串(可重叠)且不包括不论什么病毒串的字符串的最小长度为多少. AC自己主动机 + bfs + 状态压缩DP ...
- UVALive 3956 Key Task (bfs+状态压缩)
Key Task 题目链接: http://acm.hust.edu.cn/vjudge/contest/129733#problem/D Description The Czech Technica ...
- BFS+状态压缩DP+二分枚举+TSP
http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others) ...
随机推荐
- js便利关联数组 及数组定义方式 分类
"http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv=& ...
- ssh配置导致Ansible并发失败
Ansible并发失败原因, fork=100. 执行playbook时候没有并发 vim /usr/lib/python2.7/site-packages/ansible/runner/conne ...
- ios 显示其他app的购买页面
using UnityEngine; using System.Collections; using System.Runtime.InteropServices ; public class IOS ...
- 集线器(HUB),交换机,和路由器的区别
交换机与集线器的区别从大的方面来看可以分为以下三点: 1.从OSI体系结构来看,集线器属于OSI第一层物理层设备,而交换机属于OSI的第二层数据链路层设备.也就意味着集线器只是对数据的传输起到同步.放 ...
- 实战JAVA虚拟机 JVM故障诊断与性能优化 pdf
需要的小伙伴拿走,百度云盘:http://pan.baidu.com/s/1nvm6RHZ
- Redis链表相关操作命令
lists链表类型lists类型就是一个双向链表,通过push,pop操作.从链表的头部或者尾部添加删除元素,这样list即可以作为栈也可以作为队列 lpush key value 在链表key的头部 ...
- 导航栏底部黑线隐藏 UINavigationBar hidden Bottom Line
3种方法: 1.大杀器 ,iOS 10.2 最新系统亲测无问题( 添加导航栏分类) https://github.com/samwize/UINavigationBar-Addition/ 2.io ...
- [转]cmd-bat批处理命令延时方法
批处理延时启动的几个方法 方法一:ping 缺点:时间精度为1秒,不够精确 @echo off @ping 127.0.0.1 -n 6 >nul start gdh.txt 方法二:vbs s ...
- 下载PhpStorm并进行激活
1.首先登陆PhpStorm官网http://www.jetbrains.com/phpstorm/ 点击附图中的download now 按钮 2.第二步根据os x \wind\ linux进行下 ...
- openstack私有云布署实践【14.2 登录页dashboard-controller(办公网环境)】
这一小节基本配置相同,但留意以下紫色部份的配置,当初为了管理方便,我们让办公网openstack的dashboard的登录桥接了科兴的dashboard,由此统一dashboard界面的登录地址 ...