diff --git a/.gitignore b/.gitignore index cf3452c5ed9711da7cb48b328277114d87435598..be4e21604f0029f5107fa5b6efaf8f36f0518239 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ gen-branch.js +.DS_Store package.json package-lock.json node_modules diff --git a/Main.java b/Main.java new file mode 100644 index 0000000000000000000000000000000000000000..ac824ab88ca8cf50955cbbb491f124b630262f16 --- /dev/null +++ b/Main.java @@ -0,0 +1,28 @@ +class Main { + public static void main(String[] args) { + // Stack s = new Stack(); + // System.out.println(s.isEmpty()); + // try { + // System.out.println(s.peek()); + // } catch(Exception e){ + // e.printStackTrace(); + // } + // s.push(10); + // s.push(20); + // s.push(30); + // try { + // int v = s.pop(); + // System.out.println(v); + // } catch(Exception e){ + // e.printStackTrace(); + // } + + // int sz = s.size(); + // System.out.println(sz); + + Maze maze = new Maze(); + Tuple start = new Tuple(1,1); + maze.walk(start); + + } +} \ No newline at end of file diff --git a/Maze.java b/Maze.java new file mode 100644 index 0000000000000000000000000000000000000000..5804842bc76b2a6917c3e615b2a5c94427c6d27a --- /dev/null +++ b/Maze.java @@ -0,0 +1,114 @@ +public class Maze { + private boolean[][] maze= { + {false, false, false, false, false, false, false, false}, + {false, true, true, true, true, true, true, false}, + {false, true, false, true, false, false, false, false}, + {false, false, false, true, true, true, true, false}, + {false, true, false, false, false, false, true, false}, + {false, true, false, false, true, false, true, false}, + {false, true, true, true, true, false, true, false}, + {false, false, false, false, false, false, false, false} + }; + private Tuple end; + + Maze(){ + + end = new Tuple(6,6); + } + + public void walk(Tuple start){ + // bad solution + boolean[][] isDiscovered = { + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false}, + {false, false, false, false, false, false, false, false} + }; + + // TupleStack stack = new TupleStack(); + Stack stack = new Stack(); + try { + stack.push(start); + } catch(Exception e){ + e.printStackTrace(); + } + + isDiscovered[start.first][start.second] = true; + + while(!stack.isEmpty()){ + Tuple currentLocation = null; + try { + currentLocation = stack.pop(); + if(currentLocation != null){ + System.out.println(currentLocation.toString()); + } + } catch(Exception e){ + e.printStackTrace(); + } + + // Termination case + if(currentLocation !=null && currentLocation.first == end.first && currentLocation.second == end.second){ + // System.out.println(currentLocation.toString()); + System.out.println("Reached"); + break; + } + + // North + if(currentLocation !=null && currentLocation.first>0){ + if(maze[currentLocation.first-1][currentLocation.second] && !isDiscovered[currentLocation.first-1][currentLocation.second]){ + Tuple temp = new Tuple(currentLocation.first-1, currentLocation.second); + try { + stack.push(temp); + } catch(Exception e){ + e.printStackTrace(); + } + isDiscovered[currentLocation.first-1][currentLocation.second] = true; + } + } + // West + if(currentLocation !=null && currentLocation.second>0){ + if(maze[currentLocation.first][currentLocation.second-1] && !isDiscovered[currentLocation.first][currentLocation.second-1]){ + Tuple temp = new Tuple(currentLocation.first, currentLocation.second-1); + try { + stack.push(temp); + } catch(Exception e){ + e.printStackTrace(); + } + isDiscovered[currentLocation.first][currentLocation.second-1] = true; + } + } + // South + if(currentLocation !=null && currentLocation.first<6){ + if(maze[currentLocation.first+1][currentLocation.second] && !isDiscovered[currentLocation.first+1][currentLocation.second]){ + Tuple temp = new Tuple(currentLocation.first+1, currentLocation.second); + try { + stack.push(temp); + } catch(Exception e){ + e.printStackTrace(); + } + isDiscovered[currentLocation.first+1][currentLocation.second] = true; + } + } + // East + if(currentLocation !=null && currentLocation.second<6){ + if(maze[currentLocation.first][currentLocation.second+1] && !isDiscovered[currentLocation.first][currentLocation.second+1]){ + Tuple temp = new Tuple(currentLocation.first, currentLocation.second+1); + try { + stack.push(temp); + } catch(Exception e){ + e.printStackTrace(); + } + isDiscovered[currentLocation.first][currentLocation.second+1] = true; + } + } + } + + System.out.println("Finished"); + + } + +} \ No newline at end of file diff --git a/Stack.java b/Stack.java new file mode 100644 index 0000000000000000000000000000000000000000..41d19dee026c37020f88a0cb148518307afafdab --- /dev/null +++ b/Stack.java @@ -0,0 +1,43 @@ +public class Stack { + private T[] stack; + private int top; + private static int SIZE = 100; + + Stack(){ + stack = (T[])new Object[SIZE]; + top = 0; + } + + public void push(T value){ + if(top >= SIZE){ + System.out.println("Stack overflow"); + // throw new Exception("Stack Overflow"); + } + stack[top] = value; + top++; + } + + public T pop() throws Exception { + if(top <=0){ + throw new Exception("Stack empty"); + } + top--; + return stack[top]; + } + + public T peek() throws Exception { + if(top <=0){ + throw new Exception("Stack empty"); + } else { + return stack[top-1]; + } + } + + public boolean isEmpty(){ + return top == 0; + } + + public int size(){ + return top; + } +} \ No newline at end of file diff --git a/Tuple.java b/Tuple.java new file mode 100644 index 0000000000000000000000000000000000000000..52765c25aae296e021f76bdcfbe8dfaa1143f63f --- /dev/null +++ b/Tuple.java @@ -0,0 +1,16 @@ +public class Tuple { + + public int first; + + public int second; + + Tuple(int a, int b){ + first = a; + second = b; + } + + public String toString(){ + return "(" + first + ", " + second + ")"; + } + +} \ No newline at end of file diff --git "a/\350\257\276\344\273\266/lecture03-stack-queue.pdf" "b/\350\257\276\344\273\266/lecture03-stack-queue.pdf" new file mode 100644 index 0000000000000000000000000000000000000000..08e8e4e5c3bb10c9fa74da7a70e257118b3b15cd Binary files /dev/null and "b/\350\257\276\344\273\266/lecture03-stack-queue.pdf" differ