Click here to Skip to main content
14,453,897 members
Rate this:
Please Sign up or sign in to vote.
See more:
For some reason the code does not increment the 2 arrays i.e xa[] and yarray[] when the conditions are met. This is supposed to be the solution to the code jam 2019 round 1B question manhaton the question can be found here. If you could help me I would he highly grateful.

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int cases = input.nextInt();

        for (int a = 1; a <= cases; a++) {
            int people = input.nextInt();
            int length = input.nextInt();

            int[] xa = new int[length];
            int[] yarray = new int[length];

            for (int b = 1; b <= people; b++) {

                int xd = input.nextInt();
                int yd = input.nextInt();
                String direction = input.next();

                if (direction =="N") {
                    for (int c = (yd + 1); c < length; c++) {
                        yarray[c]+=1;
                    }
                }
                else if (direction == "S") {
                    for (int v = (yd - 1); v >= 0; v--) {
                        yarray[v]+=1;
                    }
                }
                else if (direction == "E") {
                    for (int o = (xd + 1); o < length; o++) {
                        xa[o]+=1;
                    }
                }
                else if (direction == "W") {
                    for (int m = (xd - 1); m >= 0; m--) {
                        xa[m]+=1;
                    }
                }
            }
            int max = xa[0];
            int xcord = 0;
            for(int i = 0; i < length; i++)
            {
                if(max < xa[i])
                {
                    max = xa[i];
                    xcord=i;
                }
            }

            int maxy = yarray[0];
            int ycor=0;

            for(int i = 0; i < length; i++)
            {
                if(maxy < yarray[i]) {
                    maxy = yarray[i];
                    ycor=i;
                }
            }

            System.out.println("Case #" + a + ": " + xcord + " "+ ycor);

        }

    }
}


Sample Input :
3
1 10
5 5 N
4 10
2 4 N
2 6 S
1 5 E
3 5 W
8 10
0 2 S
0 3 N
0 3 N
0 4 N
0 5 S
0 5 S
0 8 S
1 5 W


What I have tried:

I have tried changing the increment statements and I have tried changing the loop statements but had no success.
Posted
Updated 28-Apr-19 10:21am
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
For some reason the code does not increment the 2 arrays i.e xa[] and yarray[] when the conditions are met.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Comments
UA12 28-Apr-19 15:20pm
   
Thank You but I found the fault on my code, I was supposed to use .equals() instead of "==".
Patrice T 28-Apr-19 15:23pm
   
Nice to have been helpful.
In order to close the question, either accept my solution, or explain how you solved the question and accpect your solution.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Using .equals() instead of == does the job.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100