Skip to content

Commit

Permalink
Merge pull request ReciHub#404 from mostefa-laoumir/seven-bridges-pro…
Browse files Browse the repository at this point in the history
…blem-java

added new folder called java and added solution of (the seven bridges of königsberg) problem in it
  • Loading branch information
OtacilioN authored Oct 21, 2019
2 parents 256e7d2 + 4199909 commit 74709d2
Show file tree
Hide file tree
Showing 13 changed files with 278 additions and 0 deletions.
6 changes: 6 additions & 0 deletions java/The-seven-bridges-of-Königsberg/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
this is a simple java solution of the famous "The seven bridges of K�nigsberg" problem written by me



thank you!
by mostefa_laoumir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions java/The-seven-bridges-of-Königsberg/THG_Project/THG_Project.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class Bridge {

public Vertige v1, v2;
public Bridge(Vertige v1, Vertige v2){

this.v1 = v1;
this.v2 = v2;
}
}
159 changes: 159 additions & 0 deletions java/The-seven-bridges-of-Königsberg/THG_Project/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import java.util.Scanner;
import java.util.Vector;

public class Main {

public static void main(String[] args) {
Vector<Vertige> vtgs = new Vector<>();
Vector<Bridge> bdgs = new Vector<>();
Scanner scn = new Scanner(System.in);
System.out.println("Donner le nombre de vertiges : ");
int n = scn.nextInt();
Vertige v;
String nm;
for(int i = 0 ; i<n;i++){
System.out.println("donner le nom du vertige num "+(i+1)+" : ");
nm = scn.next();
v = new Vertige(nm);
vtgs.add(v);
}
System.out.println("donner le nombres de ponts : ");
int ponts = scn.nextInt();
Vertige v1 = new Vertige("vide");
v = new Vertige("vide");
Bridge b;
String nm2, nm3 = "";
for(int i = 0; i<ponts;i++){
System.out.println("pont "+(i+1)+" : ");
System.out.println("donner le nom du premiere vertige reliée : ");
nm2 = "";
nm3 = "";
nm2 = scn.next();
for(int j = 0 ;j<n;j++){
if (vtgs.elementAt(j).nom.equals(nm2)){
v = new Vertige(vtgs.elementAt(j));
vtgs.elementAt(j).path++;

}
}
System.out.println("donner le nom du deuxieme vertige reliée : ");
do{
nm3 = scn.next();
}while(nm3.equals(nm2));
for(int j = 0 ;j<n;j++){
if (vtgs.elementAt(j).nom.equals(nm3)){
v1 = new Vertige(vtgs.elementAt(j));
vtgs.elementAt(j).path++;
}
}
b = new Bridge(v,v1);
bdgs.add(b);
}




/*for(int i = 0 ; i<n; i++){
System.out.println(vtgs.elementAt(i).nom+ " : " +vtgs.elementAt(i).path);
}*/
boolean works = true;

for(int j = 0 ; j<n; j++){
if(vtgs.elementAt(1).path!=vtgs.elementAt(j).path)works=false;
}
if(!works){
int t[][] = new int[2][n];
for(int i = 0 ; i<n; i++){
t[0][i] = vtgs.elementAt(i).path;
t[1][i]=0;
}
for(int i = 0 ; i<n; i++){
for(int j = 0 ; j<n; j++){
if(t[0][i]==t[0][j])t[1][i]++;
}
}
int k = dup.supprimer_dup(t[1],n);

for(int i = 0; i<k;i++){
if(t[1][i]==2)works=true;
}

}
if(works){
System.out.println("Chemin Trouvé");
}else{
System.out.println("Chemin Non Trouvé");
System.out.println("voulez vous ajouter ou supprimer un pont? A/S");
String choix = scn.next();
if(choix.equals("A") || choix.equals("a")){
ponts++;
System.out.println("pont "+(ponts)+" : ");
System.out.println("donner le nom du premiere vertige reliée : ");
nm2 = "";
nm3 = "";
nm2 = scn.next();
for(int j = 0 ;j<n;j++){
if (vtgs.elementAt(j).nom.equals(nm2)){
v = new Vertige(vtgs.elementAt(j));
vtgs.elementAt(j).path++;

}
}
System.out.println("donner le nom du deuxieme vertige reliée : ");
do{
nm3 = scn.next();
}while(nm3.equals(nm2));
for(int j = 0 ;j<n;j++){
if (vtgs.elementAt(j).nom.equals(nm3)){
v1 = new Vertige(vtgs.elementAt(j));
vtgs.elementAt(j).path++;
}
}
b = new Bridge(v,v1);
bdgs.add(b);
}else if(choix.equals("s") || choix.equals("S")){
System.out.println("donner les vertiges qui se relient par le pont : ");
String p1 = scn.next();
String p2 = scn.next();
for(int m = 0;m<ponts;m++){
if(bdgs.elementAt(m).v1.nom.equals(p1) && bdgs.elementAt(m).v2.nom.equals(p2)){
bdgs.remove(m);break;
}
}
for(int q = 0; q<n;q++){
if(vtgs.elementAt(q).nom.equals(p1) || vtgs.elementAt(q).nom.equals(p2))vtgs.elementAt(q).path--;
}
ponts--;
}
works = true;

for(int j = 0 ; j<n; j++){
if(vtgs.elementAt(1).path!=vtgs.elementAt(j).path)works=false;
}
if(!works){
int t[][] = new int[2][n];
for(int i = 0 ; i<n; i++){
t[0][i] = vtgs.elementAt(i).path;
t[1][i]=0;
}
for(int i = 0 ; i<n; i++){
for(int j = 0 ; j<n; j++){
if(t[0][i]==t[0][j])t[1][i]++;
}
}
int k = dup.supprimer_dup(t[1],n);

for(int i = 0; i<k;i++){
if(t[1][i]==2)works=true;
}

}
if(works){
System.out.println("Chemin Trouvé!!!");
}else{
System.out.println("Chemin Non Trouvé!"); }
}


}
}
13 changes: 13 additions & 0 deletions java/The-seven-bridges-of-Königsberg/THG_Project/src/Vertige.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Vertige {
public String nom;
int path=0;
public Vertige(String nom){
this.nom = nom;


}
public Vertige(Vertige v){
this.nom = v.nom;
this.path = v.path;
}
}
20 changes: 20 additions & 0 deletions java/The-seven-bridges-of-Königsberg/THG_Project/src/dup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class dup {
public static int supprimer_dup(int arr[], int n) {
if (n == 0 || n == 1)
return n;

int[] temp = new int[n];
int j = 0;
for (int i = 0; i < n - 1; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];

temp[j++] = arr[n - 1];

// Changing original array
for (int i = 0; i < j; i++)
arr[i] = temp[i];

return j;
}
}

0 comments on commit 74709d2

Please sign in to comment.