Holedox Moving
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 7
Problem 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).
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.
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
题目:给定网格,蛇,障碍,求蛇到达(1,1)的最小步数.
分析:BFS.注意状态空间的压缩:用蛇头坐标和其后每一个蛇身块相对于前一蛇身块的方向来唯一地确定状态. 码代码过程中关键是状态空间压缩带来的各种转换.
提交版:
#include<iostream>
#include<map>
#include<string>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 21
bool mark[maxn][maxn][4][4][4][4][4][4][4];
bool stone[maxn][maxn];
struct NODE
{
int p[10];
int step;
}Q[maxn*maxn*4*4*4*4*4*4*4],Try,u;
int head, tail;
int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };
int N, M, L, K;
int flag;
int JudgeDir(int x, int y, int px, int py)
{
if (x == px)
{
if (y == py - 1)
return 2;
else return 3;
}
else if (y == py)
{
if (x == px - 1)
return 0;
else return 1;
}
}
void Update(struct NODE &a,int dir)//
{
for (int i = 8; i >=3 ; i--)
{
if (i<=L)
a.p[i] = u.p[i - 1];
}
switch (dir)
{
case 0:dir = 1; break;
case 1:dir = 0; break;
case 2:dir = 3; break;
case 3:dir = 2; break;
}
a.p[2] = dir;
}
int is_visited(struct NODE &Try)//
{
return mark[Try.p[0]][Try.p[1]][Try.p[2]][Try.p[3]][Try.p[4]][Try.p[5]][Try.p[6]][Try.p[7]][Try.p[8]];
}
int trouble_body_1(struct NODE &Try)
{
int x = Try.p[0], y = Try.p[1];
for (int i = 2; i <= 8;i++)
if (i <= L)
{
if (Try.p[0] == x + dx[Try.p[i]] && Try.p[1] == y + dy[Try.p[i]])
return 1;
x = x + dx[Try.p[i]]; y = y + dy[Try.p[i]];
}
return 0;
}
int trouble_body_2(struct NODE &Try,struct NODE &u)
{
int x = u.p[0], y = u.p[1];
for (int i = 2; i <= 8; i++)
if (i <= L)
{
if (Try.p[0] == x + dx[u.p[i]] && Try.p[1] == y + dy[u.p[i]])
return 1;
x = x + dx[u.p[i]]; y = y + dy[u.p[i]];
}
return 0;
}
void BFS()
{
int i, j;
head = tail = 0;
Q[tail++] = Try;
if (Try.p[0] == 1 && Try.p[1] == 1)
{
flag = 1;
return;
}
mark[Try.p[0]][Try.p[1]][Try.p[2]][Try.p[3]][Try.p[4]][Try.p[5]][Try.p[6]][Try.p[7]][Try.p[8]] = true;
while (head < tail)
{
u = Q[head++];
for (i = 0; i < 4; i++)
{
Try.p[0] = u.p[0] + dx[i]; Try.p[1] = u.p[1] + dy[i]; Try.step = u.step + 1;
Update(Try,i);//
if (1 <= Try.p[0] && Try.p[0] <= N && 1 <= Try.p[1] && Try.p[1] <= M)
if (stone[Try.p[0]][Try.p[1]]==false)
//if (trouble_body_1(Try) == 0)
if (trouble_body_2(Try,u)==0)//
if (is_visited(Try) == 0)//
{
Q[tail++] = Try;
if (Try.p[0] == 1 && Try.p[1] == 1)
{
flag = 1;
return;
}
mark[Try.p[0]][Try.p[1]][Try.p[2]][Try.p[3]][Try.p[4]][Try.p[5]][Try.p[6]][Try.p[7]][Try.p[8]] = true;
}
}
}
}
int main()
{
freopen("f:\\input.txt", "r", stdin);
int i, j;
int cas=0;
while (scanf("%d%d%d", &N, &M, &L) != EOF&&!(N==0&&M==0&&L==0))
{
cas++;
for (i = 0; i <= 8; i++)
if (i > L)
Try.p[i] = 0;
scanf("%d%d", &Try.p[0], &Try.p[1]);
int px = Try.p[0], py = Try.p[1];
int x, y;
for (i = 2; i <= L; i++)
{
scanf("%d%d", &x, &y);
Try.p[i] = JudgeDir(x, y, px, py);//
px=x; py=y;
}
memset(stone, false, sizeof(stone));
scanf("%d", &K);
for (i = 0; i < K; i++)
{
scanf("%d%d", &x, &y);
stone[x][y] = true;
}
flag = 0;
memset(mark, false, sizeof(mark));
Try.step = 0;
BFS();
if (flag)
{
printf("Case %d: %d\n", cas, Q[tail - 1].step);
}
else
printf("Case %d: -1\n", cas);
}
return 0;
}
调试版:
#include<iostream>
#include<map>
#include<string>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 21
bool mark[maxn][maxn][4][4][4][4][4][4][4];
bool stone[maxn][maxn];
struct NODE
{
int p[10];
int step;
int pre;
int dir;
}Q[maxn*maxn * 4 * 4 * 4 * 4 * 4 * 4 * 4], Try, u;
int head, tail;
int dx[4] = { -1, 1, 0, 0 }, dy[4] = { 0, 0, -1, 1 };
char str[4][10] = { "上", "下", "左", "右" };
int N, M, L, K;
int flag;
int JudgeDir(int x, int y, int px, int py)
{
if (x == px)
{
if (y == py - 1)
return 2;
else return 3;
}
else if (y == py)
{
if (x == px - 1)
return 0;
else return 1;
}
}
void Update(struct NODE &a,struct NODE &u,int dir)//
{
for (int i = 8; i >= 3; i--)
{
if (i <= L)
a.p[i] = u.p[i - 1]; //(根据父亲来更新,最初写错了)
}
switch (dir) //p[]保存的是当前蛇身块相对前一蛇身块的方向
{ //故p[2]与蛇头行进的方向相反
case 0:dir = 1; break;
case 1:dir = 0; break;
case 2:dir = 3; break;
case 3:dir = 2; break;
}
a.p[2] = dir;
}
bool is_visited(struct NODE &Try)//
{
return mark[Try.p[0]][Try.p[1]][Try.p[2]][Try.p[3]][Try.p[4]][Try.p[5]][Try.p[6]][Try.p[7]][Try.p[8]];
}
int trouble_body_1(struct NODE &Try)
{
int x = Try.p[0], y = Try.p[1];
for (int i = 2; i <= 8; i++)
if (i <= L)
{
if (Try.p[0] == x + dx[Try.p[i]] && Try.p[1] == y + dy[Try.p[i]])
return 1;
x = x + dx[Try.p[i]]; y = y + dy[Try.p[i]];
}
return 0;
}
int trouble_body_2(struct NODE &Try, struct NODE &u)
{
int px = u.p[0], py = u.p[1];
for (int i = 2; i <= 8; i++)
if (i <= L)
{
if (Try.p[0] == px + dx[u.p[i]] && Try.p[1] == py + dy[u.p[i]])
return 1;
px = px + dx[u.p[i]]; py = py + dy[u.p[i]];
}
return 0;
}
void BFS()
{
int i, j;
head = tail = 0;
Q[tail++] = Try;
if (Try.p[0] == 1 && Try.p[1] == 1)
{
flag = 1;
return;
}
mark[Try.p[0]][Try.p[1]][Try.p[2]][Try.p[3]][Try.p[4]][Try.p[5]][Try.p[6]][Try.p[7]][Try.p[8]] = true;
while (head < tail)
{
u = Q[head++];
for (i = 0; i < 4; i++)
{
Try.p[0] = u.p[0] + dx[i]; Try.p[1] = u.p[1] + dy[i]; Try.step = u.step + 1; Try.pre = head - 1; Try.dir = i; //计算步数,记住父亲在队列中的下标,记住入边方向
Update(Try,u, i);//
if (1 <= Try.p[0] && Try.p[0] <= N && 1 <= Try.p[1] && Try.p[1] <= M)
if (stone[Try.p[0]][Try.p[1]] == false)
//if (trouble_body_1(Try) == 0) //现实版的关于贪吃蛇自己吃自己规则
if (trouble_body_2(Try, u) == 0) //该题的关于贪吃蛇自己吃自己规则
if (is_visited(Try) == 0)//
{
Q[tail++] = Try;
if (Try.p[0] == 1 && Try.p[1] == 1)
{
flag = 1;
return;
}
mark[Try.p[0]][Try.p[1]][Try.p[2]][Try.p[3]][Try.p[4]][Try.p[5]][Try.p[6]][Try.p[7]][Try.p[8]] = true;
}
}
}
}
void print_path() //输出路径
{
int ans[50];
int top = 0;
int pos = tail - 1;
while (Q[pos].pre != -1)
{
ans[top++] = Q[pos].dir;
pos = Q[pos].pre;
}
char str[4][10] = { "上", "下", "左", "右" };
for (top--; top >= 0; top--)
printf("%s ", str[ans[top]]);
printf("\n");
}
int main()
{
freopen("f:\\input.txt", "r", stdin);
int i, j;
int cas = 0;
while (scanf("%d%d%d", &N, &M, &L) != EOF&&!(N == 0 && M == 0 && L == 0))
{
cas++;
for (i = 0; i <= 8; i++)
if (i > L)
Try.p[i] = 0;
scanf("%d%d", &Try.p[0], &Try.p[1]); //(p[0],p[1])保存蛇头坐标
int px = Try.p[0], py = Try.p[1];
int x, y;
for (i = 2; i <= L; i++)
{
scanf("%d%d", &x, &y);
Try.p[i] = JudgeDir(x, y, px, py);
px = x; py = y;
}
memset(stone, false, sizeof(stone));
scanf("%d", &K);
for (i = 0; i < K; i++)
{
scanf("%d%d", &x, &y);
stone[x][y] = true;
}
flag = 0;
memset(mark, false, sizeof(mark));
Try.step = 0; Try.pre = -1;
BFS();
if (flag)
{
printf("Case %d: %d\n", cas, Q[tail - 1].step);
//print_path();
}
else
printf("Case %d: -1\n", cas);
}
return 0;
}