Completato esercizio pile
This commit is contained in:
parent
eb57e85a22
commit
43b2f0dd89
|
@ -1,2 +1,2 @@
|
||||||
compile.on.save=true
|
compile.on.save=true
|
||||||
user.properties.file=/home/giacomo/.netbeans/17/build.properties
|
user.properties.file=/home/gicorada/.netbeans/17/build.properties
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
|
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
|
||||||
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
|
||||||
<group>
|
<group>
|
||||||
<file>file:/home/giacomo/NetBeansProjects/pile/src/pile/Pile.java</file>
|
<file>file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/pile/src/pile/Pile.java</file>
|
||||||
<file>file:/home/giacomo/NetBeansProjects/pile/src/pile/Pair.java</file>
|
<file>file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/pile/src/pile/Pair.java</file>
|
||||||
</group>
|
</group>
|
||||||
</open-files>
|
</open-files>
|
||||||
</project-private>
|
</project-private>
|
||||||
|
|
|
@ -1,29 +1,52 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 radaelli11353.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
package pile;
|
package pile;
|
||||||
|
|
||||||
/*
|
|
||||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
|
||||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Classe che rappresenta una coppia riga/colonna
|
||||||
* @author giacomo
|
* @author radaelli11353
|
||||||
*/
|
*/
|
||||||
public class Pair {
|
public class Pair {
|
||||||
private int row;
|
private final int row;
|
||||||
private int column;
|
private final int column;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Costruttore parametrico completo
|
||||||
|
* @param row Numero riga
|
||||||
|
* @param column Numero colonna
|
||||||
|
*/
|
||||||
public Pair(int row, int column) {
|
public Pair(int row, int column) {
|
||||||
this.row = row;
|
this.row = row;
|
||||||
this.column = column;
|
this.column = column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter della riga
|
||||||
|
* @return Riga
|
||||||
|
*/
|
||||||
public int getRow() {
|
public int getRow() {
|
||||||
return row;
|
return row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter della colonna
|
||||||
|
* @return Colonna
|
||||||
|
*/
|
||||||
public int getColumn() {
|
public int getColumn() {
|
||||||
return column;
|
return column;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,27 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2023 radaelli11353.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
package pile;
|
package pile;
|
||||||
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* Classe effettua il riempimento a inondazione per riempire un array
|
||||||
* @author giacomo
|
* @author radaelli11353
|
||||||
*/
|
*/
|
||||||
public class Pile {
|
public class Pile {
|
||||||
|
|
||||||
|
@ -13,51 +29,50 @@ public class Pile {
|
||||||
* @param args the command line arguments
|
* @param args the command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
int[][] grid = new int[10][10];//Pair array to insert in the stack
|
int[][] grid = new int[10][10];
|
||||||
|
|
||||||
Stack<Pair> pairs = new Stack<>();
|
Stack<Pair> pairs = new Stack<>();
|
||||||
for(int i=0; i<grid.length; i++){
|
for(int i=0; i<grid.length; i++){
|
||||||
for(int j=0; j<grid[i].length; j++){ //to fill up the array
|
for(int j=0; j<grid[i].length; j++){
|
||||||
grid[i][j]=0;
|
grid[i][j]=0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Scanner sc = new Scanner(System.in);
|
Scanner in = new Scanner(System.in);
|
||||||
int row, column;
|
System.out.print("Inserisci posizione da cui iniziare flood fill (riga colonna): ");
|
||||||
row=sc.nextInt();
|
pairs.push(new Pair(in.nextInt()-1, in.nextInt()-1));
|
||||||
column=sc.nextInt();
|
|
||||||
|
|
||||||
pairs.push(new Pair(row, column));
|
|
||||||
|
|
||||||
int counter = 1;
|
int counter = 1;
|
||||||
|
|
||||||
while(!pairs.isEmpty()) {
|
while(!pairs.isEmpty()) {
|
||||||
Pair actual = pairs.pop();
|
Pair actual = pairs.pop();
|
||||||
|
int row = actual.getRow();
|
||||||
|
int col = actual.getColumn();
|
||||||
|
|
||||||
if(grid[actual.getRow()][actual.getColumn()] != 0) {
|
if(grid[row][col] == 0) {
|
||||||
grid[actual.getRow()][actual.getColumn()] = counter++;
|
grid[row][col] = counter++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(row - 1 >= 0 && grid[row-1][col] == 0) {
|
||||||
|
pairs.push(new Pair(row-1, col));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(grid.length != actual.getRow()-1 && grid[actual.getRow()-1][actual.getColumn()] == 0) {
|
if(row + 1 <= 9 && grid[row+1][col] == 0) {
|
||||||
pairs.push(new Pair(actual.getRow()-1, actual.getColumn()));
|
pairs.push(new Pair(row+1, col));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(grid[actual.getRow()+1][actual.getColumn()] == 0) {
|
if(col - 1 >= 0 && grid[row][col-1] == 0) {
|
||||||
pairs.push(new Pair(actual.getRow()+1, actual.getColumn()));
|
pairs.push(new Pair(row, col-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
if(grid[actual.getRow()][actual.getColumn()-1] == 0) {
|
if(col + 1 <= 9 && grid[row][col+1] == 0) {
|
||||||
pairs.push(new Pair(actual.getRow(), actual.getColumn()-1));
|
pairs.push(new Pair(row, col+1));
|
||||||
}
|
|
||||||
|
|
||||||
if(grid[actual.getRow()][actual.getColumn()+1] == 0) {
|
|
||||||
pairs.push(new Pair(actual.getRow(), actual.getColumn()+1));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int[] r : grid) {
|
for (int[] r : grid) {
|
||||||
for (int i : r) {
|
for (int e : r) {
|
||||||
System.out.print(i + "\t");
|
System.out.print(e + "\t");
|
||||||
}
|
}
|
||||||
System.out.println("");
|
System.out.println("");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue