Untitled
unknown
plain_text
2 years ago
5.9 kB
10
Indexable
// Dinamond - Stack
package Lesson_9;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Diamond {
static int T, nodes, edges, cnode;
static int map[][], index[];
static boolean visited[];
public static class Stack{
static int top;
static int Data[];
public Stack() {
this.top = -1;
Data = new int[nodes];
}
public static void reset(){
top = -1;
}
public static boolean is_Empty(){
return top == -1 ? true : false;
}
public static void push(int value){
Data[++top] = value;
}
public static int pop(){
return Data[top--];
}
public static int peek(){
top = Data.length - 1;
return Data[top];
}
}
public static void main(String[] args) throws FileNotFoundException{
System.setIn(new FileInputStream("Diamond"));
Scanner scanner = new Scanner(System.in);
T = scanner.nextInt();
for(int tc = 1; tc <= T; tc++){
nodes = scanner.nextInt();
index = new int[nodes+1];
map = new int[nodes+1][1000];
for(int i = 1; i <= nodes; i++){
edges = scanner.nextInt();
for(int j = 0; j < edges; j++){
map[i][index[i]++] = scanner.nextInt();
}
}
Stack stack = new Stack();
int check = 0;
for(int i = 1; i <= nodes; i++){
check = 0;
stack.reset();
stack.push(i);
visited = new boolean[nodes+1];
while(!stack.is_Empty()){
cnode = stack.pop();
visited[cnode] = true;
for(int k = 0; k < index[cnode]; k++){
if(visited[map[cnode][k]] == false){
stack.push(map[cnode][k]);
}
else{
check = 1;
break;
}
}
if(check == 1) break;
}
if(check == 1) break;
}
System.out.println("Case #"+ tc);
if(check == 1){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
}
}
// Contact
package Pratice_Code;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Contact_2 {
static int N, x;
static int node[][];
static boolean visited[];
public static class Queue{
int Data[];
int front, rear;
public Queue() {
this.front = this.rear = -1;
Data = new int[1000];
}
public void reset(){
this.front = this.rear = -1;
}
public void enQueue(int value){
Data[++rear] = value;
}
public int deQueue(){
return Data[++front];
}
public boolean is_Empty(){
if(this.front == this.rear) return true;
return false;
}
}
public static void main(String[] args) throws FileNotFoundException{
System.setIn(new FileInputStream("input8.txt"));
Scanner scanner = new Scanner(System.in);
for(int tc = 1; tc <= 10; tc++){
int max = 0;
Queue mQueue = new Queue();
N = scanner.nextInt();
node = new int[N/2][2];
x = scanner.nextInt();
mQueue.enQueue(x);
visited = new boolean[1000];
for(int i = 0; i < N/2; i++){
node[i][0] = scanner.nextInt();
node[i][1] = scanner.nextInt();
}
while(!mQueue.is_Empty()){
int value = 0;
int sizeQueue = mQueue.rear - mQueue.front;
for(int i = 0; i < sizeQueue; i++){
int cPoint = mQueue.deQueue();
value = value > cPoint ? value : cPoint;
for(int k = 0; k < N/2; k++){
if(cPoint == node[k][0] && visited[node[k][1]] == false){
mQueue.enQueue(node[k][1]);
visited[node[k][1]] = true;
}
}
}
if(value != 0) max = value;
}
System.out.println("#" + tc + " " + max);
}
}
}
// Sinh nhi phan
package Lesson_12;
public class Sinh_Chuoi_Nhi_Phan {
static int N, arr[];
public static void backTrack(int index){
if(index == N){
for(int i = 0; i < N; i++){
System.out.print(arr[i]);
}
System.out.println();
return;
}
// Co the dung for(int k = 0; k < 2; k++)
arr[index] = 0;
backTrack(index+1);
arr[index] = 1;
backTrack(index+1);
}
}
// Sinh Hoan vi
package Lesson_12;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Sinh_Hoan_Vi {
static int arr[], T, N;
static boolean visited[];
public static void backTrack(int index){
if(index == N){
for(int i = 0; i < N; i++){
System.out.print(arr[i]);
}
System.out.println();
return;
}
for(int i = 1; i <= N; i++){
if(!visited[i]){
visited[i] = true;
arr[index] = i;
backTrack(index+1);
visited[i] = false;
}
}
}
public static void main(String[] args) throws FileNotFoundException{
System.setIn(new FileInputStream("Hoan_vi_BackTrack"));
Scanner scanner = new Scanner(System.in);
T = scanner.nextInt();
for(int tc = 1; tc <= T; tc++){
N = scanner.nextInt();
arr = new int[N];
visited = new boolean[N+1];
}
backTrack(0);
}
}
// Sinh to hop
package Lesson_12;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Sinh_To_Hop {
static int arr[], T, N, length;
public static void backTrack(int index,int start){
if(index == length){
for(int i = 0; i < length; i++){
System.out.print(arr[i]);
}
System.out.println();
return;
}
for(int i = start; i <= N; i++){
arr[index] = i;
backTrack(index+1, i+1);
}
}
public static void main(String[] args) throws FileNotFoundException{
System.setIn(new FileInputStream("To_hop_BackTrack"));
Scanner scanner = new Scanner(System.in);
T = scanner.nextInt();
for(int tc = 1; tc <= T; tc++){
N = scanner.nextInt();
length = scanner.nextInt();
arr = new int[N];
}
backTrack(0, 1);
}
}
Editor is loading...
Leave a Comment