Google Kick Start 2019 C round solution to a problem the first question Wiggle Walk

Google Kick Start 2019 C round solution to a problem the first question Wiggle Walk

Topic address: https: //codingcompetitions.withgoogle.com/kickstart/round/0000000000050ff2/0000000000150aac

The following code is what I wrote violent approaches to modeling, can only sample had submitted will go Runtime Error.

import java.util.*;
import java.io.*;
public class Solution {
    static String input = "3" + "\n"
        + "5 3 6 2 3" + "\n"
        + "EEWNS" + "\n"
        + "4 3 3 1 1" + "\n"
        + "SESE" + "\n"
        + "11 5 8 3 4" + "\n"
        + "NEESSWWNESE" + "\n";
    
    public static void main(String[] args) {
        //Scanner sc = new Scanner(System.in);
        Scanner sc = new Scanner(new StringReader(input));
        int T = sc.nextInt();
        int[][] D = new int[128][2];
        D['E'] = new int[]{0, 1};
        D['W'] = new int[]{0, -1};
        D['N'] = new int[]{-1,0};
        D['S'] = new int[]{1,0};
        for (int i = 0; i < T; i++) {
            int N = sc.nextInt();
            int R = sc.nextInt();
            int C = sc.nextInt();
            int Sr = sc.nextInt();
            int Sc = sc.nextInt();
            sc.nextLine();
            String seq = sc.nextLine();
            boolean[][] map = new boolean[R + 1][C + 1];
            map[Sr][Sc] = true;
            
            for (int j = 0; j < N; j++) {
                do {
                    Sr += D[seq.charAt(j)][0];
                    Sc += D[seq.charAt(j)][1];
                } while (map[Sr][Sc] == true);
                map[Sr][Sc] = true;
            }
            System.out.println("Case #" + (i + 1) + ": " + Sr + " " + Sc);
        }
    }
}

Here we are HashMapable to live version of the original answer from Python solution on StackExchange .

Note that the static inner classes Point, I rewrote object equals()methods and hashCode()methods to find objects HashMap correctly. hashCode()The method used to calculate the hash value without rewriting the memory address of the object will be used as a hash value, which we do not want to see.
equals()Method for solving hash collision objects after the actual content comparison.

import java.util.*;
import java.io.*;
public class Solution {
    static String input = "3" + "\n"
        + "5 3 6 2 3" + "\n"
        + "EEWNS" + "\n"
        + "4 3 3 1 1" + "\n"
        + "SESE" + "\n"
        + "11 5 8 3 4" + "\n"
        + "NEESSWWNESE" + "\n";
    
    static class Point {
        int x;
        int y;
        Point() {}
        Point(int xx, int yy) { x = xx; y = yy; }
        @Override
        public boolean equals(Object obj) {
            Point that = (Point) obj;
            return this.x == that.x && this.y == that.y;
        }
        @Override
        public int hashCode() {
            return x * 50000  + y;
        }
    }
    static HashMap<Point, Point>[] neighbors;

    public static void init() {
        neighbors = new HashMap[128];
        neighbors['W'] = new HashMap<Point, Point>();
        neighbors['E'] = new HashMap<Point, Point>();
        neighbors['S'] = new HashMap<Point, Point>();
        neighbors['N'] = new HashMap<Point, Point>();
    }

    public static Point getNeighbor(Point cur, char direction) {
        if (neighbors[direction].containsKey(cur)) {
            return neighbors[direction].get(cur);
        }
        switch(direction) {
            case 'W': return new Point(cur.x - 1, cur.y);
            case 'E': return new Point(cur.x + 1, cur.y);
            case 'N': return new Point(cur.x, cur.y - 1);
            case 'S': return new Point(cur.x, cur.y + 1);
            default: return null;
        }
    }

    public static void linkNeighbors(Point cur) {
        Point west = getNeighbor(cur, 'W');
        Point east = getNeighbor(cur, 'E');
        Point north = getNeighbor(cur, 'N');
        Point south = getNeighbor(cur, 'S');
        neighbors['W'].put(east, west);
        neighbors['E'].put(west, east);
        neighbors['N'].put(south, north);
        neighbors['S'].put(north, south);
    }
    
    public static void main(String[] args) {
        //Scanner sc = new Scanner(System.in);
        Scanner sc = new Scanner(new StringReader(input));
        int T = sc.nextInt();
        for (int i = 0; i < T; i++) {
            int N = sc.nextInt();
            int R = sc.nextInt();
            int C = sc.nextInt();
            int Sr = sc.nextInt();
            int Sc = sc.nextInt();
            sc.nextLine();  // skip \n at end of previous line
            String seq = sc.nextLine();
            
            init();
            Point cur = new Point(Sc, Sr);
            for (int j = 0; j < N; j++) {
                linkNeighbors(cur);
                cur = getNeighbor(cur, seq.charAt(j));
            }

            System.out.println("Case #" + (i + 1) + ": " + cur.y + " " + cur.x);
        }
    }
}

Guess you like

Origin www.cnblogs.com/fondoger/p/google-kick-start-2019-round-C-question-1-wiggle-walk-solution.html