CCF 201604-4
当做一个三维处理(x,y,t)。即位置x,位置y,时间t。
然后BFS处理,找到最短路径。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#define MAXN 330
using namespace std;
int n,m,t;
bool vis[MAXN][MAXN][MAXN]={false};
bool map[MAXN][MAXN][MAXN]={true};
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
struct Node{
int x,y,h;
};
queue<Node> Q;
bool ifok(int x,int y){
if(x<=0 || y<=0 || x>n || y>m) return false;
return true;
}
int ans=0;
void BFS(int x,int y,int h){
Q.push(Node{x,y,h});
vis[x][y][h]=true;
while(!Q.empty()){
Node top=Q.front();
Q.pop();
if(top.x==n && top.y==m){
ans=top.h;
break;
}
for(int i=0;i<4;i++){
int tx,ty;
tx=top.x+dir[i][0];
ty=top.y+dir[i][1];
if(ifok(tx,ty) && map[tx][ty][top.h+1] && !vis[tx][ty][top.h+1]){
Q.push((Node{tx,ty,top.h+1}));
vis[tx][ty][top.h+1]=true;
}
}
}
}
int main(){
cin >> n >> m >> t;
for(int i=0;i<=n;i++)
for(int j=0;j<=m;j++)
for(int k=0;k<330;k++)
map[i][j][k]=true;
for(int i=0;i<t;i++){
int r,c,a,b;
cin >> r >> c >> a >> b;
for(int j=a;j<=b;j++){
map[r][c][j]=false;
}
}
BFS(1,1,0);
cout << ans;
}