aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorInigoGutierrez <inigogf.95@gmail.com>2017-11-19 13:49:12 +0100
committerInigoGutierrez <inigogf.95@gmail.com>2017-11-19 13:49:12 +0100
commitdac79080e066d17f6971d4f2145943ac50c4a7a8 (patch)
tree0d20ae566345c29d7d8774720ae9eb8b1ccef16b
parent86b552ba70fd18a6e450c89315469d1a7b5e640f (diff)
downloadAceptaElReto-dac79080e066d17f6971d4f2145943ac50c4a7a8.tar.gz
AceptaElReto-dac79080e066d17f6971d4f2145943ac50c4a7a8.zip
Problema 207 (Sombras en el camping) resuelto.
-rw-r--r--AceptaElReto/src/problemas/Problema207SombrasEnElCamping.java56
1 files changed, 56 insertions, 0 deletions
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;
+ }
+}