praktische informatik i der imperative kern rekursive
play

Praktische Informatik I Der Imperative Kern Rekursive Suche Prof. - PowerPoint PPT Presentation

Praktische Informatik I Der Imperative Kern Rekursive Suche Prof. Dr. Stefan Edelkamp Institut fr Knstliche Intelligenz Technologie-Zentrum fr Informatik und Informationstechnik (TZI) Am Fallturm 1, 28359 Bremen +49-(0)421-218-64007


  1. Praktische Informatik I – Der Imperative Kern Rekursive Suche Prof. Dr. Stefan Edelkamp Institut für Künstliche Intelligenz Technologie-Zentrum für Informatik und Informationstechnik (TZI) Am Fallturm 1, 28359 Bremen +49-(0)421-218-64007 www.tzi.de/~edelkamp Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 1 / 15

  2. Outline Tower-of-Hanoi 1 Labyrinth-Suche 2 Dameprobleme 3 Sudokus 4 Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 2 / 15

  3. Ewiges Stapeln Tower-of-Hanoi fragt nach einer Umlegung von n Platten von Stapel a auf Stapel b unter Zuhilfenahme eines dritten Stapels c . Die Platten sind in der Größe verschieden und eine kleinere Platte darf niemals unterhalb einer größere Platte liegen. Das folgende Bild zeigt alle möglichen Zustände mit n = 2 Platten Die rekursive Lösung verschiebt einen Stapel der Größe n durch das rekursive Verschieben von zwei Stapeln der Größe n − 1. Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 3 / 15

  4. Programm 1: Die Lösung von Tower-of-Hanoi. public class Hanoi { / ∗∗ ∗ Constructor for objects of class Gauss ∗ / public Hanoi () { move (4,’a’,’b’,’c’); // call function } / ∗∗ ∗ moves of a stack in TOH problem ∗ ∗ @param y a sample parameter for a method ∗ @return the sum of x and y ∗ / public void move ( int n , char a , char b , char c ) { // move stack if ( n == 0) return ; // stack is empty move ( n − 1, a , c , b ); // recursive call, a to c via b System . out . println ("move "+ n +" from "+ a +" to "+ c ); move ( n − 1, c , a , b ); // recursive call, c to a via b } } Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 4 / 15

  5. Outline Tower-of-Hanoi 1 Labyrinth-Suche 2 Dameprobleme 3 Sudokus 4 Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 5 / 15

  6. Ariadne unterwegs Das Labyrinthproblem erfragt den Weg vom Start- zum Zielpunkt. Das Programm 3 liefert die eine Lösung des Labyrinthproblems mit zufällig ausgewürfelten Wänden Die Tiefensuche schaut in alle Himmelsrichtungen und jede besuchte Koordinate wird mit einer Markierung versehen. Die Besuchtmarkierung verhindert den Mehrfachbesuch von Positionen – die Zielbedingung sichert die Terminierung Desweiteren wird die Lösungssequenz auf der Konsole ausgegeben. Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 6 / 15

  7. Programm 2: Ein Labyrinthproblem. import java . util . Random ; public class Maze { int maze [][]; // maze with walls that block int visited [][]; // memorization to avoid duplicates int goalx ; int goaly ; public Maze ( int x , int y ) { Random r = new Random (); maze = new int [ x ][ y ]; visited = new int [ x ][ y ]; for ( int i =0; i < x ; i ++) for ( int j =0; j < y ; j ++) maze [ i ][ j ] = visited [ i ][ j ] = 0; for ( int i =1; i < x − 1; i ++) for ( int j =1; j < y − 1; j ++) if ( r . nextInt () % 5 != 0) maze [ i ][ j ] = 1; goalx = x − 1; goaly = y − 1; maze [ goalx ][ goaly ] = 1; } Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 7 / 15

  8. Programm 3: Ein Labyrinth (Tiefensuch-) Löser. public void dfs ( int x , int y ) { if ( maze [ x ][ y ] == 0 || visited [ x ][ y ] != 0) return ; // failure System . out . println (" @ ("+ x +","+ y +")"); // output if ( x == goalx && y == goaly ) { // goal coordinates found System . out . println ("goal found"); System . exit (1); } visited [ x ][ y ] = 1; // memorize, not to be visited again dfs ( x , y +1); dfs ( x , y − 1); // calls to neighbors dfs ( x +1, y ); dfs ( x − 1, y ); // calls to neighbors } } Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 8 / 15

  9. Outline Tower-of-Hanoi 1 Labyrinth-Suche 2 Dameprobleme 3 Sudokus 4 Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 9 / 15

  10. Bitte nicht schlagen! Das n -Dameproblem fragt nach der Platzierung von n Damen auf einem ( n × n ) großen Schachspiel, dass keine Dame eine andere (vertikal, horizontal, oder diagonal) bedroht. Die Lösung produziert im Gegensatz zu Tower-of-Hanoi Rücksprünge – die bei illegalen Stellungen den Lösungsprozess weiterführen. Das Programm 4 erzeugt alle Lösungen. Es existiert eine suchfreie Lösung des Problems. Aufgabe: Gibt es Werte von n für die Sie die Lösung direkt angeben können? Tipp: Die Werte von n , bei denen eine einfache Konstruktion funktioniert haben einen Abstand von 6 und die Konstruktion nutzt Rösselsprünge Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 10 / 15

  11. Programm 4: Die Lösung des Dameproblems. public class Queens { private int [] a ; public Queens ( int n ) { a = new int [ n ]; solve ( n ,0); } public boolean legal ( int q , int row ) { for ( int column =0; column < row ; column ++) if ( a [ column ] == q || a [ column ] − q == column − row || q − a [ column ] == column − row ) return false ; return true ; } public void solve ( int n , int j ) { if ( j == n ) { for ( int i =0; i < n ; i ++) { // for each row for ( int k =0; k < n ; k ++) // for each column if ( i == a [ k ]) System . out . print ("@"); else System . out . print ("."); System . out . println (); } } for ( int q =0; q < n ; q ++) if ( legal ( q , j )) { a [ j ] = q ; solve ( n , j +1); } } } Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 11 / 15

  12. Outline Tower-of-Hanoi 1 Labyrinth-Suche 2 Dameprobleme 3 Sudokus 4 Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 12 / 15

  13. Sichtlich gelöst Ein Sudoku besteht aus 9 × 9 Feldern, die zusätzlich in 3 × 3 Blöcken mit 3 × 3 Feldern aufgeteilt sind. Zeilen, Spalten und Blöcke enthalten alle Zahlen von 1 bis 9 einmal Sudokus wurden wahrscheinlich um 1970 erstmalig vom Rätselspezialisten Dell im Magazin Mathematische Rätsel und logische Probleme veröffentlicht. 1984 publizierte ein japanischer Zeitschriftenverleger das Rätsel in der Zeitung Monthly Nikolist Der folgende Löser für Sudokus ist praktisch. Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 13 / 15

  14. Programm 5: Ein Sudokuproblem-Löser. public class Sudoku { public char s [] = {’0’,’0’,’0’,’0’,’0’,’0’,’0’,’0’,’0’, ’0’,’0’,’0’,’0’,’0’,’3’,’0’,’8’,’5’, ’0’,’0’,’1’,’0’,’2’,’0’,’0’,’0’,’0’, ’0’,’0’,’0’,’5’,’0’,’7’,’0’,’0’,’0’, ’0’,’0’,’4’,’0’,’0’,’0’,’1’,’0’,’0’, ’0’,’9’,’0’,’0’,’0’,’0’,’0’,’0’,’0’, ’5’,’0’,’0’,’0’,’0’,’0’,’0’,’7’,’3’, ’0’,’0’,’2’,’0’,’1’,’0’,’0’,’0’,’0’, ’0’,’0’,’0’,’0’,’4’,’0’,’0’,’0’,’9’}; / ∗∗ ∗ calling the solver ∗ / public void solve () { show (); search (0); show (); } private void show () { for ( int i =0; i <9; i ++) { for ( int j =0; j <9; j ++) System . out . print ( s [ i ∗ 9+ j ] == ’0’ ? "." : s [ i ∗ 9+ j ]); System . out . println (); } System . out . println (); Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 14 / 15 }

  15. Programm 6: Ein Sudokuproblem-Löser. public boolean test ( char d , int r , int c ) { for ( int i =0; i <9; i ++) if ( s [9 ∗ r + i ] == d || s [9 ∗ i + c ] == d || s [9 ∗ ( r /3 ∗ 3+ i /3)+( c /3 ∗ 3+ i %3)] == d ) return false ; return true ; } public boolean search ( int pos ) { if ( pos == 81) return true ; if ( s [ pos ] > ’0’) { if ( search ( pos +1)) return true ; } else for ( int i =0; i <9; i ++) if ( test (( char ) (’1’+ i ), pos /9, pos %9)) { s [ pos ] = ( char ) (’1’+ i ); if ( search ( pos +1)) return true ; s [ pos ] = 0; } return false ; } } Stefan Edelkamp (IAI) PI 1 – Imperativer Kern November 14, 2013 15 / 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend