Click here to Skip to main content
15,892,059 members

How do I fix the the code (JAVA)?

UA12 asked:

Open original thread
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.
Tags: Java

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900