I tried to replicate solutions for this puzzle MOON ROBOTS
But it led to failure while the solutions i wrote closely looked like my original working codes !
I tried to replicate solutions for this puzzle MOON ROBOTS
But it led to failure while the solutions i wrote closely looked like my original working codes !
I can’t find any puzzle named “Moon Robots”, what puzzle are you talking about?
Go to the option LEARN then slect java then select ALGORYTHMN IN JAVA then select page 4/6 in which there’s the pgm Moon Robots
ok it’s a playground written by @egaetan
do you have a specific question about what you’re trying to achieve?
I’ll show you two versions solutions which look same but one works and the other does not. I think the website may have issues.
Here’s the solution using the enumeration which works :
// {
package com.egaetan;
// }
/*******
* Lire les donnees depuis System.in
* Utilise System.out.println pour afficher le resultat
* Utilise System.err.println pour afficher des donnees de debug
* ***/
import java.util.*;
public class RobotsLunaires {
public void main() {
String ligne;
char action;
int x,y;
final int abscisse;
final int ordonnee;
Scanner sc = new Scanner(System.in);
abscisse = sc.nextInt();
ordonnee = sc.nextInt();
sc.nextLine();
while(sc.hasNextLine()) {
/* Lisez les données et effectuez votre traitement */
ligne = sc.nextLine();
x = Integer.parseInt(ligne.split(" ")[0]);
y = Integer.parseInt(ligne.split(" ")[1]);
String orientation = ligne.split(" ")[2];
Orientation o = Orientation.valueOf(orientation);
String ordre = sc.nextLine();
for(int k = 0; k < ordre.length();k++)
{
action = ordre.charAt(k);
switch(action)
{
case 'L' :
o = o.left();
break;
case 'R' :
o = o.right();
break;
case 'M' :
switch(o)
{
case N :
y++;
break;
case W :
x--;
break;
case E :
x++;
break;
case S :
y--;
break;
}
break;
}
}
System.out.println(x + " " + y + " " + o.toString());
}
/* Vous pouvez aussi effectuer votre traitement une fois que vous avez lu toutes les données.*/
}
}
enum Orientation {
N, W, S, E;
public Orientation right(){
switch(this)
{
case N :
return E;
case W :
return N;
case E :
return S;
case S :
return W;
}
return null;
}
public Orientation left(){
switch(this)
{
case N :
return W;
case W :
return S;
case E :
return N;
case S :
return E;
}
return null;
}
}
Here’s the solution using the enumeration which does not work :
// {
package com.egaetan;
// }
/*******
* Lire les donnees depuis System.in
* Utilise System.out.println pour afficher le resultat
* Utilise System.err.println pour afficher des donnees de debug
* ***/
import java.util.*;
public class RobotsLunaires {
public void main() {
String line;
char action;
int x,y;
final int abscisse;
final int ordonnee;
Scanner sc = new Scanner(System.in);
abscisse = sc.nextInt();
ordonnee = sc.nextInt();
sc.nextLine();
while(sc.hasNextLine()) {
/* Lisez les données et effectuez votre traitement */
line = sc.nextLine();
x = Integer.parseInt(line.split(" ")[0]);
y = Integer.parseInt(line.split(" ")[1]);
String direction = line.split(" ")[2];
Orientation o = Orientation.valueOf(direction);
String ordre = sc.nextLine();
for(int k=0;k < ordre.length();k++)
{
action = ordre.charAt(k);
switch(action)
{
case 'L' :
o = o.left();
break;
case 'R' :
o = o.right();
break;
case 'M' :
switch(o)
{
case N :
y++;
break;
case W :
x--;
break;
case E :
x++;
break;
case S :
y--;
break;
}
break;
}
}
System.out.println(x + y + o.toString());
}
/* Vous pouvez aussi effectuer votre traitement une fois que vous avez lu toutes les données.*/
}
}
enum Orientation{
N,W,S,E;
public Orientation left(){
switch(this){
case N :
return W;
case W :
return S;
case E :
return N;
case S :
return E;
}
return null;
}
public Orientation right(){
switch(this){
case N :
return E;
case W :
return N;
case E :
return S;
case S :
return W;
}
return null;
}
}
These solutions are definitely different
VS
Tanks bud !