Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.78 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.IO;
  7.  
  8. namespace MazeSolver
  9. {
  10.  
  11. /// <summary>
  12. /// Solveur de labyrinthe par Pascal Boutin
  13. /// </summary>
  14.  
  15.     class Program
  16.     {
  17.         struct Coord
  18.         {
  19.             public int x;
  20.             public int y;
  21.         }
  22.  
  23.         struct Orientation
  24.         {
  25.             public bool north;
  26.             public bool east;
  27.             public bool south;
  28.             public bool west;
  29.         }
  30.  
  31.         struct PosLab
  32.         {
  33.             public Coord Position;
  34.             public Orientation Orient;
  35.         }
  36.  
  37.         static void Main(string[] args)
  38.         {
  39.             // Dimensions du labyrinthe
  40.             const int xMax = 70;
  41.             const int yMax = 20;
  42.  
  43.             // Structures de données
  44.             Stack LabIntersect = new Stack();   // Contient les intersections
  45.             ArrayList History = new ArrayList();
  46.    
  47.             // Fichier
  48.             StreamReader file = new StreamReader("laby.txt");
  49.  
  50.             // Tableaux
  51.             string [] MazeMap = new string[yMax];   // Contient la map
  52.             bool[,] MazeResult;                     // Va contenir la solution
  53.  
  54.             // Variables
  55.             bool finish = false;                // Indique si la résolution est finit
  56.             bool resolved = false;                      // Indique si le labyrinthe est résolu
  57.             int NbPoss;                         // Indique le nombre de possibilités de déplacements
  58.             string way = "";                        // Indique par ou je vais
  59.             PosLab CurrentPos = new PosLab();   // Position courante
  60.  
  61.             Random rndpos = new Random();
  62.  
  63.             for ( int i = 0; i < yMax; i++ )
  64.                 // Charger le fichier et trouver la position courante
  65.             {
  66.                 MazeMap[i] = file.ReadLine();
  67.  
  68.                 // Si on trouve l'entrée, on stocke les coordonnées dans la position courante
  69.                 if (MazeMap[i].IndexOf('E') != 0)
  70.                 {
  71.                     CurrentPos.Position.x = MazeMap[i].IndexOf('E');
  72.                     CurrentPos.Position.y = i;
  73.                 }
  74.                 i++;
  75.             }
  76.  
  77.             // Résolution du labyrinthe
  78.             while (!finish)
  79.             {
  80.                 // Vérifier si on on peux se déplacer dans chaque direction et si on est dans les limites
  81.                 if (CurrentPos.Position.y == 0) CurrentPos.Orient.north = false; else CurrentPos.Orient.north = (MazeMap[CurrentPos.Position.y - 1][CurrentPos.Position.x] == '1');
  82.  
  83.                 if (CurrentPos.Position.x == xMax) CurrentPos.Orient.east = false; else CurrentPos.Orient.east = (MazeMap[CurrentPos.Position.y][CurrentPos.Position.x + 1] == '1');
  84.  
  85.                 if (CurrentPos.Position.y == yMax) CurrentPos.Orient.south = false; else CurrentPos.Orient.south = (MazeMap[CurrentPos.Position.y + 1][CurrentPos.Position.x] == '1');
  86.  
  87.                 if (CurrentPos.Position.x == 0) CurrentPos.Orient.west = false; else CurrentPos.Orient.west = (MazeMap[CurrentPos.Position.y][CurrentPos.Position.x - 1] == '1');
  88.  
  89.                 // Éviter de revenir sur ses pas
  90.                 switch (way)          
  91.                 {
  92.                     case "n": CurrentPos.Orient.south = false;
  93.                         break;
  94.                     case "e": CurrentPos.Orient.west = false;
  95.                         break;
  96.                     case "s": CurrentPos.Orient.north = false;
  97.                         break;
  98.                     case "w": CurrentPos.Orient.east = false;
  99.                         break;
  100.                 }
  101.  
  102.                 History.Add(CurrentPos);
  103.  
  104.             Depile:
  105.  
  106.                 // Trouver le nombre de possibilités
  107.                 way = "";
  108.  
  109.                 if (CurrentPos.Orient.north) way = way + "n";
  110.                 if (CurrentPos.Orient.east) way = way + "e";
  111.                 if (CurrentPos.Orient.south) way = way + "s";
  112.                 if (CurrentPos.Orient.west) way = way + "w";
  113.  
  114.                 NbPoss = way.Length;
  115.                
  116.                 // Choix aléatoire de la direction à emprunter
  117.                 way = way[rndpos.Next(1, NbPoss)].ToString();
  118.  
  119.  
  120.                 // Si on a pas de possibilitées
  121.                 if (NbPoss == 0)
  122.                 {
  123.                     // Et que la pile est vide
  124.                     if (LabIntersect.Count == 0)
  125.                     {
  126.                           finish = true;
  127.                           resolved = false;
  128.                      }
  129.                         // sinon de dépile
  130.                      else
  131.                      {
  132.                           CurrentPos = (PosLab)LabIntersect.Pop();
  133.  
  134.                           History.RemoveRange(History.IndexOf(CurrentPos) + 1, History.Count - History.IndexOf(CurrentPos));
  135.  
  136.                         // Pour contourner l'évaluation des possibilités
  137.                           goto Depile;  
  138.                      }
  139.                 }
  140.                              
  141.                 else
  142.                 {
  143.                     if (NbPoss > 1)
  144.                     {
  145.                         switch (way)
  146.                         {
  147.                             case "n":
  148.                                 {
  149.                                     CurrentPos.Orient.north = false;
  150.                                 }
  151.                                 break;
  152.  
  153.                             case "e":
  154.                                 {
  155.                                     CurrentPos.Orient.east = false;
  156.                                 }
  157.                                 break;
  158.  
  159.                             case "s":
  160.                                 {
  161.                                     CurrentPos.Orient.south = false;
  162.                                 }
  163.                                 break;
  164.  
  165.                             case "w":
  166.                                 {
  167.                                     CurrentPos.Orient.west = false;
  168.                                 }
  169.                                 break;
  170.                         }
  171.  
  172.                         LabIntersect.Push(CurrentPos);
  173.                     }
  174.  
  175.                       switch (way)
  176.                       {
  177.                                 case "n":
  178.                                     {
  179.                                         CurrentPos.Position.y = CurrentPos.Position.y - 1;
  180.                                     }
  181.                                     break;
  182.  
  183.                                 case "e":
  184.                                     {
  185.                                         CurrentPos.Position.x = CurrentPos.Position.x + 1;
  186.                                     }
  187.                                     break;
  188.  
  189.                                 case "s":
  190.                                     {
  191.                                         CurrentPos.Position.y = CurrentPos.Position.y + 1;
  192.                                     }
  193.                                     break;
  194.  
  195.                                 case "w":
  196.                                     {
  197.                                         CurrentPos.Position.x = CurrentPos.Position.x - 1;
  198.                                     }
  199.                                     break;
  200.                        }
  201.                 }
  202.                      
  203.             }
  204.  
  205.             // Écrit la solution dans un tableau de booléen
  206.             for (int i = 0; i < History.Count; i++)
  207.             {
  208.                 History[i
  209.  
  210.             }
  211.  
  212.  
  213.             if (resolved)
  214.             {
  215.                 System.Console.WriteLine("Le labyrinthe est résolu !");
  216.             }
  217.             else
  218.             {
  219.                 System.Console.WriteLine("C'est impossible !!!");
  220.             }
  221.  
  222.  
  223.          }
  224.  
  225.  
  226.            
  227.  
  228.      }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement