From 43b2f0dd897c4f7aa21cb6fc5c1947b899018829 Mon Sep 17 00:00:00 2001 From: Giacomo Radaelli Date: Mon, 24 Apr 2023 15:08:06 +0200 Subject: [PATCH] Completato esercizio pile --- .../pile/nbproject/private/private.properties | 2 +- .../pile/nbproject/private/private.xml | 4 +- NetBeans Projects/pile/src/pile/Pair.java | 43 +++++++++--- NetBeans Projects/pile/src/pile/Pile.java | 69 +++++++++++-------- 4 files changed, 78 insertions(+), 40 deletions(-) diff --git a/NetBeans Projects/pile/nbproject/private/private.properties b/NetBeans Projects/pile/nbproject/private/private.properties index a2ad8bd..e17ea87 100644 --- a/NetBeans Projects/pile/nbproject/private/private.properties +++ b/NetBeans Projects/pile/nbproject/private/private.properties @@ -1,2 +1,2 @@ compile.on.save=true -user.properties.file=/home/giacomo/.netbeans/17/build.properties +user.properties.file=/home/gicorada/.netbeans/17/build.properties diff --git a/NetBeans Projects/pile/nbproject/private/private.xml b/NetBeans Projects/pile/nbproject/private/private.xml index 0ef534b..dff5538 100644 --- a/NetBeans Projects/pile/nbproject/private/private.xml +++ b/NetBeans Projects/pile/nbproject/private/private.xml @@ -3,8 +3,8 @@ - file:/home/giacomo/NetBeansProjects/pile/src/pile/Pile.java - file:/home/giacomo/NetBeansProjects/pile/src/pile/Pair.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/pile/src/pile/Pile.java + file:/media/gicorada/Scuola/Anni%20Severi/22-23/Informatica/java-scuola/NetBeans%20Projects/pile/src/pile/Pair.java diff --git a/NetBeans Projects/pile/src/pile/Pair.java b/NetBeans Projects/pile/src/pile/Pair.java index fc67f54..4baa17b 100644 --- a/NetBeans Projects/pile/src/pile/Pair.java +++ b/NetBeans Projects/pile/src/pile/Pair.java @@ -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; -/* - * 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 - */ - /** - * - * @author giacomo + * Classe che rappresenta una coppia riga/colonna + * @author radaelli11353 */ public class Pair { - private int row; - private int column; + private final int row; + private final int column; + /** + * Costruttore parametrico completo + * @param row Numero riga + * @param column Numero colonna + */ public Pair(int row, int column) { this.row = row; this.column = column; } + /** + * Getter della riga + * @return Riga + */ public int getRow() { return row; } + /** + * Getter della colonna + * @return Colonna + */ public int getColumn() { return column; } - } diff --git a/NetBeans Projects/pile/src/pile/Pile.java b/NetBeans Projects/pile/src/pile/Pile.java index f7f5462..c138391 100644 --- a/NetBeans Projects/pile/src/pile/Pile.java +++ b/NetBeans Projects/pile/src/pile/Pile.java @@ -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; import java.util.Scanner; import java.util.Stack; /** - * - * @author giacomo + * Classe effettua il riempimento a inondazione per riempire un array + * @author radaelli11353 */ public class Pile { @@ -13,51 +29,50 @@ public class Pile { * @param args the command line arguments */ 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 pairs = new Stack<>(); for(int i=0; i= 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) { - pairs.push(new Pair(actual.getRow()-1, actual.getColumn())); + + if(row + 1 <= 9 && grid[row+1][col] == 0) { + pairs.push(new Pair(row+1, col)); } - - if(grid[actual.getRow()+1][actual.getColumn()] == 0) { - pairs.push(new Pair(actual.getRow()+1, actual.getColumn())); + + if(col - 1 >= 0 && grid[row][col-1] == 0) { + pairs.push(new Pair(row, col-1)); } - - if(grid[actual.getRow()][actual.getColumn()-1] == 0) { - pairs.push(new Pair(actual.getRow(), actual.getColumn()-1)); - } - - if(grid[actual.getRow()][actual.getColumn()+1] == 0) { - pairs.push(new Pair(actual.getRow(), actual.getColumn()+1)); + + if(col + 1 <= 9 && grid[row][col+1] == 0) { + pairs.push(new Pair(row, col+1)); } } for (int[] r : grid) { - for (int i : r) { - System.out.print(i + "\t"); + for (int e : r) { + System.out.print(e + "\t"); } System.out.println(""); }