准备3/30号的阿里巴巴笔试编程题目,BFS相关问题以后会多关注,多刷题,后续更新在我的github笔记中,欢迎star和关注,分享优质学习资源,本人工作意向java后端,研究生二年级,科研方向为深度学习与无线通信结合的研究,欢迎交流。
地址:https://github.com/YoungofNUAA/CodingInterviewGuide
阿里巴巴笔试题目:
一个地图n*m,包含1个起点,1个终点,其他点包括可达点和不可达点。 每一次可以:上下左右移动,或使用1点能量从(i,j)瞬间移动到(n-1-i, m-1-j),最多可以使用5点能量。
package cn.nuaa.alibaba;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class test02 {
static int[] dx = {
1,-1,0,0};
static int[] dy = {
0,0,1,-1};
static int m;
static int n;
static int endX;
static int endY;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
m = scanner.nextInt();
char[][] map = new char[n][m];
Queue<Pair> queue = new LinkedList<>();
for(int i=0;i<n;i++){
map[i] = scanner.next().toCharArray();
for(int j=0;j<map[i].length;j++){
if(map[i][j]=='S'){
Pair pair = new Pair(i,j);
queue.add(pair);
}else if(map[i][j]=='E'){
endX = i;
endY = j;
}
}
}
//BFS
System.out.println(BFS(map,queue));
}
public static boolean check(int x,int y){
if(x<0 || y<0 || x>=n || y>=m){
return false;
}
return true;
}
public static int BFS(char[][] map,Queue<Pair>queue){
while (!queue.isEmpty()){
int size = queue.size();
while (size-- >0){
Pair top = queue.poll();
if(top.x==endX && top.y==endY){
return top.step;
}
for(int k=0;k<4;k++){
int curX = top.x+dx[k];
int curY = top.y+dy[k];
Pair nextPair = new Pair(curX,curY);
nextPair.step = top.step + 1;
nextPair.fly = top.fly;
if(check(curX,curY) && (map[curX][curY]=='.' || map[curX][curY]=='E')){
queue.add(nextPair);
map[curX][curY] = 'X';
}
}
int flyX = n-1-top.x;
int flyY = m-1-top.y;
if(check(flyX,flyY) && top.fly<5 && (map[flyX][flyY]=='.' || map[flyX][flyY]=='E')){
Pair pair = new Pair(flyX,flyY);
pair.step = top.step+1;
pair.fly = top.fly+1;
queue.add(pair);
map[flyX][flyY] = 'X';
}
}
}
return -1;
}
}
class Pair{
int x;
int y;
int step;
int fly;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
LeetCode相关BFS题目
总结:
1、一般需要定义一个内部类,代表地图每个点,将题目中给的属性加进去,基础属性为坐标x,y
2、BFS问题需要queue,DFS问题需要stack
3、一般需要定义上下左右坐标转移数组
4、需要定义坐标范围检查函数
class Solution {
private class Node{
int x;
int y;
public Node(int x,int y){
this.x = x;
this.y = y;
}
}
int m = 0;
int n = 0;
public void solve(char[][] board) {
if(board.length==0 || board==null){
return;
}
m = board.length;
n = board[0].length;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
boolean isEdge = (i==0 || i==m-1 ||j==0 ||j==n-1) ? true:false;
if(isEdge && board[i][j]=='O'){
bfs(board,i,j);
}
}
}
//与边界相连的O用#代替
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(board[i][j] == 'O'){
board[i][j] = 'X';
}
if(board[i][j] == '#'){
board[i][j] = 'O';
}
}
}
}
public boolean check(int x,int y){
if(x<0 || x>=m || y<0 || y>=n){
return false;
}
return true;
}
public void bfs(char[][] board,int i,int j){
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(i,j));
board[i][j] = '#';
//上下左右
int[] dx = {
-1,1,0,0};
int[] dy = {
0,0,-1,1};
while(!queue.isEmpty()){
Node curNode = queue.poll();
for(int k=0;k<4;k++){
int nextX = curNode.x + dx[k];
int nextY = curNode.y + dy[k];
if(check(nextX,nextY) && board[nextX][nextY]=='O'){
queue.add(new Node(nextX,nextY));
board[nextX][nextY] = '#';
}
}
}
}
}
地图最远海洋问题BFS
我们只要先把所有的陆地都入队,然后从各个陆地同时开始一层一层的向海洋扩散,那么最后扩散到的海洋就是最远的海洋!并且这个海洋肯定是被离他最近的陆地给扩散到的!
class Solution {
int m;
int n;
//定义内部类标记一些题目要求的属性
private class Node{
int x;
int y;
int far; //定义该点距离出发点大陆的距离
public Node(int x,int y){
this.x = x;
this.y = y;
}
}
public boolean check(int x,int y){
if(x<0 || x>=m || y<0 || y>=n){
return false;
}
return true;
}
public int maxDistance(int[][] grid) {
//首先遍历grid找出所有为1的元素
m = grid.length;
n = grid[0].length;
Queue<Node> queue = new LinkedList<>();
for(int i=0;i<m;i++){
for(int j = 0;j<n;j++){
if(grid[i][j]==1){
queue.add(new Node(i,j));
}
}
}
return BFS(queue,grid);
}
public int BFS(Queue<Node> queue,int[][] grid){
int[] dx = {
-1,1,0,0};
int[]dy = {
0,0,-1,1};
boolean hasOcean = false;
Node newNode = null;
while(!queue.isEmpty()){
Node curNode = queue.poll();
for(int k=0;k<4;k++){
int newX = curNode.x+dx[k];
int newY = curNode.y+dy[k];
if(check(newX,newY) && grid[newX][newY]==0){
newNode = new Node(newX,newY);
newNode.far = curNode.far+1; //一定是curNode.step
grid[newX][newY] = 1;
hasOcean = true;
queue.add(newNode);
}
}
}
if(!hasOcean){
return -1;
}
return newNode.far;
}
}