-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dimitris Bekris
committed
Mar 8, 2023
1 parent
f789421
commit aa5122a
Showing
58 changed files
with
1,498,057 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import java.util.*; | ||
|
||
|
||
public class MyPoint { | ||
|
||
private int X; | ||
private int Y; | ||
|
||
public MyPoint(int x, int y){ | ||
X = x; | ||
Y = y; | ||
} | ||
|
||
public MyPoint(MyPoint p){ | ||
X = p.getX(); | ||
Y = p.getY(); | ||
} | ||
|
||
public int getX(){return X;} | ||
|
||
public int getY(){return Y;} | ||
|
||
@Override | ||
public boolean equals (Object o){ | ||
if(this == o) return true; | ||
if(o == null || getClass() != o.getClass()) return false; | ||
MyPoint other = (MyPoint) o; | ||
return getX() == other.getX() && getY() == other.getY(); | ||
} | ||
|
||
@Override | ||
public int hashCode(){ | ||
return Objects.hash(X, Y); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "(x = " + X + ", y = " + Y + ")" ; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import java.util.*; | ||
import java.lang.*; | ||
|
||
public class PoluteState implements State{ | ||
private static Integer N; | ||
private static Integer M; | ||
private Integer coor_x; | ||
private Integer coor_y; | ||
private State previous; | ||
private Integer time; | ||
private Boolean sea; | ||
private Boolean plane; | ||
private static Set<MyPoint> seas; | ||
private static Set<MyPoint> planes; | ||
|
||
public boolean isPlane(){return plane;} | ||
|
||
public static void initialize_rows(Integer n){N = n;} | ||
|
||
public static void initialize_columns(Integer m){M = m;} | ||
|
||
public static void initialize_seas(Set<MyPoint> s){seas = s;} | ||
|
||
public static void initialize_planes(Set<MyPoint> pl){planes = pl;} | ||
|
||
public int getCoor_x(){return coor_x;} | ||
|
||
public int getCoor_y(){return coor_y;} | ||
|
||
public int gettime(){return time;} | ||
|
||
public PoluteState(SwterState s){ | ||
coor_x = s.getcoor_x(); | ||
coor_y = s.getcoor_y(); | ||
previous = null; | ||
time = s.gettime(); | ||
sea = null; | ||
plane = null; | ||
|
||
} | ||
|
||
public PoluteState(Integer x, Integer y, Integer t, State p, Boolean Sea, Boolean Plane){ | ||
coor_x = x; | ||
coor_y = y; | ||
time = t; | ||
previous = p; | ||
sea = Sea; | ||
plane = Plane; | ||
} | ||
|
||
@Override | ||
public boolean isFinal(){ | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isBad(){ | ||
return coor_x < 0 || coor_x > N-1 || coor_y < 0 || coor_y > M-1 || sea; | ||
} | ||
|
||
@Override | ||
public State getPrevious(){ | ||
return previous; | ||
} | ||
|
||
|
||
@Override | ||
public Collection<State> next(){ | ||
Collection<State> states = new ArrayList<>(); | ||
states.add(new PoluteState(coor_x-1,coor_y, time+2, this, seas.contains(new MyPoint (coor_x-1,coor_y)),planes.contains(new MyPoint (coor_x-1,coor_y)))); | ||
states.add(new PoluteState(coor_x+1,coor_y, time+2, this, seas.contains(new MyPoint (coor_x+1,coor_y)),planes.contains(new MyPoint (coor_x+1,coor_y)))); | ||
states.add(new PoluteState(coor_x,coor_y-1, time+2, this, seas.contains(new MyPoint (coor_x,coor_y-1)),planes.contains(new MyPoint (coor_x,coor_y-1)))); | ||
states.add(new PoluteState(coor_x,coor_y+1, time+2, this, seas.contains(new MyPoint (coor_x,coor_y+1)),planes.contains(new MyPoint (coor_x,coor_y+1)))); | ||
return states; | ||
} | ||
|
||
@Override | ||
public boolean equals (Object o){ | ||
if(this == o) return true; | ||
if(o == null || getClass() != o.getClass()) return false; | ||
PoluteState other = (PoluteState) o; | ||
return coor_x == other.getCoor_x() && coor_y == other.getCoor_y() && time >= other.gettime(); | ||
} | ||
|
||
@Override | ||
public int hashCode(){ | ||
return Objects.hash(coor_x, coor_y); | ||
} | ||
|
||
@Override | ||
public String toString(){ | ||
return "(coor_x = " + coor_x + ", coor_y = " + coor_y + ", polution time = " + time + ")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import java.util.*; | ||
import java.util.Collection; | ||
|
||
public class Polution implements Solver { | ||
|
||
@Override | ||
public Set<State> solve(State initial, Set<MyPoint> Planes){ | ||
Set<State> poluted = new HashSet<>(); | ||
Queue<State> deque = new ArrayDeque<>(); | ||
deque.add(initial); | ||
poluted.add(initial); | ||
State s; | ||
|
||
while(!deque.isEmpty()){ | ||
|
||
s = deque.remove(); | ||
for (State n : s.next()){ | ||
if(!n.isBad()){ | ||
if (!poluted.contains(n)){ | ||
poluted.remove(n); | ||
if(n.isPlane()){ | ||
PoluteState N = (PoluteState) n; | ||
PoluteState Initial = (PoluteState) initial; | ||
for(MyPoint p : Planes){ | ||
if((int)p.getX() != N.getCoor_x() && (int)p.getY() != N.getCoor_y()){ | ||
deque.add(new PoluteState((int)p.getX(),(int)p.getY(), N.gettime()+5, n, false,false)); | ||
poluted.add(new PoluteState( (int)p.getX(),(int)p.getY(), N.gettime()+5, n, false,false)); | ||
} | ||
} | ||
} | ||
deque.add(n); | ||
poluted.add(n); | ||
} | ||
} | ||
|
||
} | ||
|
||
} | ||
return poluted; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java.util.*; | ||
|
||
public interface Solver { | ||
public Set<State> solve(State initial, Set<MyPoint> Planes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import java.util.Collection; | ||
|
||
public interface State{ | ||
public boolean isFinal(); | ||
public boolean isBad(); | ||
public Collection<State> next(); | ||
public State getPrevious(); | ||
public boolean isPlane(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import java.io.*; | ||
import javax.print.attribute.ResolutionSyntax; | ||
import java.util.*; | ||
import java.lang.*; | ||
|
||
public class Stayhome { | ||
private Set<MyPoint> seas = new HashSet<>(); | ||
private Set<MyPoint> airplanes = new HashSet<>(); | ||
private Queue<MyPoint> virus = new LinkedList<>(); | ||
private Queue<MyPoint> home = new LinkedList<>(); | ||
private Queue<MyPoint> swter = new LinkedList<>(); | ||
private int rows; | ||
private int columns; | ||
|
||
|
||
public static void main(String args[]) throws Exception{ | ||
try{ | ||
Stayhome object = new Stayhome(); | ||
object.run(args); | ||
MyPoint coor_virus = new MyPoint(object.virus.remove()); | ||
MyPoint coor_swter = new MyPoint(object.swter.remove()); | ||
MyPoint coor_home = new MyPoint(object.home.remove()); | ||
Solver Virus = new Polution(); | ||
State initial = new PoluteState(new Integer(coor_virus.getX()),new Integer(coor_virus.getY()), new Integer(0), | ||
null, new Boolean(false), new Boolean(false)); | ||
PoluteState.initialize_rows(new Integer(object.rows)); | ||
PoluteState.initialize_columns(new Integer(object.columns)); | ||
PoluteState.initialize_seas(object.getSeas()); | ||
PoluteState.initialize_planes(object.getPlanes()); | ||
|
||
Set<State> poluted_map = Virus.solve(initial, object.airplanes); | ||
SwterState Final = new SwterState(new Integer(coor_home.getX()), new Integer(coor_home.getY()), new Integer(0), null, null,false); | ||
State Initial = new SwterState(new Integer(coor_swter.getX()), new Integer(coor_swter.getY()), new Integer(0), null, null, new Boolean(false)); | ||
Solver swter = new Swter(); | ||
SwterState.initialize_rows(new Integer(object.rows)); | ||
SwterState.initialize_columns(new Integer(object.columns)); | ||
SwterState.initialize_seas(object.getSeas()); | ||
SwterState.initialize_poluted(poluted_map); | ||
SwterState.initialize_final(Final); | ||
Set<State> path = swter.solve(Initial, null); | ||
if(path == null){ | ||
System.out.println("IMPOSSIBLE"); | ||
return; | ||
} | ||
SwterState F = object.find_final(path,Final); | ||
Deque<Character> Path = new ArrayDeque<>(); | ||
Path.addFirst(F.getPathName()); | ||
F = (SwterState)F.getPrevious(); | ||
while(!F.equals(Initial)){ | ||
Path.addFirst(F.getPathName()); | ||
F = (SwterState)F.getPrevious(); | ||
} | ||
System.out.println(Path.size()); | ||
object.print(Path); | ||
return; | ||
} | ||
catch(Exception e){ | ||
System.out.println("Problem"); | ||
} | ||
} | ||
|
||
public void print(Deque<Character> Path){ | ||
for(Character ch : Path){ | ||
System.out.print(ch); | ||
} | ||
System.out.println(); | ||
} | ||
|
||
public SwterState find_final(Set<State> path, SwterState Final){ | ||
|
||
for(State s : path){ | ||
|
||
SwterState S = (SwterState) s; | ||
if(S.equals(Final)){ | ||
return S; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
|
||
public Set<MyPoint> getSeas(){return seas;} | ||
|
||
public Set<MyPoint> getPlanes(){return airplanes;} | ||
|
||
public void run(String args [])throws IOException{ | ||
try{BufferedReader reader = | ||
new BufferedReader ( new FileReader ( args[0] ) ) ; | ||
|
||
|
||
int c = 0; | ||
int r = 0; | ||
int k; | ||
|
||
while((k = reader.read()) != -1){ | ||
char ch = (char) k; | ||
if (ch == '\n'){ | ||
if (r == 0) | ||
columns = c; | ||
r+=1; | ||
c = 0; | ||
continue; | ||
} | ||
if (ch == 'W'){ | ||
virus.add(new MyPoint(r,c)); | ||
} | ||
else if (ch == 'A'){ | ||
airplanes.add(new MyPoint(r,c)); | ||
} | ||
else if (ch == 'T'){ | ||
home.add(new MyPoint(r,c)); | ||
} | ||
else if (ch == 'S'){ | ||
swter.add(new MyPoint(r,c)); | ||
} | ||
else if (ch == 'X'){ | ||
seas.add(new MyPoint(r,c)); | ||
} | ||
c+=1; | ||
} | ||
reader.close(); | ||
rows = r; | ||
} | ||
catch(IOException e){ | ||
System.out.println("Error. Usage: java Stayhome inputfile"); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import java.util.*; | ||
|
||
public class Swter implements Solver{ | ||
public Set<State> solve (State initial, Set<MyPoint> Planes){ | ||
Set<State> path = new HashSet<>(); | ||
Queue<State> deque = new ArrayDeque<>(); | ||
deque.add(initial); | ||
path.add(initial); | ||
State s; | ||
while(!deque.isEmpty()){ | ||
s = deque.remove(); | ||
for(State n : s.next()){ | ||
|
||
if(!n.isBad()){ | ||
if (!path.contains(n)){ | ||
|
||
deque.add(n); | ||
path.add(n); | ||
|
||
if(n.isFinal()){ | ||
return path;} | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
Oops, something went wrong.