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:39:31
2018-05-26 15:37:01
2018-05-26 15:34:36
2018-05-26 15:05:34
Task:Find a Word
Sender:DROP TABLE
Submission time:2018-05-26 15:39:31
Status:READY
Result:TIME LIMIT EXCEEDED

Show test data

Code

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;


public class Main2 {
	static List<String> WORDS = new ArrayList<>();
	static int n;
	static int k;
	static Scanner scanner = new Scanner(System.in);
	static char[][] table;
	
	public static void rec(int y, int x){
		String s = "";
		s += table[y][x];
		if (y == n - 1 && x == n - 1){
			WORDS.add(s);
			return;
		}
		if(x < n - 1) {
			rec(y, x + 1, s);
		}
		if(y < n - 1) {
			rec(y + 1, x, s);
		}
	}
	public static void rec(int y, int x, String s){
		s += table[y][x];
		if (y == n - 1 && x == n - 1){
			WORDS.add(s);
			return;
		}
		if(x < n - 1) {
			rec(y, x + 1, s);
		}
		if(y < n - 1) {
			rec(y + 1, x, s);
		}
	}
	
	public static void main(String[] args) {
		
		
		n = scanner.nextInt();
		k = scanner.nextInt();
		table = new char[n][n];
		for(int i = 0; i < n; i++){
			table[i] = scanner.next().toCharArray();
		}
		rec(0,0);
		WORDS.sort(null);
		//System.out.println(WORDS);
		System.out.println(WORDS.get(k-1));
	}
}