题目大意:一条蛇从初始位置到蛇头移动到(1,1)点所需要的最短时间(单位时间移动一格);(蛇的移动方式参见贪吃蛇。。。。)
分析:最短时间毫无疑问使用BFS,难点就在于记录蛇的形状,由于蛇的长度比较短只有8,我们可以先记录蛇头的位置,再者使用两位二进制表示舍得形状;(首先两位二进制有4中情况正好可以用来表示上下左右四个方向,这里的方向表示蛇的某一节在上一节的那个方向,只有1 << 14那么多的状态可以开三维数组记录状态)分析到这里剩下的部分就是代码能力来解决了;
以下是AC代码:
/*
Problem: 1324 User: Burglar
Memory: 41360K Time: 2266MS
Language: G++ Result: Accepted
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <queue>
#define MAXN 25
using namespace std;
struct Snake//用结构体记录蛇
{
int shape;//形状
int head_x, head_y;//蛇头的位置;
int step;//走到这个状态所需要的时间
};
int rock[MAXN][MAXN];//石头
int move_step[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};//0、1、2、3分别表示上下左右
int judge[MAXN][MAXN][1 << 14];//状态标记
int n, m, l, k;//输入变量
int mask;//主要用于在蛇的形状发生改变时,去除位移动带来多出来的位
Snake init_snake;//蛇的初始状态
void input_snack()//蛇的输入
{
int x, y;
int last_x, last_y;
scanf("%d%d",&init_snake.head_x, &init_snake.head_y);
init_snake.step = 0;
init_snake.shape = 0;
last_x = init_snake.head_x;
last_y = init_snake.head_y;
for(int i = 0; i < l - 1; i++)//由于蛇头已经输入所以是l - 1
{
scanf("%d%d",&x, &y);
if(last_x == x)
{
if(last_y - 1 == y )
{
init_snake.shape = init_snake.shape | ( 2 << (i << 1));//左
}
else if(last_y + 1 == y)
{
init_snake.shape = init_snake.shape | ( 3 << (i << 1));//右
}
}
else if(last_y == y)
{
if(last_x - 1 == x)
{
init_snake.shape = init_snake.shape | ( 0 << (i << 1));//上
}
else if(last_x + 1 == x)
{
init_snake.shape = init_snake.shape | ( 1 << (i << 1));//下
}
}
last_x = x;
last_y = y;
}
}
void input_rock()//输入石头;
{
int x, y;
memset(rock, 0, sizeof(rock));
scanf("%d",&k);
for(int i = 0; i < k; i++)
{
scanf("%d%d",&x, &y);
rock[x][y] = 1;
}
}
bool is_snake(int x, int y, Snake now)//判断(x, y)这一点在不在这条蛇的身上
{
int temp_x = now.head_x;
int temp_y = now.head_y;
int temp_shape = now.shape;
for(int i = 0; i < l - 1; i++)
{
int dir=( now.shape >> (i << 1) ) & 3;
int nx = temp_x + move_step[dir][0];
int ny = temp_y + move_step[dir][1];
if(x == nx && y == ny) return true;
temp_x = nx;
temp_y = ny;
//printf("====%d %d\n",nx, ny);
}
return false;
}
bool can_move(int x, int y, Snake now)//表示从now这条蛇可不可以把蛇头移动到点(x, y)
{
if(1 <= x && x <= n && 1 <= y && y <= m && !rock[x][y] && !is_snake(x, y, now)) return true;
else
return false;
}
int change_snake(Snake now, int dir)//now这条蛇的蛇头朝dir这个方向移动一格单位;
{
int res_shape = now.shape;
res_shape = res_shape << 2;//除了蛇头后面的节点需要重新判断意外 其他的节点都可以不需要变化
res_shape = res_shape & mask;//去除最左边的两位;
if(dir == 0)
{
dir = 1;
}
else if(dir == 1)
{
dir = 0;
}
else if(dir == 2)
{
dir = 3;
}
else if(dir == 3)
dir = 2;
res_shape = res_shape | dir;//补全最右边的两位;
return res_shape;//返回蛇的形状;
}
int BFS()//这个BFS比较模板化没什么好说的,理解了上面的几个函数这个BFS就不难看懂
{
memset(judge, 0, sizeof(judge));
queue<Snake> Q;
while(!Q.empty()) Q.pop();
Q.push(init_snake);
judge[init_snake.head_x][init_snake.head_y][init_snake.shape] = 1;
while(!Q.empty())
{
Snake now, next;
now = Q.front();
Q.pop();
if(now.head_x == 1 && now.head_y == 1) return now.step;
for(int i = 0; i < 4; i++)
{
next.head_x = now.head_x + move_step[i][0];
next.head_y = now.head_y + move_step[i][1];
if(!can_move(next.head_x, next.head_y, now)) continue;
//printf("------%d %d\n",next.head_x, next.head_y);
next.shape = change_snake(now, i);
if(!judge[next.head_x][next.head_y][next.shape])
{
judge[next.head_x][next.head_y][next.shape] = 1;
next.step = now.step + 1;
Q.push(next);
}
}
}
return -1;
}
int main()
{
int num_case = 1;
while(scanf("%d%d%d",&n, &m, &l) != EOF)
{
if(n == 0 && m == 0 && l == 0) break;
input_snack();
input_rock();
mask = (1 << ( (l - 1) << 1) ) - 1;//功能在上面已经叙述过了,这里应该不难理解;
printf("Case %d: %d\n",num_case++, BFS());
}
return 0;
}
时间差不多两秒多,有大神可以写到200MS以内的,据说是使用了A*这种高端搜索。。。我还不会过几天脑补一下。。