Rezultati

Up. imeNalogaJezikRezultatČas oddaje
avokado-2018 Kvadrat števil Java 100/100OK 13. okt '18 @ 12:29

Test Točke Porabljen spomin Porabljen čas Status
#1 10/10 33,699 MiB 0,000 s OK
#2 10/10 35,285 MiB 0,000 s OK
#3 10/10 35,289 MiB 0,000 s OK
#4 10/10 39,277 MiB 0,000 s OK
#5 10/10 43,262 MiB 0,000 s OK
#6 10/10 43,445 MiB 0,000 s OK
#7 10/10 49,223 MiB 0,005 s OK
#8 10/10 52,512 MiB 0,000 s OK
#9 10/10 47,969 MiB 0,000 s OK
#10 10/10 48,383 MiB 0,025 s OK

Ocenjevani program (resitev3.java):
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class resitev3 {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		String first = br.readLine();
		int stevilo = Integer.parseInt(first);
		int[] stevila = new int[stevilo*stevilo];
		int konec = (stevilo*stevilo)-1;

		for (int i = 0; i < stevilo * stevilo; i++) {
			stevila[i] = Integer.parseInt(br.readLine());
		}
		
		Arrays.sort(stevila);
		
		int[][] matrika = new int[stevilo][stevilo];
		
		for (int j = 0; j < matrika.length; j++) {
			for (int i = 0; i < matrika.length; i++) {
				matrika[i][j] = Integer.MAX_VALUE;
			}
		}
		
		Queue<int[]> q = new LinkedList<int[]>();
		int[] start = new int[2];
		start[0] = stevilo/2;
		start[1] = stevilo/2;

		q.add(start);
		
		while (!q.isEmpty()) {
			int[] v = q.poll();
//			System.out.println(v[0] + " " + v[1]);
			if (v[0] < 0 || v[1] < 0 || v[0] >= stevilo || v[1] >= stevilo) {
				continue;
			}
			if (matrika[v[0]][v[1]] == Integer.MAX_VALUE) {
				matrika[v[0]][v[1]] = stevila[konec];
				konec-=1;
				int[] next1 = new int[2];
				next1[0] = v[0]+1;
				next1[1] = v[1];
				q.add(next1);
				int[] next2 = new int[2];
				next2[0] = v[0]-1;
				next2[1] = v[1];
				q.add(next2);
				int[] next3 = new int[2];
				next3[0] = v[0];
				next3[1] = v[1]+1;
				q.add(next3);
				int[] next4 = new int[2];
				next4[0] = v[0];
				next4[1] = v[1]-1;
				q.add(next4);
			}
		}
			
		for (int i = 0; i < matrika.length; i++) {
			for (int j = 0; j < matrika.length; j++) {
				System.out.print(matrika[i][j]);
				if (j != matrika.length-1) {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
		
	}
}