骑士旅行 算法 IO 多线程 递归

多个类  

骑士旅行算法  将算出的不同走法 存入到文件中  每次运行的时候  载入以前算出的走法   新算出的走法 必须和以前算出的不同


 

 

package 骑士旅行;

import java.util.Random;
import java.util.Vector;

public class Store {

 static Vector<int[][]> v = new Vector<int[][]>(420, 20);

 static int vIndex = 0;

 static String storeWay = new String(
   "f://javaData//骑士旅行.dat");

 static boolean isRun = false;

 static Random Rd = new Random();


 public static void main(String[] args) {
  new Load();
  if (!v.isEmpty()) {
   for (int i = 0; i < v.size(); i++) {
    showArr();
    vIndex++;
   }
  }
  int useCpus = Runtime.getRuntime()
    .availableProcessors();
  if (useCpus > 1) {
   Thread[] thrd = new Thread[useCpus - 2];
   for (int i = 0; i < thrd.length; i++) {
    thrd[i] = new Thread(new Line());
    thrd[i].start();
   }
  } else {
   Line l1 = new Line();
   Thread t1 = new Thread(l1);
   t1.start();
  }
  // Line l1 = new Line();
  // Thread t1 = new Thread(l1);
  // t1.start();
  // Line l2 = new Line();
  // Thread t2 = new Thread(l2);
  // t2.start();
  // Line l3 = new Line();
  // Thread t3 = new Thread(l3);
  // t3.start();
 }


 final static void showArr() {
  System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^");
  System.out.println(vIndex);
  for (int i = 0; i < v.elementAt(Store.vIndex).length; i++) {
   for (int j = 0; j < v.elementAt(vIndex)[i].length; j++) {
    if (Store.v.elementAt(vIndex)[i][j] == 100) {
     System.out.print(" .. ");
    } else {
     StringBuffer step = new StringBuffer(4);
     step
       .append(" ")
       .append(
         Store.v
           .elementAt(Store.vIndex)[i][j]);
     while (step.length() < 4) {
      step.append(" ");
     }
     // String step = new
     // String(" ");
     // step +=
     // v.elementAt(vIndex)[i][j];
     // while
     // (step.length() <
     // 4) {
     // step += " ";
     // }
     System.out.print(step);
     step = null;
     // System.gc();
    }
   }
   System.out.println();
  }
 }
}

 


 

package 骑士旅行;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public final class Save {

 Save() {
  DataOutputStream dos = null;
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] b = null;
  dos = new DataOutputStream(baos);
  try {
   for (int i = 0; i < Store.v
     .elementAt(Store.vIndex).length; i++) {
    for (int j = 0; j < Store.v
      .elementAt(Store.vIndex)[i].length; j++) {
     dos
       .writeInt(~(Store.v
         .elementAt(Store.vIndex)[i][j] << j));
    }
   }
   dos.close();
   b = baos.toByteArray();
   baos.close();
  } catch (IOException e) {
  }
  FileOutputStream fos = null;
  try {
   fos = new FileOutputStream(Store.storeWay, true);
   fos.write(b);
   fos.close();
  } catch (FileNotFoundException e) {
  } catch (IOException e) {
  }
 }
}
 


package 骑士旅行;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public final class Load {

 Load() {
  FileInputStream fis = null;
  byte[] b = null;
  try {
   fis = new FileInputStream(Store.storeWay);
   b = new byte[fis.available()];
   fis.read(b);
   fis.close();
  } catch (FileNotFoundException e) {
   System.out.println("File not found !");
   return;
  } catch (NullPointerException e) {
   return;
  } catch (IOException e) {
  }
  DataInputStream dis = null;
  dis = new DataInputStream(new ByteArrayInputStream(
    b));
  try {
   while (dis.available() > 0) {
    int[][] arr = new int[10][10];
    for (int i = 0; i < arr.length; i++) {
     for (int j = 0; j < arr[i].length; j++) {
      arr[i][j] = (~dis.readInt()) >>> j;
     }
    }
    Store.v.addElement(arr);
   }
   dis.close();
  } catch (IOException e) {
   return;
  }
 }
}


package 骑士旅行;

import java.util.Random;

class Line implements Runnable {

 public void run() {
  Knight Knight = new Knight();
  int canGo = 1;
  while (true) {
   Map Map = new Map();
   Map.mapSet();
   Map.map[Knight.y][Knight.x] = Knight.steps++;
   Move M = new Move();
   while (true) {
    canGo = M.tryMove(Knight, Map);
    if (canGo == 0) {
     break;
    }
    if (Knight.steps == 65) {
     saveArrs(Map.map);
     System.gc();
     Store.isRun = false;
     break;
    }
   }
   Knight.steps = 1;
   M.last = 0;
   canGo = 1;
   Knight.x = 1 + (new Random().nextInt() >>> 29);
   Knight.y = 1 + (new Random().nextInt() >>> 29);
  }
 }


 final void saveArrs(int[][] map) {
  while (Store.isRun) {
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
   }
   System.out.println("Wait other Thread !");
  }
  Store.isRun = true;
  if (Store.vIndex == 0) {
   copyArr(map);
   return;
  }
  if (checkSame(map)) {
   // System.out.println(Thread.activeCount());
   return;
  }
  copyArr(map);
 }


 final void showArr() {
  System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^");
  System.out.println(Store.vIndex);
  for (int i = 0; i < Store.v.elementAt(Store.vIndex).length; i++) {
   for (int j = 0; j < Store.v
     .elementAt(Store.vIndex)[i].length; j++) {
    if (Store.v.elementAt(Store.vIndex)[i][j] == 100) {
     System.out.print(" .. ");
    } else {
     StringBuffer step = new StringBuffer();
     step
       .append(" ")
       .append(
         Store.v
           .elementAt(Store.vIndex)[i][j]);
     while (step.length() < 4) {
      step.append(" ");
     }
     System.out.print(step);
     step = null;
     System.gc();
    }
   }
   System.out.println();
  }
 }


 final void copyArr(int[][] map) {
  Store.v.addElement(map);
  map = null;
  new Save();
  showArr();
  Store.vIndex++;
 }


 final boolean checkSame(int[][] map) {
  for (int i = 0; i < Store.v.size(); i++) {
   if (check2(map, Store.v.elementAt(i))) {
    continue;
   }
   return true;
  }
  return false;
 }


 final boolean check2(int[][] a, int[][] b) {
  for (int i = 1; i < a.length - 1; i++) {
   if (check1(a[i], b[i])) {
    return false;
   }
  }
  return true;
 }


 final boolean check1(int[] a, int[] b) {
  for (int i = 1; i < a.length - 1; i++) {
   if (a[i] != b[i]) {
    return false;
   }
  }
  return true;
 }


 void show(int[][] map) {
  for (int i = 0; i < map.length; i++) {
   for (int j = 0; j < map[i].length; j++) {
    if (map[i][j] == 100) {
     System.out.print(" .. ");
    } else {
     StringBuffer step = new StringBuffer();
     step.append(" ").append(map[i][j]);
     while (step.length() < 4) {
      step.append(" ");
     }
     System.out.print(step);
    }
   }
   System.out.println();
  }
 }
}


package 骑士旅行;

final class Knight {

 int x = 5;

 int y = 5;

 int steps;
}


package 骑士旅行;

final class Map {

 int map[][] = new int[10][10];

 int hight = this.map.length;

 int weight = this.map[0].length;


 final void mapSet() {
  for (int i = 0; i < this.map.length; i++) {
   for (int j = 0; j < this.map[i].length; j++) {
    // 边界
    if (i == 0 || i == this.map.length - 1
      || j == 0
      || j == this.map[i].length - 1) {
     this.map[i][j] = 100;
     // 低级怪物
    } else {
     this.map[i][j] = 0;
    }
   }
  }
 }
}


package 骑士旅行;


final class Move {

 final int X[] = { 2, 1, -1, -2, -2, -1, 1, 2 };

 final int Y[] = { -1, -2, -2, -1, 1, 2, 2, 1 };

 int save[] = new int[8];

 int last;


 final int tryMove(Knight Knight, Map Map) {
  int i = Store.Rd.nextInt() >>> 29;
  this.save[i] = this.X[i];
  if (this.last != 1) {
   // 对比 最后组
   int count = 0;
   for (int j = 0; j < 8; j++) {
    if (this.save[j] == this.X[j]) {
     count++;
    } else {
     break;
    }
    if (count == 8) {
     this.last = 1;
    }
   }
   if (Knight.x + this.X[i] > 0
     && Knight.x + this.X[i] < Map.weight - 1
     && Knight.y + this.Y[i] > 0
     && Knight.y + this.Y[i] < Map.hight - 1) {
    if (Map.map[Knight.y + this.Y[i]][Knight.x
      + this.X[i]] != 0) {
     tryMove(Knight, Map);
    } else {
     Knight.x += this.X[i];
     Knight.y += this.Y[i];
     Map.map[Knight.y][Knight.x] = Knight.steps++;
     this.last = 0;
     for (int k = 0; k < this.save.length; k++) {
      this.save[k] = 0;
     }
     return 1;
    }
   } else {
    tryMove(Knight, Map);
   }
  } else if (this.last == 0) {
   return 1;
  } else {
   return 0;
  }
  return 3;
 }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值