Return to problem list

2019-1B Problem A

Final solution

cmd.go:
save   open on GitHub
// package, import, etc...

func start() {
  // read T, repeat test() T times...
}

func test() {
	var numPeople, size int
	mustReadLineOfInts(&numPeople, &size)
	size++
	xs := make([]int, size)
	ys := make([]int, size)
	for i := 0; i < numPeople; i++ {
		line := strings.Split(mustReadLine(), " ")
		assert(len(line) == 3)
		x := mustAtoi(line[0])
		y := mustAtoi(line[1])
		assert(len(line[2]) == 1)
		d := line[2][0]
		switch d {
		case 'N':
			for i := y + 1; i < size; i++ {
				ys[i]++
			}
		case 'S':
			for i := y - 1; i >= 0; i-- {
				ys[i]++
			}
		case 'E':
			for i := x + 1; i < size; i++ {
				xs[i]++
			}
		case 'W':
			for i := x - 1; i >= 0; i-- {
				xs[i]++
			}
		}
	}
	maxX := 0
	maxY := 0
	for i := 0; i < size; i++ {
		if xs[i] > xs[maxX] {
			maxX = i
		}
		if ys[i] > ys[maxY] {
			maxY = i
		}
	}
	fmt.Fprintf(stdout, "%d %d\n", maxX, maxY)
}

// boilerplate omitted...

Solution that only works for the small input

small/cmd.go:
save   open on GitHub
// package, import, etc...

func start() {
  // read T, repeat test() T times...
}

func test() {
	var numPeople, size int
	mustReadLineOfInts(&numPeople, &size)
	size++
	grid := make([][]int, size)
	for y := 0; y < size; y++ {
		grid[y] = make([]int, size)
	}
	for i := 0; i < numPeople; i++ {
		line := strings.Split(mustReadLine(), " ")
		assert(len(line) == 3)
		x := mustAtoi(line[0])
		y := mustAtoi(line[1])
		assert(len(line[2]) == 1)
		d := line[2][0]
		switch d {
		case 'N':
			for y := y + 1; y < size; y++ {
				for x := 0; x < size; x++ {
					grid[y][x]++
				}
			}
		case 'S':
			for y := y - 1; y >= 0; y-- {
				for x := 0; x < size; x++ {
					grid[y][x]++
				}
			}
		case 'E':
			for x := x + 1; x < size; x++ {
				for y := 0; y < size; y++ {
					grid[y][x]++
				}
			}
		case 'W':
			for x := x - 1; x >= 0; x-- {
				for y := 0; y < size; y++ {
					grid[y][x]++
				}
			}
		}
	}
	var maxX, maxY, maxN int
	for x := 0; x < size; x++ {
		for y := 0; y < size; y++ {
			if maxN < grid[y][x] {
				maxN = grid[y][x]
				maxX = x
				maxY = y
			}
		}
	}
	fmt.Fprintf(stdout, "%d %d\n", maxX, maxY)
}

// boilerplate omitted...

Sample test cases given in question

sample.in:
save   open on GitHub
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