CSP 2016_4_4 游戏

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {
	int n, m, t;
	Node [][]map;
	int [][]start;
	int [][]end;
	int[][][]flag;
	int [][]dir = {{1,0},{-1,0},{0,1},{0,-1}};
	Queue<Node> que;
	public static void main(String[] args) {
		new Main().run();
	}
	public void run(){
		Scanner in = new Scanner(System.in);
		que = new LinkedList<>();
		int n = in.nextInt();
		int m = in.nextInt();
		int t = in.nextInt();
		map = new Node[n+1][m+1];
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++ ){
				map[i][j] = new Node(i, j, 0);
			}
		}
		flag = new int[102][102][305];
		start = new int[n+1][m+1];
		end = new int[n+1][m+1];
		for(int i = 0; i < t; i++ ){
			int x = in.nextInt();
			int y = in.nextInt();
			int a = in.nextInt();
			int b = in.nextInt();
			start[x][y] = a;
			end[x][y] = b;
		}
		
		flag[1][1][0] = 1;
		que.offer(map[1][1]);
		while(!que.isEmpty()){
			Node node = que.poll();
			if(node.x == n && node.y == m){
				System.out.println(node.step);
				break;
			}
			for(int i = 0; i < 4; i++ ){
				int xx = node.x + dir[i][0];
				int yy = node.y + dir[i][1];
				int step = node.step + 1;
				if(step > 300)
					break;
//				if(xx == n && yy == m){
//					System.out.println(step);
//					tag = 0;
//					break;
//				}
//				if(tag == 0)
//					break;
				if(flag[xx][yy][step] == 0 && (xx>0&&yy>0&&xx<=n&&yy<=m)&&(step < start[xx][yy] || step > end[xx][yy])){
					flag[xx][yy][step] = 1;
					que.offer(new Node(xx, yy, step));
				}
			}
			
		}
		
		
		
	}

}
class Node{
	int x, y;
	int step;
	public Node(int x, int y, int step){
		this.x = x;
		this.y = y;
		this.step = step;
	}
	
}

flag三维数组 表示此位置此时刻不能同时被入队

此外:注释的部分不对 也不知道为啥。。。理论上一样的

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值