cb2

 avatar
unknown
plain_text
2 years ago
12 kB
5
Indexable
package Magnetic;

import java.io.FileInputStream;
import java.util.Scanner;

class MyStack {
	   private int maxSize;
	   private long[] stackArray;
	   private int top;
	   
	   public MyStack(int s) {
	      maxSize = s;
	      stackArray = new long[maxSize];
	      top = -1;
	   }
	   public void push(long j) {
	      stackArray[++top] = j;
	   }
	   public long pop() {
		   stackArray[top] = 0;
	      return stackArray[top--];
	   }
	   public long peek() {
	      return stackArray[top];
	   }
	   public boolean isEmpty() {
	      return (top == -1);
	   }
}
public class magnetic {
	static int T = 10;
	static int N;
	static int [][] map;
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Magnetic//magnetic.txt"));
		Scanner sc = new Scanner(System.in);
		for (int test_case = 1; test_case <= T; test_case++){
			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();
				}
			}
			int Ans = 0;
			for (int j = 0; j < N; j++) {
				boolean check1 = false;
				for (int i = 0; i < N; i++) {
					if (map[i][j] == 1) {
						check1 = true;
					} 
					if (map[i][j] == 2 && check1) {
						Ans++;
						check1 = false;
					}
				}
			}
			System.out.println("#"+test_case+ " "+Ans);
		}
	}
}



###########################################
package MapColor;

import java.io.FileInputStream;
import java.util.Scanner;

public class mapColor {
	static int MAX_SIZE = 100000;
	static int [] queue;
	static int rear = -1;
	static int front = -1;
	static void push(int x) {
		if (rear == MAX_SIZE-1) {
			rear = -1;
		}
		rear++;
		queue[rear] = x;
	}
	static int pop() {
		if (front == MAX_SIZE-1) {
			front = -1;
		}
		front++;
		return queue[front];
	}
	static boolean isEmpty() {
		return rear == front;
	}
	
	
	
	static int n;
	static int [][] map;
	static int [] arr;
	static int Ans;
	static void bfs () {
		int k;
		while (!isEmpty()) {
			k = pop();
			for (int i = 0; i < n; i++) {
				if (map[k][i] == 1) {
					if (arr[i] == arr[k]) {
						Ans = -1;
						return;
					} else if (arr[i] == -1){
						arr[i] = 1 - arr[k];
						push(i);
					}
				}
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//MapColor//mapColor.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			front = rear = -1;
			queue = new int [MAX_SIZE];
			System.out.print("#" + test_case+ " ");
			n = sc.nextInt();
			map = new int [n][n];
			arr = new int [n];
			int m = sc.nextInt();
			for (int i = 0; i < m; i++) {
				int a = sc.nextInt()-1;
				int b = sc.nextInt()-1;
				map[a][b] = map[b][a] = 1;
			}
			for (int i = 0; i < n; i++) {
				arr[i] = -1;
			}
			Ans = 0;
			arr[0] = 0;
			push(0);
			
			bfs();
			
			if (Ans != -1) {
				for (int i = 0; i < n; i++) {
					System.out.print(arr[i]);
				}
			} else {
				System.out.print(Ans);
			}
			System.out.println();
			
		}
		
	}

}




##########################################
package Painting;

import java.io.FileInputStream;
import java.util.Scanner;

public class painting {
	static int [][] map;
	static int n;
	static int [] arr;
	static int COLOR = 4;
	static int Ans;
	
	static boolean check(int v, int i) {
		for (int j = 0; j < n; j++) {
			if (map[v][j] == 1 && arr[j] == i) {
				return false;
			}
		}
		return true;
	}
	
	static void Try(int v) {
		if (v == n) {
			Ans++;
			return;
		}
		for (int i = 0; i < COLOR; i++) {
			if (check(v, i)) {
				arr[v] = i;
				Try(v+1);
				arr[v] = -1;
			}
		}
	}
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Painting//paint.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++ ){
			n = sc.nextInt();
			map = new int [n][n];
			arr = new int [n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = sc.nextInt();
				}
			}
			for (int i = 0; i < n; i++) {
				arr[i] = -1;
			}
			Ans = 0;
			Try(0);
			System.out.println("Case #" + test_case);
			System.out.println(Ans);
		}
	}
}



##################################
package Pize_Location;

import java.io.FileInputStream;
import java.util.Scanner;

public class pizza_location {

	static int k, r, m, n;
	static int [][] sol;
	static int [][] loc;
	static int sumPeople;
	static int [] res;
	static boolean [][] cntRes;
	static boolean [] visit;
	
	static void reset() {
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				cntRes[i][j] = false;
			}
		}
		for (int i = 0; i < m; i++) {
			res[i] = 0;
		}
		sumPeople = 0;
	}
	static void check(int a) {
		if (a > m) return;
		int sum = 0;
		for (int i = 0; i < m; i++) {
			sum += res[i];
		}
		
		if (sum == k) {
			int count = 0;
			for (int i = 0; i < n; i++) {
				visit[i] = false;
			}
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					if (res[i] == 1 && !visit[j] && cntRes[i][j]) {
						count += sol[2][j];
						visit[j] = true;
					}
				}
			}
			if (sumPeople < count) sumPeople = count;
			return;
		}
		for (int i = 1; i >= 0; i--) {
			res[a] = i;
			check(a+1);
		}
	}
	
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Pize_Location//pizza.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			
			k = sc.nextInt();
			r = sc.nextInt();
			m = sc.nextInt();
			res = new int [m+1];
			loc = new int [2][m];
			for (int i = 0; i < m; i++) {
				loc[0][i] = sc.nextInt();
				loc[1][i] = sc.nextInt();
			}
			n = sc.nextInt();
			sol = new int [3][n];
			visit = new boolean [n];
			cntRes = new boolean [m][n];
			for (int i = 0; i < n; i++) {
				sol[0][i] = sc.nextInt();
				sol[1][i] = sc.nextInt();
				sol[2][i] = sc.nextInt();
			}
			
			reset();
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					if ((loc[0][i] - sol[0][j]) * (loc[0][i] - sol[0][j])
							+ (loc[1][i] - sol[1][j]) * (loc[1][i] - sol[1][j]) <= r * r) {
						cntRes[i][j] = true;
					}
				}
			}
			check(0);
			System.out.println(sumPeople);
		}
	}

}


#####################################
package Sky_Force;

import java.io.FileInputStream;
import java.util.Scanner;

public class Sky_Force {
	static int m = 5;
	static int n;
	static int [][] map;
	static int[] dy = { -1, 0, 1 };
	static int Ans;
	
	static void useBomb (int x, int y) {
		for (int i = x; i > x-5; i --) {
			for (int j = 0; j < 5; j++) {
				if (i >= 0 && (map[i][j] == 2 || map[i][j] == -2)) {
					map[i][j] = 0 - map[i][j];
				}
			}
		}
	}
	
	static void move(int x, int y, int bomb, int sum) {
		if (x == 0) {
			if (Ans < sum) {
				Ans = sum;
			}
			
			return;
		}
		x--;
		for (int i = 0; i < 3; i++) {
			int y1 = y + dy[i];
			if (y1 >= 0 && y1 < 5 && sum >= 0) {
				if (map[x][y1] < 2) {
					if (map[x][y1] == 1) {
						move(x, y1, bomb, sum+1);
					}
					else {
						move(x, y1, bomb, sum);
					}
				} else if (map[x][y1] == 2 && bomb == 1) {
					useBomb(x, y1);
					move(x, y1, 0, sum);
					useBomb(x, y1);
				} else {
					move(x, y1, bomb, sum-1);
				}
			}
		}
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Sky_Force//sky_force.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			n = sc.nextInt();
			map = new int [n+1][m];
			for(int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					map[i][j] = sc.nextInt();
				}
			}
			Ans = -1;
			move(n, 2, 1, 0);
			System.out.println("Case #" + test_case);
			System.out.println(Ans);
		}
	}

}



#######################################3
package ValidateMaze;

import java.io.FileInputStream;
import java.util.Scanner;

public class validateMaze {
	static int T;
	static char [][] maze;
	static int col;
	static int x1;
	static int y1;
	static int row;
	static int [] dx = {0, 1, 0, -1};
	static int [] dy = {1, 0, -1, 0};
	static void move (int x, int y) {
		maze[x][y] = '#';
		for (int i = 0; i < 4; i++) {
			x1 = x + dx[i];
			y1 = y + dy[i];
			if (x1 >= 0 && y1 >= 0 && x1 < row && y1 < col && maze[x1][y1] == '.') {
				move (x1, y1);
			}
		}
	}
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//ValidateMaze//maze.txt"));
		Scanner sc = new Scanner(System.in);
		T = sc.nextInt();
		for(int test_case = 1; test_case <= T; test_case++) {
			row = sc.nextInt();
			col = sc.nextInt();
			maze = new char[row][col];
			int [][] arr = new int [2][2];
			sc.nextLine();
			for (int i = 0; i < row; i++) {
				String s = sc.nextLine();
				for (int j = 0; j < col; j++) {
					maze[i][j] = s.charAt(j);
				}
			}
			int count = 0;
			for (int i = 0; i < row; i++) {
				for (int j = 0; j < col; j++) {
					if (i == 0 || j == 0 || i == row -1 || j == col -1) {
						if (maze[i][j] == '.') {
							count ++;
							if (count > 1)break;
							arr[0][count-1] = i;
							arr[1][count-1] = j;
							
						}
					}
				}
			}
			if (count == 2) {
				move(arr[0][0], arr[1][0]);
			}
			String Ans = "Invalid";
			if (maze[arr[0][1]][arr[1][1]] == '#' && count == 2) {
				Ans = "Valid";
			}
			System.out.println("#" + test_case + " " + Ans);
			
		}

	}

}



#####################################
package Well_Project;

import java.io.FileInputStream;
import java.util.Scanner;

public class Well_Project {
	static int n;
	static int [][] map;
	static int value;
	static int [] parent, key;
	static boolean [] visit;
	
	static int min() {
		int min = 1000000;
		int k = -1;
		for (int i = 0; i < n; i++) {
			if (!visit[i] && min > key[i]) {
				min = key[i];
				k = i;
			}
		}
		return k;
	}

	static void prime() {
		key[0] = 0;
		for (long i = 0; i < n; i++) {
			int node = min(); 
			visit[node] = true;
			for (int j = 0; j < n; j++) {
				if (visit[j] && map[node][j] != 0 && map[node][j] < key[j]) {
					key[j] = map[node][j];
				}
			}
		}
	}
	
	static boolean isValidEgde (int u, int v, boolean [] inMST) {
		if (u == v) return false;
		if (inMST[u] == false && inMST[v] == false) return false;
		else if (inMST[u] == true && inMST[v] == true) return false;
		return true;
	}
	
	static void primev () {
		boolean [] inMST = new boolean [n];
		inMST[0] = true;
		int edge = 0, minCost = 0;
		while (edge < n - 1) {
			int min = 10000;
			int a = -1, b = -1;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (map[i][j] < min && isValidEgde(i, j, inMST)) {
						min = map[i][j];
						a = i;
						b = j;
					}
				}
			}
			if (a != -1 && b != -1) {
				edge++;
				minCost += min;
				inMST[b] = inMST[a] = true;
			}
			
		}
		value = minCost;
	}
	
	public static void main(String[] args) throws Exception {
		System.setIn(new FileInputStream("D://Trainee//SRV_training//src//Well_Project//well.txt"));
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		for (int test_case = 1; test_case <= T; test_case++) {
			int k = 0;
			n = sc.nextInt();
			map = new int [n][n];
			visit = new boolean [n];
			parent = new int [n];
			key = new int [n];
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					map[i][j] = sc.nextInt();
				}
			}
			
			for (int i = 0; i < n; i++) {
				key[i] = 1000000;
			}
			
			value = 0;
			primev();
			
			System.out.println("Case #" + test_case);
			System.out.println(value);
		}

	}

}


Editor is loading...