poj 1324 状态压缩+bfs
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 17042 | Accepted: 4065 |
Description
Holedox is a special snake, but its body is not very long. Its lair
is like a maze and can be imagined as a rectangle with n*m squares. Each
square is either a stone or a vacant place, and only vacant places
allow Holedox to move in. Using ordered pair of row and column number of
the lair, the square of exit located at (1,1).
Holedox's body, whose length is L, can be represented block by
block. And let B1(r1,c1) B2(r2,c2) .. BL(rL,cL) denote its L length
body, where Bi is adjacent to Bi+1 in the lair for 1 <= i <=L-1,
and B1 is its head, BL is its tail.
To move in the lair, Holedox chooses an adjacent vacant square of
its head, which is neither a stone nor occupied by its body. Then it
moves the head into the vacant square, and at the same time, each other
block of its body is moved into the square occupied by the corresponding
previous block.
For example, in the Figure 2, at the beginning the body of Holedox
can be represented as B1(4,1) B2(4,2) B3(3,2)B4(3,1). During the next
step, observing that B1'(5,1) is the only square that the head can be
moved into, Holedox moves its head into B1'(5,1), then moves B2 into B1,
B3 into B2, and B4 into B3. Thus after one step, the body of Holedox
locates in B1(5,1)B2(4,1)B3(4,2) B4(3,2) (see the Figure 3).
Given the map of the lair and the original location of each block of
Holedox's body, your task is to write a program to tell the minimal
number of steps that Holedox has to take to move its head to reach the
square of exit (1,1).
Input
input consists of several test cases. The first line of each case
contains three integers n, m (1<=n, m<=20) and L (2<=L<=8),
representing the number of rows in the lair, the number of columns in
the lair and the body length of Holedox, respectively. The next L lines
contain a pair of row and column number each, indicating the original
position of each block of Holedox's body, from B1(r1,c1) to BL(rL,cL)
orderly, where 1<=ri<=n, and 1<=ci<=m,1<=i<=L. The
next line contains an integer K, representing the number of squares of
stones in the lair. The following K lines contain a pair of row and
column number each, indicating the location of each square of stone.
Then a blank line follows to separate the cases.
The input is terminated by a line with three zeros.
Note: Bi is always adjacent to Bi+1 (1<=i<=L-1) and exit square (1,1) will never be a stone.
Output
each test case output one line containing the test case number followed
by the minimal number of steps Holedox has to take. "-1" means no
solution for that case.
Sample Input
5 6 4
4 1
4 2
3 2
3 1
3
2 3
3 3
3 4 4 4 4
2 3
1 3
1 4
2 4
4 2 1
2 2
3 4
4 2 0 0 0
Sample Output
Case 1: 9
Case 2: -1
Hint
Source
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
int N,M,L,K,all;
struct xy{int x,y;}P[];
struct node{int x,y,bs,has;};
bool vis[][][<<];
bool sto[][];
int fx[][]={-,,,,,-,,};
int ldx[]={,,,};
bool check(node t,int dx,int dy)
{
int x=t.x,y=t.y;
for(int i=;i<=L;++i)
{
int tt=;
if(t.has&) tt+=;t.has>>=;
if(t.has&) tt+=;t.has>>=;
x=x+fx[tt][];
y=y+fx[tt][];
if(x==dx&&y==dy) return ;
}
return ;
}
void bfs(node st)
{
memset(vis,,sizeof(vis));
queue<node>q;
q.push(st);
while(!q.empty()){
node t=q.front();q.pop();
if(vis[t.x][t.y][t.has]) continue;
vis[t.x][t.y][t.has]=;
if(t.x==&&t.y==){cout<<t.bs<<endl;return;}
for(int i=;i<;++i)
{
node _t=t;
int dx=_t.x+fx[i][];
int dy=_t.y+fx[i][];
if(dx<||dy<||dx>N||dy>M||sto[dx][dy]||!check(_t,dx,dy)) continue;
int has=(_t.has<<)&(all)|(ldx[i]);
_t.has=has;
_t.bs++;
_t.x=dx;
_t.y=dy;
if(vis[dx][dy][has]) continue;
q.push(_t);
}
}
puts("-1");
}
int main()
{
int i,j,k=;
while(cin>>N>>M>>L){
if(N==&&M==&&L==) break;
memset(sto,,sizeof(sto));
for(i=;i<=L;++i) scanf("%d%d",&P[i].x,&P[i].y);
scanf("%d",&K);
for(i=;i<=K;++i)
{
int o,p;
scanf("%d%d",&o,&p);
sto[o][p]=;
}
printf("Case %d: ",++k);
all=(<<((L-)*))-;
node st;
st.x=P[].x;
st.y=P[].y;
st.bs=;
st.has=;
for(i=;i<=L;++i)
{
for(j=;j<;++j)
{
int dx=P[i-].x+fx[j][];
int dy=P[i-].y+fx[j][];
if(dx==P[i].x&&dy==P[i].y){
st.has=st.has|(j<<((i-)*));
}
}
}
bfs(st);
}
return ;
}
poj 1324 状态压缩+bfs的更多相关文章
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ - 1324 Holedox Moving (状态压缩+BFS/A*)
题目链接 有一个n*m(1<=n,m<=20)的网格图,图中有k堵墙和有一条长度为L(L<=8)的蛇,蛇在移动的过程中不能碰到自己的身体.求蛇移动到点(1,1)所需的最小步数. 显然 ...
- POJ 1753 Flip Game (状态压缩 bfs+位运算)
Flip game is played on a rectangular 4x4 field with two-sided pieces placed on each of its 16 square ...
- POJ 3411 Paid Roads (状态压缩+BFS)
题意:有n座城市和m(1<=n,m<=10)条路.现在要从城市1到城市n.有些路是要收费的,从a城市到b城市,如果之前到过c城市,那么只要付P的钱, 如果没有去过就付R的钱.求的是最少要花 ...
- hdu 3681 Prison Break(状态压缩+bfs)
Problem Description Rompire . Now it’s time to escape, but Micheal# needs an optimal plan and he con ...
- 【HDU - 1429】胜利大逃亡(续) (高级搜索)【状态压缩+BFS】
Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了带锁的门,钥匙藏在地牢另外的某些地方.刚开 ...
- HDU 5025 Saving Tang Monk 【状态压缩BFS】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5025 Saving Tang Monk Time Limit: 2000/1000 MS (Java/O ...
- poj 3254(状态压缩+动态规划)
http://poj.org/problem?id=3254 题意:有一个n*m的农场(01矩阵),其中1表示种了草可以放牛,0表示没种草不能放牛,并且如果某个地方放了牛,它的上下左右四个方向都不能放 ...
- 「hdu 4845 」拯救大兵瑞恩 [CTSC 1999](状态压缩bfs & 分层图思想)
首先关于分层图思想详见2004的这个论文 https://wenku.baidu.com/view/dc57f205cc175527072208ad.html 这道题可以用状态压缩,我们对于每一把钥匙 ...
随机推荐
- 0x03 MySQl 库操作
一 系统数据库 information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息.列信息.权限信息.字符信息等performance_schema: MyS ...
- Kafka简介、安装
一.Kafka简介 Kafka是一个分布式.可分区的.可复制的消息系统.几个基本的消息系统术语:1.消费者(Consumer):从消息队列(Kafka)中请求消息的客户端应用程序.2.生产者(Prod ...
- Python中使用MySQL
@1: 安装MySQL: lxw ~$ sudo apt-get install mysql-server 可以通过 sudo netstat -tap | grep mysql` 命令检查之后,如果 ...
- yii2-lock-form 也许这就是你想要的,阻止表单多次提交
是不是被用户的行为所困扰? 一个表单用户点击提交按钮了N次,这也导致了数据提交了N次. 为了此受到了测试的欺辱,受到了老板的批评? 不用怕,它就是来拯救你的. 第一步:打开命令行,敲入 compose ...
- webpack基础配置
webpack运行规则: Webpack 会给每个模块分配一个唯一的id并通过这个id索引和访问模块.在页面启动时,会先执行入口文件中的代码,其它模块会在运行 require 的时候再执行. 运行时主 ...
- HttpServlet---getLastModified与缓存
在HttpServlet中重写service方法的代码如下: protected void service(HttpServletRequest req, HttpServletResponse re ...
- JFreeChart应用实例-折线图
http://www.tuicool.com/articles/Nr2Yna JFreeChart在制作折线图的时候可以使用两种不同的方式 package Line; import java.awt. ...
- ubuntu: lightdm 登录root超级管理员方法
ubuntu 12.04 lts 默认是不允许root登录的, 在登录窗口只能看到普通用户和访客登录. 以普通身份登陆Ubuntu后我们需要做一些修改,普通用户登录后, 修改系统配置文件需要切换到超级 ...
- 【Head First Servlets and JSP】笔记18:JSP指令
mark. jetbrain tomcat配置:https://www.jetbrains.com/help/idea/2017.1/creating-and-running-your-first-w ...
- 面向过程编程实例------grep-rl 'root 路径
#应用:grep -rl 'root' /etc import os def deco(func): def wrapper(*args): g=func(*args) next(g) return ...