Advertisement
Guest User

Untitled

a guest
Nov 21st, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Com.CodeGame.CodeRacing2015.DevKit.CSharpCgdk.Model;
  4.  
  5.  
  6. public class WayFinder
  7. {
  8.     public List<Tile> way;
  9.     private TileType[][] map;
  10.     public int[][] isMached;
  11.  
  12.     private bool rematch;
  13.  
  14.     public int[] startPos;
  15.  
  16.     public int[] targetPos;
  17.  
  18.     public WayFinder()
  19.     {
  20.         this.map = null;
  21.         this.startPos = new int[2] {0,0};
  22.         this.targetPos = new int[2] { 0, 0 };
  23.         rematch = false;
  24.     }
  25.  
  26.     public WayFinder(TileType[][] map)
  27.     {
  28.  
  29.         SetMap(map);
  30.         this.startPos = new int[2] { 0, 0 };
  31.         this.targetPos = new int[2] { 0, 0 };
  32.         rematch = true;
  33.     }
  34.  
  35.  
  36.     public void SetMap(TileType[][] map)
  37.     {
  38.         this.map = map;
  39.  
  40.     }
  41.  
  42.     private int BLANK = -2;
  43.     private int EMPTY = -1;
  44.     private int len = 0;
  45.  
  46.     private bool isCome(int x, int y, int dx, int dy)
  47.     {
  48.         TileType t1 = map[x][y];
  49.        // TileType t2 = map[x+dx][y+dy];
  50.  
  51.         if(dx > 0)
  52.         {
  53.             switch (t1)
  54.             {
  55.                 case TileType.Vertical:
  56.                 case TileType.RightTopCorner:
  57.                 case TileType.RightBottomCorner:
  58.                 case TileType.LeftHeadedT:
  59.                     return false;
  60.             }
  61.  
  62.             return true;
  63.         }
  64.  
  65.         if (dx < 0)
  66.         {
  67.             switch (t1)
  68.             {
  69.                 case TileType.Vertical:
  70.                 case TileType.LeftTopCorner:
  71.                 case TileType.LeftBottomCorner:
  72.                 case TileType.RightHeadedT:
  73.                     return false;
  74.             }
  75.  
  76.             return true;
  77.         }
  78.  
  79.         if (dy < 0)
  80.         {
  81.             switch (t1)
  82.             {
  83.                 case TileType.Horizontal:
  84.                 case TileType.LeftTopCorner:
  85.                 case TileType.RightTopCorner:
  86.                 case TileType.BottomHeadedT:
  87.                     return false;
  88.             }
  89.             return true;
  90.         }
  91.  
  92.         if (dy > 0)
  93.         {
  94.             switch (t1)
  95.             {
  96.                 case TileType.Horizontal:
  97.                 case TileType.LeftBottomCorner:
  98.                 case TileType.RightBottomCorner:
  99.                 case TileType.TopHeadedT:
  100.                     return false;
  101.             }
  102.  
  103.             return true;
  104.         }
  105.  
  106.        
  107.  
  108.         return true;
  109.     }
  110.  
  111.     public bool MathPath()
  112.     {
  113.         ClearIsMached();
  114.         if (way != null)
  115.             way.Clear();
  116.         else
  117.             way = new List<Tile>();
  118.  
  119.         int[] dx = new int[4] { -1, 0, 1, 0 };
  120.         int[] dy = new int[4] { 0, 1, 0, -1 };
  121.         int d, x, y, k;
  122.  
  123.         int W = map.Length;
  124.         int H = map[0].Length;
  125.  
  126.         int ax = startPos[0];
  127.         int ay = startPos[1];
  128.  
  129.         int bx = targetPos[0];
  130.         int by = targetPos[1];
  131.  
  132.  
  133.         bool stop;
  134.  
  135.         if (isMached[ax][ay] == EMPTY || isMached[bx][by] == EMPTY) return false;  // ячейка (ax, ay) или (bx, by) - стена
  136.  
  137.         // распространение волны
  138.         d = 0;
  139.         isMached[ax][ay] = 0;            // стартовая ячейка помечена 0
  140.         do
  141.         {
  142.             stop = true;               // предполагаем, что все свободные клетки уже помечены
  143.             for (y = 0; y < H; ++y)
  144.                 for (x = 0; x < W; ++x)
  145.                     if (isMached[x][y] == d)                         // ячейка (x, y) помечена числом d
  146.                     {
  147.                         for (k = 0; k < 4; ++k)                    // проходим по всем непомеченным соседям
  148.                         {
  149.                             if (isCome(x, y, dx[k], dy[k])) {
  150.  
  151.                                 int iy = y + dy[k], ix = x + dx[k];
  152.                                 if (iy >= 0 && iy < H && ix >= 0 && ix < W &&
  153.                                      isMached[ix][iy] == BLANK)
  154.                                 {
  155.                                     stop = false;              // найдены непомеченные клетки
  156.                                     isMached[ix][iy] = d + 1;      // распространяем волну
  157.                                 }
  158.                             }
  159.                         }
  160.                     }
  161.             d++;
  162.         } while (!stop && isMached[bx][by] == BLANK);
  163.  
  164.         if (isMached[bx][by] == BLANK) return false;  // путь не найден
  165.  
  166.         // восстановление пути
  167.  
  168.         len = isMached[bx][by];            // длина кратчайшего пути из (ax, ay) в (bx, by)
  169.         x = bx;
  170.         y = by;
  171.         d = len;
  172.  
  173.         while (d > 0)
  174.         {
  175.             Tile tile = new Tile();
  176.             tile.type = map[x][y];
  177.             tile.x = x;
  178.             tile.y = y;
  179.             way.Add(tile);
  180.  
  181.             //px[d] = x;
  182.             //py[d] = y;                   // записываем ячейку (x, y) в путь
  183.             d--;
  184.             for (k = 0; k < 4; ++k)
  185.             {
  186.                 int iy = y + dy[k], ix = x + dx[k];
  187.                 if (iy >= 0 && iy < H && ix >= 0 && ix < W &&
  188.                      isMached[ix][iy] == d)
  189.                 {
  190.                     x = x + dx[k];
  191.                     y = y + dy[k];           // переходим в ячейку, которая на 1 ближе к старту
  192.                     break;
  193.                 }
  194.             }
  195.         }
  196.         /*
  197.         Tile t = new Tile();
  198.         t.type = map[bx][by];
  199.         t.x = bx;
  200.         t.y = by;
  201.         way.Insert(0,t);
  202.         */
  203.         //px[0] = ax;
  204.         //py[0] = ay;                    // теперь px[0..len] и py[0..len] - координаты ячеек пути
  205.         return true;
  206.     }
  207.  
  208.     private void ClearIsMached()
  209.     {
  210.         if (isMached != null)
  211.         {
  212.             for (int i = 0; i < isMached.Length; i++)
  213.                 for (int j = 0; j < isMached[i].Length; j++)
  214.                 {
  215.                     int t = -2;
  216.                     if (map[i][j] == TileType.Empty || map[i][j] == TileType.Unknown)
  217.                         t = -1;
  218.                     isMached[i][j] = t;
  219.                 }
  220.         }
  221.         else
  222.         {
  223.             isMached = new int[map.Length][];
  224.             for (int i = 0; i < isMached.Length; i++)
  225.                 isMached[i] = new int[map[0].Length];
  226.  
  227.             ClearIsMached();
  228.         }
  229.     }
  230. }
  231.     public class Tile
  232.     {
  233.         public int x;
  234.         public int y;
  235.  
  236.         public Vector wpos;
  237.  
  238.         public TileType type;
  239.  
  240.         public Tile()
  241.         {
  242.             wpos = new Vector();
  243.             type = TileType.Unknown;
  244.             x = 0;
  245.             y = 0;
  246.         }
  247.     public override string ToString()
  248.     {
  249.         return string.Format("pos:{0};{1}| type:{2}", x, y, type.ToString());
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement