Java 加权快速接头
我目前正在拼贴中做算法,我们被要求使用加权快速联合制作一个十六进制游戏,讲师给了我们项目的大部分代码。但是我在这里遇到了一个问题{Java 加权快速接头,java,Java,我目前正在拼贴中做算法,我们被要求使用加权快速联合制作一个十六进制游戏,讲师给了我们项目的大部分代码。但是我在这里遇到了一个问题{ private int[][] board; // 2D Board. 0 - empty, 1 - Player 1, 2 - Player 2 private int n1, n2; // height and width of board private WeightedQuickUnionUF wqu; // Union Find data struc
private int[][] board; // 2D Board. 0 - empty, 1 - Player 1, 2 - Player 2
private int n1, n2; // height and width of board
private WeightedQuickUnionUF wqu; // Union Find data structure to keep track
// of unions and calculate winner
private int currentPlayer; // Current player in the game, initialised to 1
public Hex(int n1, int n2) // create N-by-N grid, with all sites blocked
{
this.n1 = n1;
this.n2 = n2;
currentPlayer = 1;
// TODO: Create instance of board
// TODO: Create instance WeightedQuickUnionUF class
wqu = new WeightedQuickUnionUF(14);
board = new int[n1][n2];
for(int i=0; i < n1 ; i++){
for(int j = 0; j < n2; j++){
board[i][j] = 0;
}
}
}
/*
* (non-Javadoc)
*
* @see BoardGame#takeTurn(int, int)
*/
@Override
public void takeTurn(int x, int y) {
if(((x > n1) || (x < 0)) || ((y > n2) || (y < 0)))
{
StdOut.println("Wrong");
}
else{
if(board[x][y] == 0){
board[x][y] = currentPlayer;
}
else{
StdOut.println("Taken");
}
}
// TODO: check coords are valid
// TODO: check if location is free and set to player's value(1 or 2).
// TODO: calculate location and neighbours location in
// WeightedQuickUnionUF data structure
// TODO: create unions to neighbour sites in WeightedQuickUnionUF that
// also contain current players value
// TODO: if no winner get the next player
}
/*
* (non-Javadoc)
*
* @see BoardGame#getCurrentPlayer()
*/
@Override
public int getCurrentPlayer() {
return currentPlayer;
}
public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
/*
* (non-Javadoc)
*
* @see BoardGame#getBoard()
*/
@Override
public int[][] getBoard() {
return board;
}
private void nextPlayer() {
if (currentPlayer == 1)
currentPlayer = 2;
else
currentPlayer = 1;
}
/*
* (non-Javadoc)
*
* @see BoardGame#isWinner()
*/
@Override
public boolean isWinner() {
// TODO:check if there is a connection between either side of the board.
// You can do this by using the 'virtual site' approach in the
// percolation test.
return false;
}
/**
* THIS IS OPTIONAL:
* Modify the main method if you wish to suit your implementation.
* This is just an example of a test implementation.
* For example you may want to display the board after each turn.
* @param args
*
*/
public static void main(String[] args) {
BoardGame hexGame = new Hex(4, 4);
while (!hexGame.isWinner()) {
System.out.println("It's player " + hexGame.getCurrentPlayer()
+ "'s turn");
System.out.println("Enter x and y location:");
int x = StdIn.readInt();
int y = StdIn.readInt();
hexGame.takeTurn(x, y);
}
System.out.println("It's over. Player " + hexGame.getCurrentPlayer()
+ " wins!");
}
private int[][]board;//2D board.0-空,1-播放器1,2-播放器2
私有int n1,n2;//板的高度和宽度
私有加权QuickUnionUF wqu;//联合查找要跟踪的数据结构
//工会的数量和计算赢家
private int currentPlayer;//游戏中的当前玩家,初始化为1
公共十六进制(int n1,int n2)//创建N×N网格,所有站点都被阻止
{
这1.n1=n1;
这1.n2=n2;
currentPlayer=1;
//TODO:创建板的实例
//TODO:创建实例权重的QuickUnionUF类
wqu=新加权快速联合UF(14);
board=新整数[n1][n2];
对于(int i=0;in1)| |(x<0))| |((y>n2)| |(y<0)))
{
打印符号(“错误”);
}
否则{
如果(电路板[x][y]==0){
板[x][y]=当前播放器;
}
否则{
标准打印号(“已拍摄”);
}
}
//TODO:检查坐标是否有效
//TODO:检查位置是否空闲并设置为玩家值(1或2)。
//TODO:计算中的位置和邻居位置
//加权QuickUnionUF数据结构
//TODO:在WeightedQuickUnionUF中为相邻站点创建联合
//也包含当前玩家的价值
//TODO:如果没有胜利者,请选择下一个玩家
}
/*
*(非Javadoc)
*
*@参见BoardGame#getCurrentPlayer()
*/
@凌驾
public int getCurrentPlayer(){
返回当前播放器;
}
公共无效setCurrentPlayer(int currentPlayer){
this.currentPlayer=currentPlayer;
}
/*
*(非Javadoc)
*
*@see BoardGame#getBoard()
*/
@凌驾
public int[]getBoard(){
返回板;
}
私有void nextPlayer(){
如果(currentPlayer==1)
currentPlayer=2;
其他的
currentPlayer=1;
}
/*
*(非Javadoc)
*
*@see BoardGame#isWinner()
*/
@凌驾
公共布尔值(){
//TODO:检查电路板两侧之间是否有连接。
//您可以通过在中使用“虚拟站点”方法来实现这一点
//渗滤试验。
返回false;
}
/**
*这是可选的:
*如果希望适合您的实现,请修改main方法。
*这只是一个测试实现的示例。
*例如,您可能希望在每次转弯后显示电路板。
*@param args
*
*/
公共静态void main(字符串[]args){
棋盘游戏hexGame=新的Hex(4,4);
而(!hexGame.isWinner()){
System.out.println(“它是玩家”+hexGame.getCurrentPlayer()
+“轮到你了”);
System.out.println(“输入x和y位置:”);
intx=StdIn.readInt();
int y=StdIn.readInt();
六角游戏。轮流(x,y);
}
System.out.println(“结束了.Player”+hexGame.getCurrentPlayer()
+“赢了!”;
}
}
`
我已经检查了坐标是否有效,棋盘上的位置是否空闲。但我似乎可以通过使用加权QuickUnionuf找到位置和邻居的位置。任何帮助都会很好,因为我已经尝试了目前为止我所知道的一切。这是加权的QuickUnionUF类
public class WeightedQuickUnionUF {
private int[] id;
private int[] sz;
private int count;
public WeightedQuickUnionUF(int N){
count = N;
id = new int[N];
sz = new int[N];
for(int i = 0 ; i < N; i++){
id[i] = i;
sz[i] = i;
}
}
public int count(){
return count;
}
public int find(int p){
while(p != id[p])
p = id[p];
return p;
}
public boolean connected(int p, int q ){
return find(p) == find(q);
}
public void union(int p, int q){
int i = find(p);
int j = find(q);
if(i == j) return;
if(sz[i] < sz[j]){id[i] = j; sz[j] += sz[i];}
else {id[j] = i; sz[i] += sz[j];}
count--;
}
public static void main(String[] args) {
int N = StdIn.readInt();
WeightedQuickUnionUF uf = new WeightedQuickUnionUF(N);
while(!StdIn.isEmpty()){
int p = StdIn.readInt();
int q = StdIn.readInt();
if(uf.connected(p,q)) continue;
uf.union(p, q);
StdOut.println(p + " " + q);
}
StdOut.println(uf.count() + "components");
}
}
公共类权重QuickUnionUF{
私有int[]id;
私人互联网【】深圳;
私人整数计数;
公共权重QuickUnionUF(整数N){
计数=N;
id=新的整数[N];
sz=新整数[N];
对于(int i=0;i
sz[]的初始化代码中有一个bug
应该是:
for(int i = 0 ; i < N; i++){
id[i] = i;
sz[i] = 1; // changed to 1 so it indicates the number of nodes for this 'root'
}
for(int i=0;i
n1和n2从何而来?请更详细地解释您的解决方案中哪些地方不正常工作。目前我只看到很多代码和一个建议,即并非所有操作都如您所愿。例如,当我将board[1][1]=player 1时,我必须在WeightedQuickUnionUF类中找到该位置及其邻居,现在如果我执行wqu.find(board[1][1]),这等于1,因此它会找到id[1],即1。现在如果我搜索neighours,它们也将等于1,这将链接回id[1]。然后我可以通过wqu.union(board[1][1],board[2][3])检查它们是否为并集(如果没有连接它们的话)。。。但它似乎连接了电路板上的每个点,所以如果我检查wqu.connected(电路板[1][1],电路板[3][3]),它将返回true。希望这能帮你解释一下,或者让你更困惑