[英]Function to see if there is a break in circuit (2D Array)
I want to create a function that lets me see if there is a break in a circuit which I've stored in a 2D Array by returning either true or false.我想创建一个 function,让我通过返回真或假来查看我存储在二维数组中的电路是否存在中断。 For simplicity sake i have modeled the circuit using 1's and 0's.
为简单起见,我使用 1 和 0 对电路进行了建模。 With 1's being components and 0's being spaces.
1 是组件,0 是空格。 I have tried messing with recursive functions however to no avail and am pretty much stuck at this point.
我尝试过使用递归函数,但无济于事,并且在这一点上几乎被卡住了。
For example:例如:
1 1 1
1 0 1
1 1 1
I would like this to return true as all the 1's are connected in series.我希望这返回 true,因为所有 1 都是串联连接的。 This 2D array would be visualized as seen here .
这个 2D 数组将被可视化,如此处所示。
1 1 1
0 0 1
1 1 1
I would like this to return false as there is a break in the circuit as as shown here .我希望它返回 false,因为电路中有一个中断,如此处所示。
Any solutions or guidance would be much appreciated!任何解决方案或指导将不胜感激!
My current code is shown below.我当前的代码如下所示。 It returns incomplete when I used the completeCircuit as an input however returns complete when I have the incompleteCircuit as an input.
当我使用 completeCircuit 作为输入时,它返回不完整,但是当我将不完整电路作为输入时,它返回完整。
public class TraversalTest
{
boolean complete = false;
int runs = 0;
public static void main(String[] args)
{
TraversalTest traversal = new TraversalTest();
}
public TraversalTest()
{
Cell[][] completeCircuit =
{
{ new Cell( 0, 0, 1), new Cell( 1, 0, 1), new Cell( 2, 0, 1) },
{ new Cell( 0, 1, 1), new Cell( 1, 1, 0), new Cell( 2, 1, 1) },
{ new Cell( 0, 2, 1), new Cell( 1, 2, 1), new Cell( 2, 2, 1) }
};
Cell[][] incompleteCircuit =
{
{ new Cell( 0, 0, 1), new Cell( 1, 0, 1), new Cell( 2, 0, 1) },
{ new Cell( 0, 1, 0), new Cell( 1, 1, 0), new Cell( 2, 1, 1) },
{ new Cell( 0, 2, 1), new Cell( 1, 2, 1), new Cell( 2, 2, 1) }
};
completeCircuit[1][0].connected = true;
int cellsLeft = (numOfPositiveCells(completeCircuit));
checkComplete(completeCircuit, completeCircuit[1][0], cellsLeft);
incompleteCircuit[1][0].connected = true;
int cellsLeft1 = (numOfPositiveCells(incompleteCircuit));
checkComplete(incompleteCircuit, incompleteCircuit[1][0], cellsLeft1);
}
void checkComplete(Cell[][] circuit, Cell currentCell, int cellsLeft)
{
currentCell.connected = true;
if(cellsLeft > 0)
{
if(currentCell.x != 0 && circuit[currentCell.x-1][currentCell.y].value == 1 &&
circuit[currentCell.x-1][currentCell.y].connected == false)
{
cellsLeft--;
checkComplete(circuit, circuit[currentCell.x-1][currentCell.y], cellsLeft);
}
else if(currentCell.x != 2 &&circuit[currentCell.x+1][currentCell.y].value == 1 &&
circuit[currentCell.x+1][currentCell.y].connected == false)
{
cellsLeft--;
checkComplete(circuit, circuit[currentCell.x+1][currentCell.y], cellsLeft);
}
else if(currentCell.y != 0 && circuit[currentCell.x][currentCell.y-1].value == 1 &&
circuit[currentCell.x][currentCell.y-1].connected == false)
{
cellsLeft--;
checkComplete(circuit, circuit[currentCell.x][currentCell.y-1], cellsLeft);
}
else if(currentCell.y != 2 && circuit[currentCell.x][currentCell.y+1].value == 1 &&
circuit[currentCell.x][currentCell.y+1].connected == false)
{
cellsLeft--;
checkComplete(circuit, circuit[currentCell.x][currentCell.y+1], cellsLeft);
}
else
{
complete = false;
System.out.println("Incomplete");
}
}
else
{
complete = true;
System.out.println("Complete");
}
}
int numOfPositiveCells(Cell[][] circuit)
{
int num = 0;
for(int x=0; x < 3; x++)
for(int y=0; y < 3; y++)
if(circuit[x][y].value == 1)
num++;
return num;
}
}
class Cell
{
public boolean connected;
public int value;
public int x;
public int y;
public Cell(int x, int y, int value)
{
this.x = x;
this.y = y;
this.value = value;
}
}
I think checking that every cell has 2 connections should work for your use cases as we only look for series connections.我认为检查每个单元是否有 2 个连接应该适用于您的用例,因为我们只寻找串联连接。
You just need to go through your array and make sure that all live cells have 2 and only 2 live cells connected to it您只需要通过您的阵列 go 并确保所有活细胞都有 2 个并且只有 2 个活细胞连接到它
I'd suggest you make a dedicated class, this way you don't have to keep passing the circuit around in all the methods:我建议你制作一个专用的 class,这样你就不必在所有方法中不断传递电路:
class CircuitChecker {
private final Cell[][] circuit;
private final int nbRows, nbCols;
public CircuitChecker(Cell[][] circuit) {
this.circuit = circuit;
this.nbRows = circuit.length;
this.nbCols = circuit[0].length;
}
public boolean isCircuitComplete() {
boolean isComplete = true;
for(int col = 0; col<nbCols; col++) {
for (int row = 0; row < nbRows; row++) {
if(cellIsLive(col, row) && !cellHas2LiveConnections(col, row)) {
isComplete = false;
break;
}
}
}
return isComplete;
}
private boolean cellIsLive(int col, int row) {
return circuit[row][col].value == 1;
}
private boolean cellHas2LiveConnections(int col, int row) {
Cell left = col > 0 ? circuit[col-1][row] : null;
Cell right = col < nbCols-1 ? circuit[col+1][row] : null;
Cell up = row > 0 ? circuit[col][row-1] : null;
Cell down = row < nbRows-1 ? circuit[col][row+1] : null;
int nbConnections = Stream.of(left, right, up, down)
.filter(Objects::nonNull)
.mapToInt(c -> c.value)
.sum();
return nbConnections == 2;
}
}
you call it this way:你这样称呼它:
new CircuitChecker(completeCircuit).isCircuitComplete();
new CircuitChecker(incompleteCircuit).isCircuitComplete();
One more thing, the fields in your class Cell
should be private (and even maybe final) and you should access them via getters.还有一件事,您的 class
Cell
中的字段应该是私有的(甚至可能是最终的),您应该通过 getter 访问它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.