Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- import java.awt.geom.Line2D;
- import java.awt.geom.QuadCurve2D;
- class DrawH{
- public static void main(String[] args) {
- if(args[1] == null){
- System.out.println("You have to input a second argument that is an integer from 3 to 20.\n");
- return;
- }
- int L = Integer.parseInt(args[1]);
- while ((L <= 20) && (L >= 3)) {
- if (args[0] == null || args[1] == null) {
- System.out.println("The first parameter needs to be a character from 'c,w,f,g' and the second an integer from 3 to 20.\n");
- return;
- }
- if (args[0].equals("c")) {
- console(L);
- Scanner in = new Scanner(System.in);
- L = in.nextInt();
- } else if (args[0].equals("w")) {
- window(L);
- L = Integer.parseInt(JOptionPane.showInputDialog(
- "Give me a number ",4));
- } else if (args[0].equals("g")) {
- graphics(L);
- Scanner in = new Scanner(System.in);
- L = in.nextInt();
- } else if (args[0].equals("f")) {
- file(L);
- Scanner in = new Scanner(System.in);
- L = in.nextInt();
- } else {
- System.out.println("The first parameter needs to be a character from 'c,w,f,g'");
- break;
- }
- }
- if ((L > 20) || (L < 3)) {
- System.out.println("The second argument needs to be an integer from 3 to 20.\n");
- }
- }
- public static void console(int height){
- for(int i = 0; i<height; i++){
- if(i == (height/2) ){
- System.out.print("\n****");
- }else{
- System.out.print("\n* *");
- }
- }
- }
- public static void window(int height){
- String out = "";
- for(int i = 0; i<height; i++){
- if(i == (height/2) + 1){
- out = out + "****\n";
- }else{
- out = out + "* *\n";
- }
- }
- JOptionPane.showMessageDialog(null, out, "Output window",JOptionPane.INFORMATION_MESSAGE);
- }
- public static void file(int height){
- PrintWriter writer;
- try {
- writer = new PrintWriter("C:\\temp\\H.html", "UTF-8");
- writer.println("<!DOCTYPE html>\n" +
- "<html>\n" +
- "<head>\n" +
- "<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"/>\n" +
- "</head>\n" +
- "<body><font size=\""+ height + "\">" + "H with font size: "+ height + "</font></body>\n" +
- "</html>");
- writer.close();
- } catch (Exception e) {
- System.out.println("Problem: "+e);
- }
- }
- static void graphics(int height) {
- int mid_point = (height*19 - 50)/2;
- Frame f = new Frame("Painting H") {
- public void paint (Graphics g) {
- Graphics2D g2 = (Graphics2D) g;
- QuadCurve2D q = new QuadCurve2D.Double();
- g2.draw(new Line2D.Double(50, 50, 50, height*19)); // a line
- g2.draw(new Line2D.Double(200, 50, 200, height*19));
- g2.draw(new Line2D.Double(50, height*19 - mid_point, 200, height*19 - mid_point));
- q.setCurve(200, height*19, 225, height*19 - mid_point/3, 250, height*19);
- g2.draw(q);
- }
- };
- f.setSize(400,400);
- f.setVisible(true);
- }
- }*/
- /*
- package csd.uoc.gr.A12;
- import java.util.Arrays;
- import java.util.Scanner;
- public class MagicSquareChecker{
- private static int n;
- public static void main(String[] args){
- System.out.println("Please input the number of n(from 2 to 10).");
- Scanner in = new Scanner(System.in);
- n = in.nextInt();
- if((n < 2) || (n>10)){
- System.out.println("n should be a number from 2 to 10.");
- return;
- }
- int[][] MagicSquare = new int[n][n];
- for(int i =1; i <= n; i++){
- for(int j=1; j <= n; j++){
- System.out.println("Please type the number that goes in the position: [" + i + "]["+ j + "]." );
- MagicSquare[i-1][j-1] = in.nextInt();
- }
- }
- for(int i =0; i < MagicSquare.length; i++){
- System.out.println(toString2(MagicSquare[i]));
- }
- boolean k = checkIsMagic(MagicSquare);
- if(k){
- System.out.println("The square is magic and the magic number is: "+ getMagicNumber(MagicSquare)+" !");
- }else{
- System.out.println("The square is isn't magic!");
- }
- return;
- }
- public static boolean checkIsMagic(int[][] s){
- int magic_num = getMagicNumber(s);
- if(hasDuplicates(s)){
- System.out.println("The matrix you inputted contains duplicates.");
- return false;
- }
- for(int i =0; i < s.length; i++){
- int sum = sumOfRow(s,i);
- if(magic_num != sum){
- System.out.println("Sum of Row "+ i+1 +" isn't equal to the magic number.");
- return false;
- }
- }
- for(int i =0; i < s.length; i++){
- int sum = sumOfCollumn(s,i);
- if(magic_num != sum){
- System.out.println("Sum of Column "+ i+1 +" isn't equal to the magic number.");
- return false;
- }
- }
- int sum = sumOfDiagonal1(s);
- if(magic_num != sum){
- System.out.println("Sum of Diagonal1 isn't equal to the magic number.");
- return false;
- }
- sum = sumOfDiagonal2(s);
- if(magic_num != sum){
- System.out.println("Sum of Diagonal2 isn't equal to the magic number.");
- return false;
- }
- return true;
- }
- private static int sumOfRow (int[][] s, int k){
- int sum = 0;
- int i =0;
- do{
- sum += s[k][i];
- i++;
- }while(i < s.length);
- return sum;
- }
- private static int sumOfCollumn (int[][] s, int k){
- int sum = 0;
- int i =0;
- do{
- sum += s[i][k];
- i++;
- }while(i < s.length);
- return sum;
- }
- private static int sumOfDiagonal1 (int[][] s){
- int sum = 0;
- int i =0;
- do{
- sum += s[i][i];
- i++;
- }while(i < s.length);
- return sum;
- }
- private static int sumOfDiagonal2 (int[][] s){
- int sum = 0;
- int i = 0;
- int k = s.length -1;
- do {
- sum += s[i][k];
- i++;
- k--;
- }while(k !=-1);
- return sum;
- }
- public static int getMagicNumber(int[][] s){
- return s.length*(((s.length*s.length)+1)/2);
- }
- private static boolean hasDuplicates(int[][] s){
- for(int i = 0; i < s.length; i++){
- for(int j = 0; j < s.length; j++){
- int same = s[i][j];
- for(int k =0; k < s.length; k++){
- for(int m =0; m < s.length; m++){
- if(k == i && m == j ){
- if(m == s.length-1 && k != s.length-1){
- k++;
- m=0;
- }else if(m != s.length-1 && k == s.length-1){
- m++;
- }else{
- break;
- }
- }
- if(same == s[k][m]){
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- public static String toString2(int[] a) {
- if (a == null)
- return "null";
- int iMax = a.length - 1;
- if (iMax == -1)
- return "[]";
- StringBuilder b = new StringBuilder();
- b.append('[');
- for (int i = 0; ; i++) {
- b.append(a[i]);
- if (i == iMax)
- return b.append(']').toString();
- b.append("| ");
- }
- }
- }
- */
- /*package csd.uoc.gr.A13;
- import csd.uoc.gr.A12.MagicSquareChecker;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.swing.JOptionPane;
- import javax.swing.*;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Scanner;
- class MagicSquareCheckerFile {
- public static void main(String[] args) throws IOException {
- System.out.println("Would you like to choose a file or download one?\nPress \"1\" to choose a file or \"2\" to download a file and hit enter.");
- int choice;
- Scanner in = new Scanner(System.in);
- choice = in.nextInt();
- in.close();
- String[] all = new String[0];
- if(choice < 1 || choice > 2){
- System.out.println("Wrong input!");
- System.exit(1);
- }
- String filepath = "";
- if(choice == 1){
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Select a file");
- int userSelection = fileChooser.showSaveDialog(null);
- if (userSelection == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- filepath = file.getAbsolutePath();
- System.out.println("The path of the selected file is: " + filepath);
- all = fillArray(filepath);
- }
- }else {
- filepath =
- JOptionPane.showInputDialog("The URL is: ","");
- System.out.println(filepath);
- all = downloadANDwrite(filepath);
- }
- int commas = 0;
- char c;
- int index = 0;
- ArrayList<Integer>numOfcommas = new ArrayList<>();
- for(int i=0;i<all.length;i++) {
- commas = 0;
- for (int j = 0; j < all[i].length(); j++) {
- c = all[i].charAt(j);
- if(c == ','){
- commas++;
- }
- if(j == all[i].length()-1){
- numOfcommas.add(index,commas);
- index++;
- }
- }
- }
- for(int i=0;i< numOfcommas.size();i++){
- if(i <= numOfcommas.size()-2) {
- if (!numOfcommas.get(i).equals(numOfcommas.get(i + 1))) {
- System.out.println("The structure of the matrix is not appropriate");
- System.exit(69420);
- }
- }
- }
- int size = all.length;
- int[][] MagicSquare = new int[size][size];
- StringBuilder s1 = new StringBuilder();
- int counter = 0;
- int k;
- StringBuilder s;
- ArrayList<Integer>Numbers = new ArrayList<>();
- for (int i = 0; i < size; i++) {
- s = new StringBuilder();
- for(int j=0; j < all[i].length(); j++){
- c = all[i].charAt(j);
- if( c == ',' || c==' ' || c=='\n'){
- int L = Integer.parseInt(s.toString());
- Numbers.add(counter,L);
- counter++;
- s = new StringBuilder();
- }else{
- s.append(c);
- if(j == all[i].length()-1){
- int L = Integer.parseInt(s.toString());
- Numbers.add(counter,L);
- counter++;
- s = new StringBuilder();
- }
- }
- }
- }
- counter =0;
- for(int i = 0;i<size;i++){
- for(int j =0; j<size; j++){
- MagicSquare[i][j] = Numbers.get(counter);
- counter++;
- }
- }
- boolean check = MagicSquareChecker.checkIsMagic(MagicSquare);
- if(check){
- String newfilepath = "";
- System.out.println("The square is magic and the magic number is: " + MagicSquareChecker.getMagicNumber(MagicSquare)+".");
- JFileChooser fileChooser = new JFileChooser();
- fileChooser.setDialogTitle("Select a folder");
- fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
- int userSelection = fileChooser.showSaveDialog(null);
- if (userSelection == JFileChooser.APPROVE_OPTION) {
- File file = fileChooser.getSelectedFile();
- newfilepath = file.getAbsolutePath();
- System.out.println("The path of the selected file is: " + newfilepath);
- }
- System.out.println(newfilepath);
- System.exit(1);
- PrintWriter writer;
- try {
- writer = new PrintWriter(newfilepath,"UTF-8");
- for(int i =0; i < MagicSquare.length; i++){
- writer.println(toString2(MagicSquare[i]));
- }
- writer.println("Magic square with magic number "+ MagicSquareChecker.getMagicNumber(MagicSquare));
- writer.close();
- } catch (Exception e) {
- System.out.println("Problem: "+e);
- }
- }else{
- System.out.println("The square isn't magic.");
- }
- }
- public static String[] fillArray(String filename) throws FileNotFoundException {
- Scanner inputFile = new Scanner(new File(filename));
- int count = 0;
- while (inputFile.hasNextLine()) {
- inputFile.nextLine();
- count++;
- }
- inputFile.close();
- String[] array = new String[count];
- inputFile = new Scanner(new File(filename));
- for (int i = 0; i < count; i++) {
- array[i] = inputFile.nextLine();
- }
- inputFile.close();
- return array;
- }
- public static String toString2(int[] a) {
- if (a == null)
- return "null";
- int iMax = a.length - 1;
- if (iMax == -1)
- return "[]";
- StringBuilder b = new StringBuilder();
- b.append('[');
- for (int i = 0; ; i++) {
- b.append(a[i]);
- if (i == iMax)
- return b.append(']').toString();
- b.append("| ");
- }
- }
- static String[] downloadANDwrite(String address) throws IOException {
- int count = 0;
- URL url = new URL(address);
- Scanner in= new Scanner(
- new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
- while (in.hasNextLine()) {
- in.nextLine();
- count++;
- }
- in.close();
- String[] array = new String[count];
- in= new Scanner(
- new InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
- for (int i = 0; i < count; i++) {
- array[i] = in.nextLine();
- }
- in.close();
- return array;
- }
- }
- */
- */
- package csd.uoc.gr.A14;
- import csd.uoc.gr.A12.MagicSquareChecker;
- import java.util.Random;
- import java.util.Scanner;
- import java.util.stream.IntStream;
- class BacktrackingAlgorithm {
- private static final int BOARD_SIZE = 9;
- private static final int SUBSECTION_SIZE = 3;
- private static final int BOARD_START_INDEX = 0;
- private static final int NO_VALUE = 0;
- private static final int MIN_VALUE = 1;
- private static final int MAX_VALUE = 9;
- private static int[][] board = {
- {8, 0, 0, 0, 0, 0, 0, 0, 0},
- {0, 0, 3, 6, 0, 0, 0, 0, 0},
- {0, 7, 0, 0, 9, 0, 2, 0, 0},
- {0, 5, 0, 0, 0, 7, 0, 0, 0},
- {0, 0, 0, 0, 4, 5, 7, 0, 0},
- {0, 0, 0, 1, 0, 0, 0, 3, 0},
- {0, 0, 1, 0, 0, 0, 0, 6, 8},
- {0, 0, 8, 5, 0, 0, 0, 1, 0},
- {0, 9, 0, 0, 0, 0, 4, 0, 0}
- };
- public static void main(String[] args) {
- System.out.println("How many boards you want to create?");
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- gameMake(n);
- }
- private void printBoard() {
- for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
- for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
- System.out.print(board[row][column] + " ");
- }
- System.out.println();
- }
- }
- private static boolean solve(int[][] board) {
- for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
- for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
- if (board[row][column] == NO_VALUE) {
- for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
- board[row][column] = k;
- if (isValid(board, row, column) && solve(board)) {
- return true;
- }
- board[row][column] = NO_VALUE;
- }
- return false;
- }
- }
- }
- return true;
- }
- private static boolean isValid(int[][] board, int row, int column) {
- return rowConstraint(board, row) &&
- columnConstraint(board, column) &&
- subsectionConstraint(board, row, column);
- }
- private static boolean subsectionConstraint(int[][] board, int row, int column) {
- boolean[] constraint = new boolean[BOARD_SIZE];
- int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
- int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
- int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
- int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
- for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
- for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
- if (!checkConstraint(board, r, constraint, c)) return false;
- }
- }
- return true;
- }
- private static boolean columnConstraint(int[][] board, int column) {
- boolean[] constraint = new boolean[BOARD_SIZE];
- return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
- .allMatch(row -> checkConstraint(board, row, constraint, column));
- }
- private static boolean rowConstraint(int[][] board, int row) {
- boolean[] constraint = new boolean[BOARD_SIZE];
- return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
- .allMatch(column -> checkConstraint(board, row, constraint, column));
- }
- private static boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) {
- if (board[row][column] != NO_VALUE) {
- if (!constraint[board[row][column] - 1]) {
- constraint[board[row][column] - 1] = true;
- } else {
- return false;
- }
- }
- return true;
- }
- private static boolean isValidBoard(int[][] brd){
- for(int i = 0; i < BOARD_SIZE; i++){
- for(int j = 0; j < BOARD_SIZE; j++){
- int same = brd[i][j];
- for(int k =0; k < BOARD_SIZE; k++){
- for(int m =0; m < BOARD_SIZE; m++){
- if(k == i && m == j ){
- if(m == BOARD_SIZE-1 && k != BOARD_SIZE-1){
- k++;
- m=0;
- }else if(m != BOARD_SIZE-1 && k == BOARD_SIZE-1){
- m++;
- }else{
- break;
- }
- }
- if((same == brd[k][m] && same !=0) || same < 0 || same > 9){
- return true;
- }
- }
- }
- }
- }
- int sub_counter;
- for(sub_counter = BOARD_SIZE/SUBSECTION_SIZE;sub_counter >0; sub_counter--) {
- int i = (SUBSECTION_SIZE * sub_counter)-1;
- int j = (SUBSECTION_SIZE * sub_counter)-1;
- int k = (SUBSECTION_SIZE * sub_counter)-1;
- int m = (SUBSECTION_SIZE * sub_counter)-1;
- while (i >= SUBSECTION_SIZE * (sub_counter-1)) {
- while (j >= SUBSECTION_SIZE * (sub_counter-1)) {
- int same = brd[i][j];
- while (k >= SUBSECTION_SIZE * (sub_counter-1)) {
- while (m >= SUBSECTION_SIZE * (sub_counter-1)) {
- if(i==k && j==m){
- if(k != SUBSECTION_SIZE * (sub_counter-1) && m == SUBSECTION_SIZE * (sub_counter-1)){
- k--;
- m = (SUBSECTION_SIZE * sub_counter)-1;
- }else if(k == SUBSECTION_SIZE * (sub_counter-1) && m != SUBSECTION_SIZE * (sub_counter-1)){
- m--;
- }
- }
- if((same == brd[k][m] && same !=0) || same < 0 || same > 9){
- return true;
- }
- m--;
- }
- k--;
- }
- j--;
- }
- i--;
- }
- }
- return false;
- }
- private static boolean isSolvableBoard(int[][] brd){
- return solve(brd) && isValidBoard(brd);
- }
- private static int[][] makeBoard(int x){
- if(x >= 81){
- System.out.println("At least one cell must have a value from 1 to 9.");
- System.exit(0);
- }
- int[][] newBoard = new int[9][9];
- Random randomGenerator = new Random();
- for(int filled = 0; filled < 81 - x; filled++){
- int randomi = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- int randomj = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- while(newBoard[randomi-1][randomj-1] != 0){
- randomi = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- randomj = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- }
- int randomvalue = randomGenerator.nextInt(MAX_VALUE-MIN_VALUE+1) + MIN_VALUE;
- newBoard[randomi-1][randomj-1] = randomvalue;
- }
- return newBoard;
- }
- private static void gameMake(int n){
- int wrong = 0;
- int right = 0;
- int unsolvable = 0;
- int[][] newBoard;
- System.out.println("How many empty cells per board?");
- Scanner in = new Scanner(System.in);
- int x = in.nextInt();
- long start = System.currentTimeMillis();
- int i =1;
- while(n >= i){
- newBoard = makeBoard(x);
- int[][] newBoard2 = newBoard;
- for(int k =0; k < newBoard2.length; k++){
- for(int m = 0; m < newBoard2.length; m++){
- System.out.print(newBoard2[k][m]+" ");
- }
- System.out.println();
- }
- CHECK:
- if(isValidBoard(newBoard)){
- if(!isSolvableBoard(newBoard)){
- System.out.println("Unsolvable!");
- unsolvable++;
- i--;
- break CHECK;
- }
- System.out.println("Board #"+i);
- for(int k =0; k < newBoard2.length; k++){
- for(int m = 0; m < newBoard2.length; m++){
- System.out.print(newBoard2[k][m]+" ");
- }
- System.out.println();
- }
- System.out.println("The solution of Board #"+i);
- for(int k =0; k < newBoard.length; k++){
- for(int m = 0; m < newBoard.length; m++){
- System.out.print(newBoard[k][m]+" ");
- }
- System.out.println();
- }
- System.out.println();
- right++;
- }else if(!isValidBoard(newBoard)){
- System.out.println("Wrong!");
- wrong++;
- i--;
- }
- i++;
- }
- long end = System.currentTimeMillis();
- float sec = (end - start) / 1000F; System.out.println(sec + " seconds");
- System.out.println("Empty cells per board : "+ x);
- System.out.println("Valid boards created : "+ right);
- System.out.println("Invalid boards created : "+ wrong);
- System.out.println("Invalid boards created : "+ unsolvable);
- System.out.println("Elapsed time in seconds : "+ sec);
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement