Name: Holedox Moving
P_ID: POJ 1324
题目链接:http://poj.org/problem?id=1324
题意描述:
n*m(最大20)的迷宫,l长(不超过8)的贪吃蛇,蛇身可弯曲,出口在(1,1),不能走石头所在格子,求蛇身最少移动次数。
题目分析:
题目本身是要考bfs最短路这个很明确,唯一不寻常的是在蛇移动过程中蛇身也在动。刚开始我认为这道题并没有什么特别的地方,于是采用了很普通的一种想法:
正常对头到(1,1)进行bfs,每次状态,我判断头周围的格子,当既不是石头,也不是蛇身的时候,说明这个格子是可以走的,我进行如下三个操作:
1. 蛇身移动一格(依次前移,蛇头新位置入队)
2. 将新的蛇头所在位置vis标记为1
3. 原蛇尾位置标记为空(代表可走)
下面是我的代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 3;
int n, m, l, b;
int num = 0;
char maze[25][25];
int vis[21][21];
const int dir[][2] = {
{
0,1}, {
0,-1}, {
1,0}, {-1,0}};
struct cell {
int x;
int y;
cell(int a, int b)
{
x = a;
y = b;
}
};
struct node {
int x;
int y;
int step;
vector<cell> myBody;
};
int flag = 0;
vector<cell> myBody;
bool check(int x, int y)
{
if(x<1 || x>n || y<1 || y>m) return false;
if(maze[x][y]==