0% found this document useful (0 votes)
2 views7 pages

Java Grid Game and String Factor Solutions

Uploaded by

kutty.n0510
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Java Grid Game and String Factor Solutions

Uploaded by

kutty.n0510
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROBLEM 1

import [Link].*;

public class GridGame {

static class Node {

int x, y, steps;

Node(int x, int y, int steps) {

this.x = x;

this.y = y;

[Link] = steps;

static int[] rotate(int dx, int dy, String dir) {

switch (dir) {

case "forward": return new int[]{dx, dy};

case "right": return new int[]{dy, -dx};

case "left": return new int[]{-dy, dx};

case "backward": return new int[]{-dx, -dy};

return new int[]{0, 0};

public static int bfs(int[][] grid, int[] start, int[] end, int[] moveRule) {

int M = [Link], N = grid[0].length;


boolean[][] visited = new boolean[M][N];

Queue<Node> queue = new LinkedList<>();

[Link](new Node(start[0], start[1], 0));

visited[start[0]][start[1]] = true;

String[] dirs = {"forward", "right", "left", "backward"};

while (![Link]()) {

Node cur = [Link]();

if (cur.x == end[0] && cur.y == end[1]) return [Link];

for (String d : dirs) {

int[] move = rotate(moveRule[0], moveRule[1], d);

int nx = cur.x + move[0];

int ny = cur.y + move[1];

if (nx >= 0 && ny >= 0 && nx < M && ny < N && !visited[nx][ny] && grid[nx][ny] == 0) {

visited[nx][ny] = true;

[Link](new Node(nx, ny, [Link] + 1));

return -1;

}
public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

int M = [Link](), N = [Link]();

int[][] grid = new int[M][N];

for (int i = 0; i < M; i++)

for (int j = 0; j < N; j++)

grid[i][j] = [Link]();

int[] start = {[Link](), [Link]()};

int[] end = {[Link](), [Link]()};

int[] moveRule = {[Link](), [Link]()};

[Link](bfs(grid, start, end, moveRule));

PROBLEM 2

import [Link].*;

public class StringFactor {

static class TrieNode {

Map<Character, TrieNode> children = new HashMap<>();

boolean isEnd = false;

}
static void insert(TrieNode root, String word) {

for (int i = 0; i < [Link](); i++) {

TrieNode node = root;

for (int j = i; j < [Link](); j++) {

char c = [Link](j);

node = [Link](c, k -> new TrieNode());

[Link] = true;

static List<Integer> getMatches(TrieNode root, String X, int start) {

List<Integer> res = new ArrayList<>();

TrieNode node = root;

for (int i = start; i < [Link](); i++) {

char c = [Link](i);

if (![Link](c)) break;

node = [Link](c);

[Link](i + 1);

return res;

public static String minStringFactor(String X, String Y, int S, int R) {


int n = [Link]();

int[][] dp = new int[n + 1][2]; // [substrings, cost]

for (int i = 1;

You might also like