0% found this document useful (0 votes)
4 views2 pages

Question 1)

The document contains two Java code snippets. The first snippet implements a queue using a LinkedList to handle three types of operations: adding an element, removing the first element, and retrieving the first element. The second snippet implements a breadth-first search (BFS) algorithm to find the minimum number of moves required to reach a goal position in a grid, considering obstacles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Question 1)

The document contains two Java code snippets. The first snippet implements a queue using a LinkedList to handle three types of operations: adding an element, removing the first element, and retrieving the first element. The second snippet implements a breadth-first search (BFS) algorithm to find the minimum number of moves required to reach a goal position in a grid, considering obstacles.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question 1)

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
LinkedList<Integer> l = new LinkedList<>();
int q = in.nextInt();
while (q-- > 0) {
int type = in.nextInt();
if (type == 1) {
l.add(in.nextInt());
}
else if (type == 2) {
if (!l.isEmpty()) {
l.pollFirst();
}
}
else {
System.out.println(l.getFirst());
}
}
}
}

Question 2

In Method direct copy paste copy

public static int minimumMoves(List<String> grid, int startX, int startY, int
goalX, int goalY) {
int n = grid.size();
int m = grid.get(0).length();

// Directions for moving (Up, Down, Left, Right)


int[] dx = {-1, 1, 0, 0};
int[] dy = {0, 0, -1, 1};

// BFS queue, holding pairs of (x, y) and the number of moves


Queue<int[]> queue = new LinkedList<>();
queue.add(new int[]{startX, startY, 0}); // Starting point with 0 moves

// Visited matrix to mark cells we've already processed


boolean[][] visited = new boolean[n][m];
visited[startX][startY] = true;

while (!queue.isEmpty()) {
int[] current = queue.poll();
int x = current[0];
int y = current[1];
int moves = current[2];
// If we reach the goal, return the number of moves
if (x == goalX && y == goalY) {
return moves;
}

// Explore all four possible directions


for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];

// Check if the new position is within bounds and not blocked


if (nx >= 0 && ny >= 0 && nx < n && ny < m &&
grid.get(nx).charAt(ny) == '.' && !visited[nx][ny]) {
visited[nx][ny] = true;
queue.add(new int[]{nx, ny, moves + 1});
}
}
}

// If we exhaust the queue without finding the goal, return -1


(unreachable)
return -1;
}

You might also like