Bài đăng

Đang hiển thị bài đăng từ Tháng 6, 2017

Nâng cấp máy tính (Thuật toán quay lui)

package Advance; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class ComputerUpgrade { int N, M; int Answer; int Package[][]; int Value[]; int visit[]; int Subset[]; int SLM; int min; public static void main(String[] args) throws Exception { new ComputerUpgrade().begin(); } private void begin() throws Exception { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { Answer = 0; N = sc.nextInt(); Package = new int[51][51]; Value = new int[51]; min = 100000000; for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { if (i == j) { Package[i][j] = 1; Value[i] = sc.nextInt(); } } } M = sc.nextInt(); for (int i = N + 1; i <= M + N; i++) { Value[i] = sc.nextInt(); int sl = sc.nextInt(); for (int j = 0; j < sl; j++) { int i...

Cleaning Robot (thuật toán BFS + BackTracking)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class CleaningRobot { public static void main(String[] args) throws Exception { new CleaningRobot().inIt(); } int M; int N; int matrix[][]; Location dirty[]; int numDirty; int xRB, yRB; int[][] move = { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } }; MyQueue mQ; int[][] matrixEnd; int min; int visit[]; int Result; class Location { int x; int y; public Location(int a, int b) { x = a; y = b; } } class MyQueue { Location ele[]; int front; int rear; public MyQueue() { front = rear = 0; ele = new Location[10001]; } boolean isEmpty() { if (front == rear) return true; else return false; } void Qpush(Location x) { ele[rear] = x; rear++; if (rear == 10001) rear = 0; } Location Qpop() { Location a = ele[front]; front++; if (front == 10001) front = 0; return ...

Chess Rook (Thuật toán quay lui BackTracking)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class ChessRook { public static void main(String[] args) throws Exception { new ChessRook().inIt(); } int N; int[][] chess; int max; private void inIt() throws Exception { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { N = sc.nextInt(); chess = new int[N][N]; max = 0; for (int i = 0; i < N; i++) { String s = sc.next(); for (int j = 0; j < s.length(); j++) { if (s.charAt(j) == 'X') { chess[i][j] = 2; } } } back(0, 0); System.out.println("Case #" + test_case); System.out.println(max); } } private void back(int row, int dem) { if(dem > max){ max = dem; } int i,j; for (i = row; i < N; i++) { for (j = 0; j < N; j++) { if (check(i, j) == true) { chess[i][j] = 1; back(i, ...

Checking Cube (Thuật toán Quay lui BackTracking)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class CheckingCube { public static void main(String[] args) throws Exception { new CheckingCube().inIt(); } int D; int Result; private void inIt() throws Exception { Scanner sc = new Scanner(System.in); int T; T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { D = sc.nextInt(); Result = 0; backtrack(0, 50, 0); System.out.println("#" + test_case + " " + Result); } } private void backtrack(int total, int num, int c) { if (c > 5) { return; } if (total > D) { return; } if (total == D) { Result++; return; } for (int i = num; i >= 0; i--) { backtrack(total + (i * i * i), i, c + 1); } } }

Quân mã (Thuật toán BFS)

package Advance; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class Solution{ static int S; static int K; static int M; // col static int N; // row static int dem; static int min; static MyQueue myQueue; static int table[][]; static int[][] K_move = { { -2, -1 }, { -2, 1 }, { -1, -2 }, { -1, 2 }, { 1, -2 }, { 1, 2 }, { 2, -1 }, { 2, 1 } }; static class Location { int x; int y; public Location(int a, int b) { x = a; y = b; } } static class MyQueue { Location ele[]; int front; int rear; public MyQueue() { front = 0; rear = 0; ele = new Location[10000]; } boolean isEmpty() { if (front == rear) return true; else return false; } void push(Location x) { ele[rear] = x; rear++; if(rear == 10000) rear = 0; } Location pop() { Location a = ele[front]; front++; if(front == 10000) front = 0; ret...

Biểu thức Zero (thuật toán Quay lui BackTracking)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class BieuThucZero { public static void main(String[] args) throws Exception { new BieuThucZero().inIt(); } int N, Result; int key[]; int k, t; String bieuthuc; private void inIt() throws Exception { Scanner sc = new Scanner(System.in); int T; T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { N = sc.nextInt(); key = new int[N - 1]; Result = 0; backtrack(0); System.out.println("#"+test_case + " " + Result); } } private void backtrack(int num) { if (num == N - 1) { if (checkEnd() == 0) { Result++; } return; } for (int i = 0; i < 3; i++) { key[num] = i; backtrack(num + 1); } } private int checkEnd() { bieuthuc = getString(); k = t = 0; int[] num = new int[N]; int op[] = new int[N - 1]; for (int i = 0; i < bieuthuc.length(); i++) { if ...

Battle City (Thuật toán BFS)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class BattleCity {     public static void main(String[] args) throws Exception {         new BattleCity().inIt();     }     int N, M, xS, yS, xF, yF;     final static int TARGET = -5;     final static int BRICK = -2;     final static int EMPTY = 0;     int Result;     int matrix[][];     int layer[][];     MyQueue mQ;     int move[][] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};     class Location {         int x;         int y;         int value;         public Location(int a, int b, int v) {             x = a;             y = b;             value = v;       ...

Bảo vệ nông trang (thuật toán BFS)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class BaoVeNongTrai { public static void main(String[] args) throws Exception { new BaoVeNongTrai().inIt(); } int N, M; int map[][]; int visit[][]; int Result; boolean isHead; int move[][] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 } }; MyQueue mQ; class Location { int x; int y; int value; public Location(int a, int b, int v) { x = a; y = b; value = v; } } class MyQueue { Location ele[]; int front; int rear; public MyQueue() { front = 0; rear = 0; ele = new Location[100000]; } boolean isEmpty() { if (front == rear) return true; else return false; } void push(Location x) { ele[rear] = x; rear++; if (rear == 100000) rear = 0; } Location pop() { Location a = ele[front]; front++; if (front == 100000) fro...

Tấn công thành trì (Thuật toán Prim tìm cây khung lớn nhất)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class AttackOnTower { int V; int Result; int matrix[][]; int value[]; boolean visit[]; int key[]; public static void main(String[] args) throws Exception { new AttackOnTower().inIt(); } private void inIt() throws Exception { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { V = sc.nextInt(); Result = 0; matrix = new int[V][V]; value = new int[V]; visit = new boolean[V]; key = new int[V]; int u = 0, v = 0, w = 0, e = 0; for (int i = 0; i < V; i++) { u = sc.nextInt(); value[u] = sc.nextInt(); e = sc.nextInt(); for (int j = 0; j < e; j++) { v = sc.nextInt(); matrix[u][v] = 1; } } for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if(matrix[i][j] == 1) { matrix[i][j] = value[i] + value[j]; ...

Assigning Meeting Room (Thuật toán tham lam)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class AssigningMeetingRoom { int Result; int StartTime[]; int EndTime[]; int N; public static void main(String[] args) throws Exception { new AssigningMeetingRoom().inIt(); } private void inIt() throws Exception { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { N = sc.nextInt(); StartTime = new int[N+1]; EndTime = new int[N+1]; int addr = 0; for (int i = 0; i < N; i++) { addr = sc.nextInt(); StartTime[addr] = sc.nextInt(); EndTime[addr] = sc.nextInt(); } sapxep(); Result = 1; int temp = EndTime[1]; for (int i = 2; i <= N; i++) { if(StartTime[i] >= temp){ temp = EndTime[i]; Result++; } } System.out.println("#" + test_case + " " + Result); } } private void sapxep() { int temp = 0; f...

Array Game (Thuật toán chia để trị)

package Advance; import java.io.FileInputStream; import java.util.Scanner; public class ArrayGame { public static void main(String[] args) throws Exception { new ArrayGame().inIt(); } int N; int[] array; long Sum; int Result; private void inIt() throws Exception { Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test_case = 1; test_case <= 20; test_case++) { N = sc.nextInt(); Sum = 0; Result = 0; array = new int[N + 1]; for (int i = 0; i < N; i++) { array[i] = sc.nextInt(); Sum += array[i]; } Result = check(Sum,0,N); System.out.println(Result); } } private int findMid(long sum, int s, int e) { long total = 0; int mid = 0; for (int i = s; i < e; i++) { if (total < sum) { total += array[i]; mid = i; } else break; } if (total != sum){ return -1; } return mid; } private int check(long sum, int start, int end) { if(sum ...