ccf认证游戏100分

47 篇文章 0 订阅
问题描述
试题编号: 201604-4
试题名称: 游戏
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  小明在玩一个电脑游戏,游戏在一个 n× m的方格图上进行,小明控制的角色开始的时候站在第一行第一列,目标是前往第 n行第 m列。
  方格图上有一些方格是始终安全的,有一些在一段时间是危险的,如果小明控制的角色到达一个方格的时候方格是危险的,则小明输掉了游戏,如果小明的角色到达了第 n行第 m列,则小明过关。第一行第一列和第 n行第 m列永远都是安全的。
  每个单位时间,小明的角色必须向上下左右四个方向相邻的方格中的一个移动一格。
  经过很多次尝试,小明掌握了方格图的安全和危险的规律:每一个方格出现危险的时间一定是连续的。并且,小明还掌握了每个方格在哪段时间是危险的。
  现在,小明想知道,自己最快经过几个时间单位可以达到第 n行第 m列过关。
输入格式
  输入的第一行包含三个整数 nmt,用一个空格分隔,表示方格图的行数 n、列数 m,以及方格图中有危险的方格数量。
  接下来 t行,每行4个整数 rcab,表示第 r行第 c列的方格在第 a个时刻到第 b个时刻之间是危险的,包括 ab。游戏开始时的时刻为0。输入数据保证 rc不同时为1,而且当 rnc不为 m。一个方格只有一段时间是危险的(或者说不会出现两行拥有相同的 rc)。
输出格式
  输出一个整数,表示小明最快经过几个时间单位可以过关。输入数据保证小明一定可以过关。
样例输入
3 3 3
2 1 1 1
1 3 2 10
2 2 2 10
样例输出
6
样例说明
  第2行第1列时刻1是危险的,因此第一步必须走到第1行第2列。
  第二步可以走到第1行第1列,第三步走到第2行第1列,后面经过第3行第1列、第3行第2列到达第3行第3列。
评测用例规模与约定
  前30%的评测用例满足:0 <  nm ≤ 10,0 ≤  t < 99。
  所有评测用例满足:0 <  nm ≤ 100,0 ≤  t < 9999,1 ≤  r ≤  n,1 ≤  c ≤  m,0 ≤  a ≤  b ≤ 100。
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {
		int maxsize=301;
		dire[] director=new dire[4];
		director[0]=new dire(1,0);
		director[1]=new dire(0, 1);
		director[2]=new dire(-1,0);
		director[3]=new dire(0,-1);
		Scanner scanner=new Scanner(System.in);
		int row=scanner.nextInt();
		int col=scanner.nextInt();
		int n=scanner.nextInt();
		int[][][] visited=new int[row+1][col+1][maxsize];
		for (int i = 0; i < n; i++) {
			int r=scanner.nextInt();
			int c=scanner.nextInt();
			int st=scanner.nextInt();
			int end=scanner.nextInt();
			for (int j = st; j <= end; j++) {
				visited[r][c][j]=1;
			}
		}
		int ans=bfs(row,col,director,visited);
		System.out.println(ans);
	}

	private static int bfs(int row, int col, dire[] director, int[][][] visited) {
		node start,front,v;
		Queue<node> queue=new LinkedList<node>();
		start=new node(1, 1, 0);
		queue.add(start);
		while (!queue.isEmpty()) {
			front=queue.peek();
			queue.remove();
			
			if (front.row==row && front.col==col) {
				return front.time;
			}
			
			for (int i = 0; i < director.length; i++) {
				int r=front.row+director[i].x;
				int c=front.col+director[i].y;
				int t=front.time+1;
				v=new node(r,c,t);
				
				if (v.row<1||v.row>row||v.col<1||v.col>col) {
					continue;
				}
				
				if (visited[v.row][v.col][v.time]==1) {
					continue;
				}
				
				visited[v.row][v.col][v.time]=1;
				queue.add(v);
			}
		}
		return 0;
	}

}
class dire{
	int x;
	int y;
	public dire(int a,int b) {
		x=a;
		y=b;
	}
}
class node{
	int row;
	int col;
	int time;
	public node(int a,int b,int c) {
		row=a;
		col=b;
		time=c;
	}
	public node() {
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值