Code Submission Evaluation System Login

CSES - HIIT Open 2018

HIIT Open 2018

Contest start:2018-05-26 11:00:00
Contest end:2018-05-26 16:00:00

Task list | Submit code | Submissions | Messages | Scoreboard | Statistics


History
2018-05-26 15:58:32
2018-05-26 15:42:41
Task:Find a Word
Sender:El Numero Uno
Submission time:2018-05-26 15:58:32
Status:READY
Result:WRONG ANSWER

Show test data

Code

import java.util.*;

class Coord {
	int x;
	int y;
	public Coord(int x, int y) {
		this.x = x;
		this.y=y;
	}
}

public class f {
	static int k;
	static List<String> finalStrings = new ArrayList<>();
	private static List<Coord> getSurroundings(Coord coord, List<List<Character>> matrix) {
		Coord coord1 = null;
		Coord coord2 = null;
		if (coord.x+1 <= matrix.size()-1) {
			coord1 = new Coord(coord.x+1, coord.y);
		}
		if (coord.y+1 <= matrix.size()-1) {
			coord2 = new Coord(coord.x, coord.y+1);
		}
		if (coord1 == null && coord2 == null) {
			return Arrays.asList();
			}
		if (coord1 == null) return Arrays.asList(coord2);
		if (coord2 == null) return Arrays.asList(coord1);
		if (matrix.get(coord1.x).get(coord1.y) < matrix.get(coord2.x).get(coord2.y)) {
			return Arrays.asList(coord1, coord2);
		}
		return Arrays.asList(coord2, coord1);
	}
	
	private static void solve(List<List<Character>> matrix, Coord coord, String currentString) {
		List<Coord> surroundings = getSurroundings(coord, matrix);
		if (surroundings.isEmpty()) {
			finalStrings.add(currentString);
			if (finalStrings.size() >= k) {
				System.out.println(finalStrings.get(k-1));
				System.exit(0);
			}
		}
		List<String> strings = new ArrayList<>();
		for (Coord c : surroundings) {
			String newString = currentString + String.valueOf(matrix.get(c.x).get(c.y));
			solve(matrix, c, newString);
		}
	}
	
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int a=0;
		
		if (in.hasNextLine()) {
			String line = in.nextLine();
			String[] dimensions = line.split(" ");
			a = Integer.parseInt(dimensions[0]);
			k = Integer.parseInt(dimensions[1]);
		}
		List<List<Character>> matrix = new ArrayList<>();
		for (int i = 0; i < a && in.hasNextLine(); i++) {
			String line = in.nextLine();
			char[] charArray = line.toCharArray();
			List<Character> lineList = new ArrayList<>();
			for (char c : charArray) {
				lineList.add(c);
			}
			matrix.add(lineList);
		}
		solve(matrix,new Coord(0,0), String.valueOf(matrix.get(0).get(0)));
	}

}