Untitled
unknown
plain_text
2 years ago
3.5 kB
3
Indexable
package Princess; import java.io.FileInputStream; import java.util.Scanner; public class princess { static int MAX_SIZE = 1000; static int [][] queue = new int [2][MAX_SIZE]; static int rear = -1; static int front = -1; static void push(int x, int y) { if (rear == MAX_SIZE-1) { rear = -1; } rear++; queue[0][rear] = x; queue[1][rear] = y; } static void pop() { if (front == MAX_SIZE-1) { front = -1; } front++; } static int tx () { return queue[0][front]; } static int ty () { return queue[1][front]; } static boolean isEmpty() { return rear == front; } static int [][] map; static int n; static int [] dx = {1, 0, -1, 0}; static int [] dy = {0, 1, 0, -1}; static int a; static int b; static int [][] d; static void bfs (int x, int y) { map[x][y] = 0; push(x, y); while (!isEmpty()) { pop(); int p = tx(); int q = ty(); for (int i = 0; i < 4; i++) { int x1 = p + dx[i]; int y1 = q + dy[i]; if (x1 >= 0 && y1 >= 0 && x1 < n && y1 < n && map[x1][y1] == 1) { d[x1][y1] = d[p][q] + 1; map[x1][y1] = 0; push(x1,y1); } } } } public static void main(String[] args) throws Exception { System.setIn(new FileInputStream("F:\\eclipse\\SRV\\src\\input.txt")); Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int testcase = 1; testcase <= T; testcase++) { n = sc.nextInt(); map = new int [n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { map[i][j] = sc.nextInt(); if (map[i][j] == 2) { a = i; b = j; } } } d = new int [n][n]; bfs(a ,b); int Ans = -1; if (d[0][0] != 0 && d[n-1][n-1] != 0) { Ans = d[0][0] + d[n-1][n-1] + 1; } System.out.println(Ans); } } } package Tan_Cong_Thanh_Tri; import java.io.FileInputStream; import java.util.Scanner; public class thanhTri { static int n; static int [][] matrix; static int [] visit; static int [] arr; static int min; static int sum; static void Try (int x) { if (x == n) return; for (int i = 0; i < n; i++) { if (matrix[x][i] != 0) { if (matrix[x][i] < min) min = matrix[x][i]; if (visit[i] == 0) { sum += min; reset(); matrix[x][i] = 0; min = 0; return; } matrix[x][i] = 0; visit[i] = 0; Try(x+1); } } } static void reset() { for (int i = 0; i < n; i++) { visit[i] = arr[i]; } } public static void main(String[] args) throws Exception { System.setIn(new FileInputStream("F:\\eclipse\\SRV\\src\\Tan_Cong_Thanh_Tri\\thanhtri.txt")); Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int testcase = 1; testcase <= T; testcase++) { n = sc.nextInt(); matrix = new int [n][n]; visit = new int [n]; arr = new int [n]; for (int i = 0; i < n; i++) { int num = sc.nextInt(); int a = sc.nextInt(); visit[num] = a; int b = sc.nextInt(); for (int j = 0; j < b; j++) { int c = sc.nextInt(); matrix[i][c] = 1; } } for (int i = 0; i < n; i++) { arr[i] = visit[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (matrix[i][j] == 1) { matrix[i][j] = visit[i] + visit[j]; } } } min = 10000; sum = 0; Try(0); System.out.println(sum); } } }
Editor is loading...