hdu 1253 胜利大逃亡
Problem DescriptionIgnatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.
魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.

13 3 4 200 1 1 10 0 1 10 1 1 11 1 1 11 0 0 10 1 1 10 0 0 00 1 1 00 1 1 0
11代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int maxn=100;bool vst[maxn][maxn]; // 访问标记int dir[4][2]={0,1,0,-1,1,0,-1,0}; // 方向向量struct State // BFS 队列中的状态数据结构{int x,y; // 坐标位置int Step_Counter; // 搜索步数统计器};State a[maxn];bool CheckState(State s) // 约束条件检验{if(!vst[s.x][s.y] && ...) // 满足条件return 1;else // 约束条件冲突return 0;}void bfs(State st){queue <State> q; // BFS 队列State now,next; // 定义2 个状态,当前和下一个st.Step_Counter=0; // 计数器清零q.push(st); // 入队vst[st.x][st.y]=1; // 访问标记while(!q.empty()){now=q.front(); // 取队首元素进行扩展if(now==G) // 出现目标态,此时为Step_Counter 的最小值,可以退出即可{...... // 做相关处理return;}for(int i=0;i<4;i++){next.x=now.x+dir[i][0]; // 按照规则生成下一个状态next.y=now.y+dir[i][1];next.Step_Counter=now.Step_Counter+1; // 计数器加1if(CheckState(next)) // 如果状态满足约束条件则入队{q.push(next);vst[next.x][next.y]=1; //访问标记}}q.pop(); // 队首元素出队}return;}int main(){......return 0;}