算法题(四十):BFS解决网易2017年笔试题——地牢逃脱

题目描述

给定一个 n 行 m 列的地牢,其中 '.' 表示可以通行的位置,'X' 表示不可通行的障碍,牛牛从 (x0 , y0 ) 位置出发,遍历这个地牢,和一般的游戏所不同的是,他每一步只能按照一些指定的步长遍历地牢,要求每一步都不可以超过地牢的边界,也不能到达障碍上。地牢的出口可能在任意某个可以通行的位置上。牛牛想知道最坏情况下,他需要多少步才可以离开这个地牢。

输入描述:

每个输入包含 1 个测试用例。每个测试用例的第一行包含两个整数 n 和 m(1 <= n, m <= 50),表示地牢的长和宽。接下来的 n 行,每行 m 个字符,描述地牢,地牢将至少包含两个 '.'。接下来的一行,包含两个整数 x0, y0,表示牛牛的出发位置(0 <= x0 < n, 0 <= y0 < m,左上角的坐标为 (0, 0),出发位置一定是 '.')。之后的一行包含一个整数 k(0 < k <= 50)表示牛牛合法的步长数,接下来的 k 行,每行两个整数 dx, dy 表示每次可选择移动的行和列步长(-50 <= dx, dy <= 50)

输出描述:

输出一行一个数字表示最坏情况下需要多少次移动可以离开地牢,如果永远无法离开,输出 -1。以下测试用例中,牛牛可以上下左右移动,在所有可通行的位置.上,地牢出口如果被设置在右下角,牛牛想离开需要移动的次数最多,为3次。

示例1

输入

3 3
...
...
...
0 1
4
1 0
0 1
-1 0
0 -1

输出

3

分析

题目让人很不解,直到看到牛客网的讨论才明白题意,即从起点到任意一点的最长距离。如果存在有点不能够到达则返回-1。

明白题意就简单多了,直接用广度优先遍历来做,最后判断是否存在访问不到的点。

代码一

注:我写的代码线下运行出正确答案,但放到牛客网运行,case通过率0%。

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


public class DungonEscape {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int xLength = 3;
		int yLength = 3;
		//记录起点到每个点的距离(初始为-1)
		int[][] res = new int[xLength][yLength];
		boolean[][] flag = new boolean[xLength][yLength];
		String[][] arr = {{"X",".","."},
				          {".","X","."},
				          {".",".","."}};
		for(int i=0; i queue = new LinkedList<>();
		queue.add(new Step(x0, y0));
		while(!queue.isEmpty()){
			Step s = queue.poll();
			int x = s.x;
			int y = s.y;
			//在一个结点上的遍历
			for(int i=0; imax){
					max = res[i][j];
				}
			}
		}

		return max;
	}
	
	public static boolean go(int x, int y, int deltaX, int deltaY, int xLimit, int yLimit){
		if(x+deltaX>=0 && x+deltaX=0){
			return true;
		}
		return false;
	}

}

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

代码二

牛客网通过率100%,算法思想是一样的。

import java.util.*;
 
public class Main {
    public static void main(String[] args){
          Scanner in = new Scanner(System.in);
             
            while (in.hasNext()) {//注意while处理多个case
                  int x=in.nextInt();
                  int y=in.nextInt();
                   
                  char[][] points=new char[x][y];
                  int[][] tar=new int[x][y];
                  for(int i=0;i xqueue=new LinkedList();
                  Queue yqueue=new LinkedList();  
                  //引入队列是为了遍历到最后不能走为止
                   
                  xqueue.add(startx);
                  yqueue.add(starty);
                   
                  tar[startx][starty]=1;  //起始点访问标记;1表示已经访问
                  while(!xqueue.isEmpty()&&!yqueue.isEmpty()){
                      startx=xqueue.remove();    //取队首
                      starty=yqueue.remove();
                       for(int i=0;i=0&&starty+stepy[i]=0)   //不出界
                          if(tar[startx+stepx[i]][starty+stepy[i]]==0){
                              if(points[startx+stepx[i]][starty+stepy[i]]=='.'){
                                  tar[startx+stepx[i]][starty+stepy[i]]=tar[startx][starty]+1;
                                  xqueue.add(startx+stepx[i]);
                                  yqueue.add(starty+stepy[i]);
                              }
                              else
                                  tar[startx+stepx[i]][starty+stepy[i]]=-1;  //访问点为X
                          }
                      }
                  }
                  int max=0;
                  int getRoad=1;  
                  for(int i=0;i

 

你可能感兴趣的:(算法,编程练手)