Skip to content

included Java code #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
76 changes: 76 additions & 0 deletions track-1/bot-saves-princess.org
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,79 @@
#+RESULTS:
: DOWN
: LEFT

** Java
#+Begin_SRC java output :var _input=sample-input
import java.io.*;
import java.util.*;

public class Solution {

static PrincessPos pp;
static MarioPos mp;

static class MarioPos{
int i,j;
public MarioPos(int i, int j){
this.i = i;
this.j = j;
}
}

static class PrincessPos{
int i,j;
public PrincessPos(int i, int j){
this.i = i;
this.j = j;
}
}

static void displayPathtoPrincess(int n, char grid[][]){
int up, left;
int diff = pp.i-mp.i;
if(diff < 0)
for( ;diff < 0;++diff)
System.out.println("UP");
else
for( ; diff > 0; --diff)
System.out.println("DOWN");

diff = pp.j-mp.j;
if(diff < 0)
for( ;diff < 0; ++diff)
System.out.println("LEFT");
else
for( ;diff > 0; --diff)
System.out.println("RIGHT");

}

public static void main(String[] args) {
/* Enter your code here. Read input from STDIN.
Print output to STDOUT.
Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
sc.nextLine();
char[][] grid = new char[N][N];
for(int i =0; i < N; i++){
String line = sc.nextLine();
for(int j =0; j < line.length(); j++){
grid[i][j] = line.charAt(j);
if(grid[i][j]== 'p'){
pp= new PrincessPos(i,j);
}
else if(grid[i][j] == 'm'){
mp = new MarioPos(i,j);
}
}
System.out.println("");
}

displayPathtoPrincess(N, grid);
}
}
#+END_SRC
#+RESULTS:
: DOWN
: LEFT