Holedox Moving
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 17042   Accepted: 4065

Description

During winter, the most hungry and severe time, Holedox sleeps in its lair. When spring comes, Holedox wakes up, moves to the exit of its lair, comes out, and begins its new life.
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

The
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

For
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

In the above sample case, the head of Holedox can follows (4,1)->(5,1)->(5,2)->(5,3)->(4,3)->(4,2)->(4,1)->(3,1)->(2,1)->(1,1) to reach the square of exit with minimal number of step, which is nine.

Source

 
    bfs,难点在于标记数组的表示,如何表示当前蛇的状态,有个很巧妙的方法是利用位运算进行状压,只要知道蛇头的位置以及每一部分之间的关系(即up,down,left,right)我们就可以表示出蛇的状态,最长有7个身长(不包括头),用0,1,2,3表示四个方向每个数占两位最多14位所以内存可以接受。只要处理好状压的蛇身基本就能A了。注意不能走到石头处和当前蛇身处。跑了1700ms。

 #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 ;
}

最新文章

  1. J2SE核心开发实战
  2. 有关sublime text的插件安装
  3. static的本质
  4. json化 datatable
  5. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
  6. winpcap使用之捕获数据包
  7. JavaScript学习笔记(高级部分—01)
  8. a 标签
  9. [译]前端JS面试题汇总 Part 1(事件委托/this关键字/原型链/AMD与CommonJS/自执行函数)
  10. Ubuntu 下重启网络的方法
  11. git 三步走
  12. docker 13 dockerfile的保留字指令
  13. 以太网 ------ Auto-Negotiation(自动协商)
  14. Jenkins 配置 FindBugs,Checkstyle,PMD 实现代码的静态检查 (14)
  15. ActiveX 控件导入程序
  16. 404 Note Found 队-Beta1
  17. UVA.11806 Cheerleaders (组合数学 容斥原理 二进制枚举)
  18. ACM1009:FatMouse&#39; Trade
  19. 【转载】ARM MMU详解
  20. mysql replace 使用注意,update的时候 删除从表数据

热门文章

  1. 老铁,这年头不会点git真不行
  2. selenium 用autoIT上传下载文件
  3. Latex技巧:在图表序号中加入章节号(实现诸如&ldquo;图1.1.2&rdquo;这样的图表序号)
  4. mybatis-generator和TKmybatis的结合使用
  5. tensorflow 张量的阶、形状、数据类型及None在tensor中表示的意思。
  6. LeetCode-11-7
  7. 运行docker image 忘记添加端口号
  8. Python 模块续 configparser、shutil、XML、paramiko、系统命令、
  9. JQuery 评分系统
  10. 对象数组空指针异常说明——C#中使用对象数组必须分别为其开辟空间