Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@
*.exe
*.out
*.app

# JetBrains idea
.idea/*
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import java.util.*;

public class Solution {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int gridDimensions;
gridDimensions = in.nextInt();
CellType[][] grid = new CellType[gridDimensions][gridDimensions];
for(int row = 0; row < gridDimensions; row++) {
String rowStr = in.next();
for(int column = 0; column < gridDimensions; column++){
grid[row][column] = CellType.of(rowStr.charAt(column));
}
}

displayPathToPrincess(gridDimensions,grid);
}

private static void displayPathToPrincess(int n, CellType [][] grid){
// Find the location of the bot and princess
int[] princessLocation = getLocationOf(CellType.PRINCESS, grid);
int[] botLocation = getLocationOf(CellType.BOT, grid);

// calculate the row-Diff and column-Diff
int rowDiff = botLocation[0] - princessLocation[0];
int colDiff = princessLocation[1] - botLocation[1];

// print the output
printMovementByDifference(rowDiff, "UP", "DOWN");
printMovementByDifference(colDiff, "RIGHT", "LEFT");
}

private static void printMovementByDifference(int diff, String positiveDirectionName, String negativeDirectionName){
String verticalMovement = diff > 0 ? positiveDirectionName : negativeDirectionName;
diff = diff < 0 ? diff * -1: diff;

for(int i = 0; i < diff; i++) {
System.out.println(verticalMovement);
}
}

private static int[] getLocationOf(CellType cellTypeToSearchFor, CellType [][] grid){
// loop through the rows
for(int rowIdx = 0; rowIdx < grid.length; rowIdx++){
// loop through the columns
for(int columnIdx = 0; columnIdx < grid[rowIdx].length; columnIdx++){
// check the type
if(grid[rowIdx][columnIdx] == cellTypeToSearchFor){
return new int[]{rowIdx, columnIdx};
}
}
}
throw new IllegalArgumentException("No cell of type: " + cellTypeToSearchFor + " in the grid!");
}
}

enum CellType {

EMPTY,
BOT,
PRINCESS;

static CellType of(char cellStrRep){
switch (cellStrRep){
case '-':
return EMPTY;
case 'm':
return BOT;
case 'p':
return PRINCESS;
default:
throw new IllegalArgumentException("No CellType that is identified by " + cellStrRep);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

static void nextMove(int posr, int posc, String[] board){
// first check if the bot is on the dirty spot
if(board[posr].charAt(posc) == 'd'){
System.out.println("CLEAN");
return;
}

// determine the position of the dirty cell
int[] locationOfDirtyCell = locationOfDirtyCell(board);
int rowIdx = locationOfDirtyCell[0];
int colIdx = locationOfDirtyCell[1];

// first check the row
if(posr == rowIdx){
// check the column
if(colIdx < posc){
System.out.println("LEFT");
}else{
System.out.println("RIGHT");
}
}else if(rowIdx < posr){
System.out.println("UP");
}else{
System.out.println("DOWN");
}
}

static int[] locationOfDirtyCell(String[] board){
// loop through the rows
for(int rowIdx = 0; rowIdx < board.length; rowIdx++){
// loop through the characters
for(int colIdx = 0; colIdx < board[rowIdx].length(); colIdx++){
// determine if it is a dirty cell
if(board[rowIdx].charAt(colIdx) == 'd'){
return new int[]{rowIdx, colIdx};
}
}
}
throw new IllegalArgumentException("Cannot find any dirty spots on the board!");
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int [] pos = new int[2];
String board[] = new String[5];
for(int i=0;i<2;i++) pos[i] = in.nextInt();
for(int i=0;i<5;i++) board[i] = in.next();
nextMove(pos[0], pos[1], board);
}
}