Einzelnen Beitrag anzeigen

Benutzerbild von patti
patti

Registriert seit: 20. Okt 2004
Ort: Mittelfranken
665 Beiträge
 
Turbo Delphi für Win32
 
#6

AW: Felder,Ketten im Array finden

  Alt 26. Jul 2011, 17:35
Hab da gerade mal was zusammengetippt. Ist allerdings in Java, da ich im Moment keine Delphi-Umgebung bei der Hand habe. Vielleicht hilft dir das ja schonmal einbisschen weiter

(Hoffe, dass ich keinen Fehler mehr drin habe, aber bei meinen Tests lief alles gut )

Code:
public class KettenFinden {
   public static void main(String[] args) {
      int[][] feld = new int[5][10];
      //
      fill(feld);
      //
      System.out.println("Ausgangsfeld:\n");
      print(feld);
      //
      System.out.println("\n********************\n");
      //
      final int x = 0;
      final int y = 0;
      //
      System.out.format("Kette für x = %d, y = %d:\n",x,y);
      //
      findeKette(feld,x,y);
   }
   
   public static void print(int[][] feld) {
      for (int x = 0; x < feld.length; x++) {
         for (int y = 0; y < feld[x].length; y++) System.out.print("|"+feld[x][y]+"|");
         System.out.println();
         for (int y = 0; y < feld[x].length; y++) System.out.print("---");
         System.out.println();
      }
   }

   public static void fill(int[][] feld) {
      for (int x = 0; x < feld.length; x++)
         for (int y = 0; y < feld[x].length; y++)
            feld[x][y] = (int)(Math.random() * 3);
   }

   public static void findeKette(int[][] feld, int x, int y) {
      if (x >= 0 && x < feld.length && y >= 0 && y < feld[0].length) {
         boolean[][] besucht = new boolean[feld.length][feld[0].length];
         //
         helper(feld,besucht,x,y);
         //
         for (int i = 0; i < besucht.length; i++) {
            for (int j = 0; j < besucht[i].length; j++) System.out.print("|" + (besucht[i][j] ? "x" : " ") + "|");
            System.out.println();
            for (int j = 0; j < besucht[i].length; j++) System.out.print("---");
            System.out.println();
         }
      }
   }

   private static void helper(int[][] feld, boolean[][] besucht, int x, int y) {
      besucht[x][y] = true;
      //
      if (x-1 >= 0 && !besucht[x-1][y] && feld[x-1][y] == feld[x][y]) helper(feld,besucht,x-1,y);
      if (y-1 >= 0 && !besucht[x][y-1] && feld[x][y-1] == feld[x][y]) helper(feld,besucht,x,y-1);
      if (x+1 < feld.length && !besucht[x+1][y] && feld[x+1][y] == feld[x][y]) helper(feld,besucht,x+1,y);
      if (y+1 < feld[x].length && !besucht[x][y+1] && feld[x][y+1] == feld[x][y]) helper(feld,besucht,x,y+1);
   }
}
Beispiel-Ausgabe:

Code:
Ausgangsfeld:

|1||1||0||2||1||2||2||1||0||0|
------------------------------
|1||1||0||0||2||0||0||0||2||2|
------------------------------
|0||1||2||0||1||2||0||1||2||2|
------------------------------
|1||0||2||2||1||1||0||1||1||2|
------------------------------
|0||2||1||2||1||2||2||2||1||0|
------------------------------

********************

Kette für x = 0, y = 0:

|x||x|| || || || || || || || |
------------------------------
|x||x|| || || || || || || || |
------------------------------
| ||x|| || || || || || || || |
------------------------------
| || || || || || || || || || |
------------------------------
| || || || || || || || || || |
------------------------------

Grüße,
Patrick


EDIT:
Hm, da war ich wohl nicht der einzige, der helfen wollte Naja, jetzt hast du sogar zwei Lösungen in zwei verschiedenen Programmiersprachen, was will man mehr?

EDIT2:
Gibts hier eigentlich eine Möglichkeit zum Highlighten von Java-Quellcode?
Patrick Kreutzer
[Informatik-Student im 4. Semester]
http://www.patti-k.de/

Geändert von patti (26. Jul 2011 um 17:38 Uhr)
  Mit Zitat antworten Zitat