How come the following code fails on testcase 2 and 3? The diagonal movement works but when the inputs are in the correct state to move either west or east, the next movement is still a diagonal.
using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
* ---
* Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
**/
class Player
{
static void Main(string[] args)
{
string[] inputs = Console.ReadLine().Split(' ');
int lightX = int.Parse(inputs[0]); // the X position of the light of power
int lightY = int.Parse(inputs[1]); // the Y position of the light of power
int initialTX = int.Parse(inputs[2]); // Thor's starting X position
int initialTY = int.Parse(inputs[3]); // Thor's starting Y position
string d;
// game loop
while (true)
{
d = "";
int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.
// Write an action using Console.WriteLine()
// To debug: Console.Error.WriteLine("Debug messages...");
if (initialTX > lightX && initialTY == lightY) //5 7 8 9
{
Console.Error.WriteLine("Debug messages...");
d = "W";
}
else if (initialTX < lightX && initialTY == lightY)
{
Console.Error.WriteLine("Debug messages...");
d = "E";
}
if (initialTY > lightY && initialTX == lightX)
{
d = "N";
}
else if (initialTY < lightY && initialTX == lightX)
{
d = "S";
}
//
if (initialTX < lightX && initialTY > lightY)
{
d = "NE";
}
else if (initialTX > lightX && initialTY > lightY)
{
d = "NW";
}
if (initialTX < lightX && initialTY < lightY)
{
d = "SE";
}
else if (initialTX > lightX && initialTY < lightY)
{
d = "SW";
}
// A single line providing the move to be made: N NE E SE S SW W or NW
Console.WriteLine(d);
}
}
}
My code run pretty well on visual studio
I’m a new user, therefor I can not upload image
but it run thor(x,y) to the same location as the light once ^.^
Hope developer can fix it, then I can get the whole score;
here is my chickened code ~~
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
* ---
* Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
**/
int main()
{
string direction;
int lightX; // the X position of the light of power
int lightY; // the Y position of the light of power
int initialTX; // Thor's starting X position
int initialTY; // Thor's starting Y position
cin >> lightX;
cin.ignore();
cin >> lightY;
cin.ignore();
cin >> initialTX;
cin.ignore();
cin >> initialTY;
cin.ignore();
// game loop
while (1) {
int remainingTurns; // The remaining amount of turns Thor can move. Do not remove this line.
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
cin >> remainingTurns;
for (int i = 0; i < remainingTurns; i++) {
if (initialTY == (lightY - 1)) {
initialTY = lightY;
}
if (initialTX > lightX) {
--initialTX;
}
else if (initialTX < lightX) {
++initialTX;
}
else {}
if (initialTY > lightY) {
--initialTY;
}
else if (initialTY < lightY) {
++initialTY;
}
else {}
if ((initialTY == lightY) && (initialTX < lightX)) {
direction = "E";
}
else if ((initialTY == lightY) && (initialTX > lightX)) {
direction = "W";
}
else if ((initialTY < lightY) && (initialTX == lightX)) {
direction = "S";
}
else if ((initialTY > lightY) && (initialTX == lightX)) {
direction = "N";
}
else if ((initialTY < lightY) && (initialTX < lightX)) {
direction = "SE";
}
else if ((initialTY > lightY) && (initialTX < lightX)) {
direction = "NE";
}
else if ((initialTY > lightY) && (initialTX > lightX)) {
direction = "NW";
}
else if ((initialTY < lightY) && (initialTX > lightX)) {
direction = "SW";
}
// A single line providing the move to be made: N NE E SE S SW W or NW
cout << direction << endl;
//cout << lightX << " " << lightY << " " << initialTX << " " << initialTY;
}
}
}
Could anyone explain to me why this solution works for test 1,2,3 only, but not for 4?
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int lightX = in.nextInt(); // the X position of the light of power
int lightY = in.nextInt(); // the Y position of the light of power
int initialTX = in.nextInt(); // Thor's starting X position
int initialTY = in.nextInt(); // Thor's starting Y position
int grobX = 0;
int grobY = 0;
int bewegen=0;
String richtung="";
// game loop
while (true) {
int remainingTurns = in.nextInt(); // The remaining amount of turns Thor can move. Do not remove this line.
// Write an action using System.out.println()
// To debug: System.err.println("Debug messages...");
//x-Richtung
if (lightX-initialTX<0)
{
grobX=-1;
initialTX=initialTX-1;
}
else if (lightX-initialTX>0)
{
grobX=1;
initialTX=initialTX+1;
}
else
{
grobX=0;
}
//y-Richtung
if (lightY-initialTY<0)
{
grobY=-10;
initialTY=initialTY-1;
}
else if (lightY-initialTY>0)
{
grobY=10;
initialTY=initialTY+1;
}
else
{
grobY=0;
}
bewegen=grobX+grobY;
switch(bewegen){
case -10:
richtung="N";
break;
case -9:
richtung="NE";
break;
case 1:
richtung="E";
break;
case 11:
richtung="SE";
case 10:
richtung="S";
break;
case 9:
richtung="SW";
break;
case -1:
richtung="W";
break;
case -11:
richtung="NW";
break;
}
// A single line providing the move to be mhave obade: N NE E SE S SW W or NW
System.out.println(richtung);
}
}
Thanks for your answer. Yes, (0,0) is top-left here, too.
I changed the updates of the coordinates from the “if else if” lines to the switch cases. Thor’s movement changed, but he is still too slow to pass test #4.
I am using C++ for this puzzle. When I was trying to assign and print letters for the directions, I originally used char data type and it did not work. When I switched to string data type, the code worked fine. Why can I not print char data type for the directions?
I can’t understand why the variable ‘remaining_turn’ should be written again and again in the infinite while loop. To work it as a constraint, it should be initialized only once at the outside of the while loop.