From dac79080e066d17f6971d4f2145943ac50c4a7a8 Mon Sep 17 00:00:00 2001 From: InigoGutierrez Date: Sun, 19 Nov 2017 13:49:12 +0100 Subject: Problema 207 (Sombras en el camping) resuelto. --- .../problemas/Problema207SombrasEnElCamping.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 AceptaElReto/src/problemas/Problema207SombrasEnElCamping.java diff --git a/AceptaElReto/src/problemas/Problema207SombrasEnElCamping.java b/AceptaElReto/src/problemas/Problema207SombrasEnElCamping.java new file mode 100644 index 0000000..e771e3d --- /dev/null +++ b/AceptaElReto/src/problemas/Problema207SombrasEnElCamping.java @@ -0,0 +1,56 @@ +package problemas; + +public class Problema207SombrasEnElCamping { + + static java.util.Scanner in; + + public static void main(String args[]) { + in = new java.util.Scanner(System.in); + + int width = in.nextInt(); + int height = in.nextInt(); + int trees = in.nextInt(); + while ( width != 0 ) { + System.out.println(casoDePrueba(width, height, trees)); + width = in.nextInt(); + height = in.nextInt(); + trees = in.nextInt(); + } + } + + public static int casoDePrueba(int width, int height, int trees) { + boolean[][] camping = new boolean[width][height]; + int counter = 0; + int treeCol; + int treeRow; + for ( int i = 0; i < trees; i++ ) { // plant trees + treeCol = in.nextInt()-1; + treeRow = in.nextInt()-1; + camping[treeCol][treeRow] = true; + } + for ( int col = 0; col < width; col++ ) { + for ( int row = 0; row < height; row++ ) { + if ( !camping[col][row] ) { + counter += hasShadow(camping, col, row); + } + } + } + return counter; + } + + public static int hasShadow(boolean[][] camping, int col, int row) { + // Check if cell (col, row) has shadow + for ( int i = -1; i <= 1; i++ ) { + for ( int j = -1; j <= 1; j++ ) { + try { + if ( camping[col+i][row+j] ) { + return 1; + } + } catch (ArrayIndexOutOfBoundsException e) { + // ignore out of array cells + } + } + } + return 0; + } +} -- cgit v1.2.1