Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views6 pages

Sofa Problem

Uploaded by

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

Sofa Problem

Uploaded by

shivankyadav479
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

import java.io.

BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayDeque;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashSet;

import java.util.List;

import java.util.Objects;

import java.util.Queue;

import java.util.Set;

public class sofa { // Renamed this line to match your filename "sofa.java"

private static class State {

final int r1, c1, r2, c2;

public State(int r1, int c1, int r2, int c2) {

this.r1 = r1;

this.c1 = c1;

this.r2 = r2;

this.c2 = c2;

@Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;


State state = (State) o;

return r1 == state.r1 && c1 == state.c1 && r2 == state.r2 && c2 == state.c2;

@Override

public int hashCode() {

return Objects.hash(r1, c1, r2, c2);

private static class QueueItem {

final State state;

final int steps;

public QueueItem(State state, int steps) {

this.state = state;

this.steps = steps;

private static int M, N;

private static char[][] grid;

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

String[] dimensions = reader.readLine().split(" ");

M = Integer.parseInt(dimensions[0]);
N = Integer.parseInt(dimensions[1]);

grid = new char[M][N];

List<int[]> sCoords = new ArrayList<>();

List<int[]> SCoords = new ArrayList<>();

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

String[] line = reader.readLine().split(" ");

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

grid[i][j] = line[j].charAt(0);

if (grid[i][j] == 's') {

sCoords.add(new int[]{i, j});

grid[i][j] = '0';

} else if (grid[i][j] == 'S') {

SCoords.add(new int[]{i, j});

grid[i][j] = '0';

if (sCoords.size() != 2 || SCoords.size() != 2) {

System.out.println("Impossible");

return;

Collections.sort(sCoords, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);

State startState = new State(sCoords.get(0)[0], sCoords.get(0)[1], sCoords.get(1)[0],


sCoords.get(1)[1]);
Collections.sort(SCoords, (a, b) -> a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);

State targetState = new State(SCoords.get(0)[0], SCoords.get(0)[1],


SCoords.get(1)[0], SCoords.get(1)[1]);

Queue<QueueItem> queue = new ArrayDeque<>();

Set<State> visited = new HashSet<>();

queue.add(new QueueItem(startState, 0));

visited.add(startState);

while (!queue.isEmpty()) {

QueueItem currentItem = queue.poll();

State currentState = currentItem.state;

int steps = currentItem.steps;

if (currentState.equals(targetState)) {

System.out.println(steps);

return;

int r1 = currentState.r1;

int c1 = currentState.c1;

int r2 = currentState.r2;

int c2 = currentState.c2;

addState(queue, visited, new State(r1 - 1, c1, r2 - 1, c2), steps);

addState(queue, visited, new State(r1 + 1, c1, r2 + 1, c2), steps);


addState(queue, visited, new State(r1, c1 - 1, r2, c2 - 1), steps);

addState(queue, visited, new State(r1, c1 + 1, r2, c2 + 1), steps);

if (r1 == r2) {

if (r1 + 1 < M && grid[r1 + 1][c1] != 'H' && grid[r1 + 1][c2] != 'H') {

addState(queue, visited, new State(r1, c1, r1 + 1, c1), steps);

addState(queue, visited, new State(r1, c2, r1 + 1, c2), steps);

if (r1 - 1 >= 0 && grid[r1 - 1][c1] != 'H' && grid[r1 - 1][c2] != 'H') {

addState(queue, visited, new State(r1 - 1, c1, r1, c1), steps);

addState(queue, visited, new State(r1 - 1, c2, r1, c2), steps);

} else {

if (c1 + 1 < N && grid[r1][c1 + 1] != 'H' && grid[r2][c1 + 1] != 'H') {

addState(queue, visited, new State(r1, c1, r1, c1 + 1), steps);

addState(queue, visited, new State(r2, c1, r2, c1 + 1), steps);

if (c1 - 1 >= 0 && grid[r1][c1 - 1] != 'H' && grid[r2][c1 - 1] != 'H') {

addState(queue, visited, new State(r1, c1 - 1, r1, c1), steps);

addState(queue, visited, new State(r2, c1 - 1, r2, c1), steps);

System.out.println("Impossible");

}
private static void addState(Queue<QueueItem> queue, Set<State> visited, State
potentialState, int currentSteps) {

int r1 = potentialState.r1;

int c1 = potentialState.c1;

int r2 = potentialState.r2;

int c2 = potentialState.c2;

if (r1 < 0 || r1 >= M || c1 < 0 || c1 >= N ||

r2 < 0 || r2 >= M || c2 < 0 || c2 >= N ||

grid[r1][c1] == 'H' || grid[r2][c2] == 'H') {

return;

State normalizedState;

if (r1 < r2 || (r1 == r2 && c1 < c2)) {

normalizedState = potentialState;

} else {

normalizedState = new State(r2, c2, r1, c1);

if (!visited.contains(normalizedState)) {

visited.add(normalizedState);

queue.add(new QueueItem(normalizedState, currentSteps + 1));

You might also like