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;
}