uva 1600 Patrol Robot (草地机器人)BFS

这道题一开始真的没思路,直接dfs,结果WA了;

看了题解才知道用bfs,这道题使我加深了对BFS的理解。

这道题就是在普通的BFS上加上了一个额外条件,障碍物,所以只需要记录经过每个点时所穿过的障碍物,比如,定义layer表示穿过障碍物的个数,u表示上一个位置,则如果当前位置是1,则layer = u.layer+1,否则layer为0。如果layer<=k,并且这个点的第layer层也没有访问过,则将这个点加入到队列中,详情看代码


TLE代码:

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

public class Main {

	static int m,n,k;
	static int maxn = 25;
	static int[][] G = new int[maxn+1][maxn+1];
	static int[][] vis = new int[maxn+1][maxn+1];
	static int[] dr = {-1,0,1,0};
	static int[] dc = {0,1,0,-1};
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		while (t--!= 0) {
			m = scan.nextInt();
			n = scan.nextInt();
			k = scan.nextInt();
			for(int i=0;i<=maxn;i++){
				for(int j=0;j<=maxn;j++){
					vis[i][j] = 0;
					G[i][j] = 0;
				}
			}
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					G[i][j] = scan.nextInt();
				}
			}
			System.out.println(bfs());
		}
	}
	
	public static int bfs() {
		Queue<Node> q = new LinkedList<>();
		q.add(new Node(0, 0, 1, 0));
		vis[0][0] = 1;
		while (!q.isEmpty()) {
			Node u = q.peek();
			q.poll();
			if (u.r == m - 1 && u.c == n - 1) {
				return u.d - 1;
			}
			for (int i = 0; i < 4; i++) {
				int r = u.r + dr[i];
				int c = u.c + dc[i];
				if (r >= 0 && r < m && c >= 0 && c < n) {
					if (G[r][c] == 0) {
						q.add(new Node(r, c, u.d + 1, 0));
					} else {
						if ((u.layer + 1) <= k) {
							q.add(new Node(r, c, u.d + 1, u.layer + 1));
						}
					}
				}
			}
		}
		return -1;
	}
	
	static class Node{
		int r,c,d,layer;
		public Node(int r,int c,int d,int layer){
			this.r = r;
			this.c = c;
			this.d = d;
			this.layer = layer;
		}
	}
}


AC代码:

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

public class Main {

	static int m,n,k;
	static int maxn = 25;
	static int[][] G = new int[maxn+1][maxn+1];
	static int[][][] vis = new int[maxn+1][maxn+1][maxn+1];
	static int[] dr = {-1,0,1,0};
	static int[] dc = {0,1,0,-1};
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int t = scan.nextInt();
		while (t--!= 0) {
			m = scan.nextInt();
			n = scan.nextInt();
			k = scan.nextInt();
			for(int i=0;i<=maxn;i++){
				for(int j=0;j<=maxn;j++){
					for(int k=0;k<=maxn;k++){
						vis[i][j][k] = 0;
					}
					G[i][j] = 0;
				}
			}
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					G[i][j] = scan.nextInt();
				}
			}
			System.out.println(bfs());
		}
	}
	
	public static int bfs() {
		Queue<Node> q = new LinkedList<>();
		q.add(new Node(0, 0, 1, 0));
		vis[0][0][0] = 1;
		while (!q.isEmpty()) {
			Node u = q.peek();
			q.poll();
			if (u.r == m - 1 && u.c == n - 1) {
				return u.d - 1;
			}
			for (int i = 0; i < 4; i++) {
				int r = u.r + dr[i];
				int c = u.c + dc[i];
				int layer = u.layer;
				if (r >= 0 && r < m && c >= 0 && c < n) {
					if (G[r][c] == 0) {
						layer=0;
					} else {
						layer++;
					}
					
					if(layer<=k&&vis[r][c][layer]==0){//加上vis[r][c][layer]的判断是为了避免超时,如果去掉,一定超时。
						q.add(new Node(r,c,u.d+1,layer));
					}
					vis[r][c][layer] = 1;
				}
			}
		}
		return -1;
	}
	
	static class Node{
		int r,c,d,layer;
		public Node(int r,int c,int d,int layer){
			this.r = r;
			this.c = c;
			this.d = d;
			this.layer = layer;
		}
	}
}


你可能感兴趣的:(uva 1600 Patrol Robot (草地机器人)BFS)