CCF-CSP 20160404:游戏

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define N 101
#define M 101
#define T 5000    //10001内存超出,评测范围T为9999,没想明白5000的维度竟然满分了 
int map[M][N][T];
struct node{
	int x,y,time;
};

struct Pos{
	int x,y;
}pos[4]={{-1,0},{1,0},{0,-1},{0,1}};

int main(){
	int n,m,t;
	memset(map,0,sizeof(map));
	cin>>n>>m>>t;
	for(int i=1;i<=t;i++){
		int r,c,a,b;
		cin>>r>>c>>a>>b;
		for(int j=a;j<=b;j++){
			map[r][c][j]=1;
		}
	}
	
	//BFS queue
	node tmp;
	queue<node> q;
	q.push({1,1,0});
	map[1][1][0]=1;
	while(!q.empty()){
		node p = q.front();
		q.pop();
		
		if(p.x==n&&p.y==m){
			cout<<p.time; 
			break; 
		}  
		for(int i=0;i<4;i++){
			tmp.x = p.x + pos[i].x;
			tmp.y = p.y + pos[i].y;
			tmp.time = p.time + 1;
			if(tmp.x<1||tmp.x>n||tmp.y<1||tmp.y>m) continue;
			if(map[tmp.x][tmp.y][tmp.time]==1) continue;   //有危险 
			if(map[tmp.x][tmp.y][tmp.time]==2) continue;   //走过了
			map[tmp.x][tmp.y][tmp.time] = 2;              //标志走过了
			q.push(tmp);
		}
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>