ccf 游戏

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
struct state{
	int x,y,step;
}; 
struct node{
	int a,b,date;
	node(){
		date=0;
		b=0;
		a=0;
	}
}v[101][101];
int n,m,t,book[101][101][201];//!!!! 
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
struct state temp,now;
int text(struct state s){
	if(s.x<0||s.y<0||s.x>=n||s.y>=m) return 0;
	if(book[s.x][s.y][s.step]) return 0;
	if((s.step>=v[s.x][s.y].a)&&(s.step<=v[s.x][s.y].b)){
		return 0;
	}
	return 1;
}
int bfs(){
	queue<struct state> q;
	book[0][0][0]=1;
	now.step=0;now.x=0;now.y=0;
	q.push(now);
	while(!q.empty()){
		now=q.front();
		q.pop();
		if((now.x==n-1)&&(now.y==m-1)){
			return now.step;
		}
		temp.step=now.step+1;
		for(int i=0;i<4;i++){
			temp.x=now.x+dx[i],temp.y=now.y+dy[i];
			if(text(temp)){
				q.push(temp);
				book[temp.x][temp.y][temp.step]=1;//剪枝的核心,标记每个点的状态,同一个时间不走同样的点 
			}
		} 
	}
	return 0;
}
int main(){
	int r,c,a,b;
	scanf("%d%d%d",&n,&m,&t);
	memset(book,0,sizeof(book));
	for(int i=0;i<t;i++){
		scanf("%d%d%d%d",&r,&c,&a,&b);
		v[r-1][c-1].a=a;v[r-1][c-1].b=b;
		v[r-1][c-1].date=1;	
	}
	cout<<bfs();
	return 0;
}

猜你喜欢

转载自blog.csdn.net/chan_yeol/article/details/52505798