Пример #1
0
  public Graph_model(In in) {
    this(in.readInt());
    int E = in.readInt();

    for (int i = 0; i < E; i++) {
      int v = in.readInt();
      int w = in.readInt();
      addEdge(v, w);
    }
  }
Пример #2
0
 /**
  * Initializes a board from the given filename.
  *
  * @param filename the name of the file containing the Boggle board
  */
 public BoggleBoard(String filename) {
   In in = new In(filename);
   M = in.readInt();
   N = in.readInt();
   if (M <= 0) {
     throw new IllegalArgumentException("number of rows must be a positive integer");
   }
   if (N <= 0) {
     throw new IllegalArgumentException("number of columns must be a positive integer");
   }
   board = new char[M][N];
   for (int i = 0; i < M; i++) {
     for (int j = 0; j < N; j++) {
       String letter = in.readString().toUpperCase();
       if (letter.equals("QU")) {
         board[i][j] = 'Q';
       } else if (letter.length() != 1) {
         throw new IllegalArgumentException("invalid character: " + letter);
       } else if (ALPHABET.indexOf(letter) == -1) {
         throw new IllegalArgumentException("invalid character: " + letter);
       } else {
         board[i][j] = letter.charAt(0);
       }
     }
   }
 }
 public Digraph(In in) {
   this(in.readInt());
   int edge = in.readInt();
   try {
     for (int i = 0; i < edge; i++) {
       if (in.hasNextLine()) {
         int v = in.readInt();
         int w = in.readInt();
         addEdge(v, w);
       }
     }
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Пример #4
0
 /** @param args */
 public static void main(String[] args) {
   // TODO Auto-generated method stub
   In in = new In("E:\\Further study in north America\\CS\\algorithmI\\collinear\\input6.txt");
   int N = in.readInt();
   Point[] pts = new Point[N];
   for (int i = 0; i < N; i++) {
     int x = in.readInt();
     int y = in.readInt();
     pts[i] = new Point(x, y);
     // pts[i].draw();
   }
   for (int i = 0; i < pts.length - 3; i++) {
     Arrays.sort(pts, i + 1, pts.length, pts[i].SLOPE_ORDER);
     Arrays.sort(pts, 0, i, pts[i].SLOPE_ORDER);
     check(pts, i);
   }
 }
 /**
  * Create a baseball division from given filename in format specified below.
  *
  * @param filename
  */
 public BaseballElimination(String filename) {
   In in = new In(filename);
   int l = in.readInt();
   wins = new int[l];
   loses = new int[l];
   remaining = new int[l];
   games = new int[l][l];
   for (int i = 0; i < l; i++) {
     teams.add(in.readString());
     wins[i] = in.readInt();
     loses[i] = in.readInt();
     remaining[i] = in.readInt();
     for (int j = 0; j < l; j++) {
       games[i][j] = in.readInt();
     }
   }
 }
Пример #6
0
  public static void main(String[] args) {

    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    StdDraw.show(0);
    In in = new In(args[0]);

    int N = in.readInt();
    Point[] points = new Point[N];
    for (int i = 0; i < N; i++) {
      int x = in.readInt();
      int y = in.readInt();
      Point p = new Point(x, y);
      p.draw();
      points[i] = p;
    }
    brute(points);
    StdDraw.show(0);
  }
  public static void main(String[] args) {
    In in = new In(args[0]); // input file
    int N = in.readInt(); // N-by-N percolation system

    // turn on animation mode
    StdDraw.show(0);

    // repeatedly read in sites to open and draw resulting system
    Percolation perc = new Percolation(N);
    draw(perc, N);
    StdDraw.show(DELAY);
    while (!in.isEmpty()) {
      int i = in.readInt();
      int j = in.readInt();
      perc.open(i, j);
      draw(perc, N);
      StdDraw.show(DELAY);
    }
  }
Пример #8
0
  /**
   * Ex_4.1.4 Ex_Graph_model 生成函数 使用一个没用的类来表明本图不允许自环
   *
   * @param in 输入流
   * @param isGraphWithoutSelfLoop 只是一个flag区别构造函数,代码中并未使用
   */
  public Ex_Graph_model(In in, boolean isGraphWithoutSelfLoop) {
    this(in.readInt());
    int E = in.readInt();

    try {
      for (int i = 0; i < E; i++) {
        int v = in.readInt();
        int w = in.readInt();

        if (v == w) {
          // 自环
          throw new SelfLoopException();
        }

        addEdgeWithoutParallelEdges(v, w);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Пример #9
-1
  public static void main(String[] args) {

    // read the N points from a file
    args = new String[1];
    args[0] = "test/rs1423.txt";
    In in = new In(args[0]);
    int N = in.readInt();
    Point[] points = new Point[N];
    for (int i = 0; i < N; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
    }

    // draw the points
    StdDraw.show(0);
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    for (Point p : points) {
      p.draw();
    }
    StdDraw.show();

    // print and draw the line segments
    FastCollinearPoints collinear = new FastCollinearPoints(points);
    for (LineSegment segment : collinear.segments()) {
      StdOut.println(segment);
      segment.draw();
    }
  }
Пример #10
-1
  public static void main(String[] args) {
    String input = "C:/Users/gkoontz/workspace/algoAssignments/src/collinear/equidistant.txt";
    // read the N points from a file
    //	    In in = new In(args[0]);
    In in = new In(input);
    int N = in.readInt();
    Point[] points = new Point[N];
    for (int i = 0; i < N; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
    }

    // draw the points
    StdDraw.show(0);
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    for (Point p : points) {
      p.draw();
    }
    StdDraw.show();

    // print and draw the line segments
    // BruteCollinearPoints collinear = new BruteCollinearPoints(points);
    FastCollinearPoints collinear = new FastCollinearPoints(points);
    for (LineSegment segment : collinear.segments()) {
      StdOut.println(segment);
      segment.draw();
    }
  }
  public static void main(String[] args) {
    // read the n points from a file
    In in = new In(args[0]);
    int n = in.readInt();
    Point[] points = new Point[n];
    for (int i = 0; i < n; i++) {
      int x = in.readInt();
      int y = in.readInt();
      points[i] = new Point(x, y);
    }

    // draw the points
    StdDraw.enableDoubleBuffering();
    StdDraw.setXscale(0, 32768);
    StdDraw.setYscale(0, 32768);
    for (Point p : points) {
      p.draw();
    }
    StdDraw.show();

    // print and draw the line segments
    FastCollinearPoints collinear = new FastCollinearPoints(points);
    for (LineSegment segment : collinear.segments()) {
      StdOut.println(segment);
      segment.draw();
    }
    StdDraw.show();
  }