题意:在
N∗M
的矩阵内只可以走六步,如果碰到4就可以再走六步,问能不能走出迷宫?(如果能走出,输出最少的秒数,如果不能走出,输出-1。)
思路:BFS呀,我开始蠢,写了DFS。建一个t数组,
tij
代表走到
(i,j)
还剩几步可以走,如果碰到4的话,设置一个时间,重新加入队列即可。
http://acm.hdu.edu.cn/showproblem.php?pid=1072
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define rep(i,a,b) for(int i = (a) ; i <= (b) ; i ++)
#define rrep(i,a,b) for(int i = (b) ; i >= (a) ; i --)
#define repE(p,u) for(Edge * p = G[u].first ; p ; p = p -> next)
#define cls(a,x) memset(a,x,sizeof(a))
#define eps 1e-8
using namespace std;
const int MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int MAXN = 1e5+5;
const int MAXE = 2e5+5;
struct Node {
int x,y,t;
int cost;
Node(){}
Node(int _x,int _y,int _t,int _cost){x = _x ; y = _y ; t = _t;cost = _cost;}
};
typedef long long LL;
typedef unsigned long long ULL;
int T,n,m,k;
queue<Node>q;
int fx[] = {0,1,-1,0,0};
int fy[] = {0,0,0,-1,1};
int sx,sy;
int Map[10][10];
int t[10][10];
int ok ;
void BFS() {
while(!q.empty()) q.pop();
q.push(Node(sx,sy,6,0));
t[sx][sy] = 6;
while(!q.empty()) {
Node tmp = q.front();
q.pop();
if(tmp.t == 0) continue;
if(Map[tmp.x][tmp.y] == 3) {
ok = 1;
printf("%d\n",tmp.cost);
break;
}
rep(i,1,4) {
int tmpx = tmp.x + fx[i];
int tmpy = tmp.y + fy[i];
int tmpt = tmp.t - 1;
if(tmpx >= 1 && tmpx <= n && tmpy >= 1 && tmpy <= m) {
if(t[tmpx][tmpy] < tmpt) {
t[tmpx][tmpy] = tmpt;
if(Map[tmpx][tmpy] == 1 || Map[tmpx][tmpy] == 3) {
q.push(Node(tmpx,tmpy,tmp.t-1,tmp.cost+1));
}
else if(Map[tmpx][tmpy] == 4) {
t[tmpx][tmpy] = 6;
q.push(Node(tmpx,tmpy,6,tmp.cost+1));
}
}
}
}
}
}
void input() {
scanf("%d %d",&n,&m);
rep(i,1,n) rep(j,1,m) {
scanf("%d",&Map[i][j]);
if(Map[i][j] == 2) {
sx = i ; sy = j;
}
}
}
void solve() {
cls(t,0);
ok = 0;
BFS();
if(!ok) puts("-1");
}
int main(void) {
scanf("%d",&T); while(T--) {
input();
solve();
}
return 0;
}