【问题标题】:Finding number of rooms in castle查找城堡的房间数量
【发布时间】:2021-05-16 23:49:34
【问题描述】:

如果您有给定的城堡,以及以下信息:

4

7

11 6 11 6 3 10 6

7 9 6 13 5 15 5

1 10 12 7 13 7 5

13 11 10 8 10 12 13

前两行是两个整数,分别是城堡南北和东西方向的单元格数量。在接下来的输入行中,每个单元格由一个数字 (0 ≤ p ≤ 50) 描述。用数字表示广场周围的墙,1代表西墙,2代表北墙,4代表东墙,8代表南墙。每个正方形由代表周围墙壁的数字之和表示。城堡的内墙算了两次,广场的南墙(1, 1)也是广场的北墙(2, 1)。

它在给定的城堡上工作得很好,但如果我设计其他城堡,它就不再工作了,它提供的房间数量很差。你看到这里的问题了吗? 我应该用 c# 语言来实现它。到目前为止,这就是我的代码的样子。

private void CheckRoom(int i, int j)
    {
        if((i<0 | i>pilis.m) | (j<0 | j > pilis.n))
        {
            return;
        }
        // If room was visited, we return
        if (pilis.ImtiKambarioLankomuma(i,j) == true)
        {
            return;
        }
        pilis.DetiKambarioLankomuma(i, j, true);
        if ((pilis.ImtiKambari(i,j) & 1) == 0) CheckRoom(i, j - 1);
        if ((pilis.ImtiKambari(i,j) & 2) == 0) CheckRoom(i - 1, j);
        if ((pilis.ImtiKambari(i,j) & 4) == 0) CheckRoom(i, j + 1);
        if ((pilis.ImtiKambari(i,j) & 8) == 0) CheckRoom(i + 1, j);
    }

    private void CountRooms(Pilis pilis)
    {
        for (int i = 0; i <= pilis.m; i++)
        {
            for (int j = 0; j <= pilis.n; j++)
            {
                if (pilis.ImtiKambarioLankomuma(i,j) == false) //Indicates a new room that has not been entered 
                {
                    //adds +1 to total room count
                    pilis.DidintiKambariuSuma();
                    CheckRoom(i, j);
                }
            }
        }
    }

【问题讨论】:

    标签: c# algorithm recursion


    【解决方案1】:

    您的循环有错误的上限。应该是&lt; 而不是&lt;=

    for (int i = 0; i <= pilis.m; i++)
            {
                for (int j = 0; j <= pilis.n; j++)
    

    还有

    if((i<0 | i>=pilis.m) | (j<0 | j >= pilis.n))
    

    【讨论】:

      【解决方案2】:

      这看起来很有趣,所以我决定尝试一下。

      输出:

      Castle Data:
      4
      7
      11 6 11 6 3 10 6
      7 9 6 13 5 15 5
      1 10 12 7 13 7 5
      13 11 10 8 10 12 13
      
      Castle:
      +--+--+--+--+--+--+--+
      |     |     |        |
      +--+  +--+  +  +--+  +
      |  |     |  |  |  |  |
      +  +--+  +--+  +--+  +
      |        |  |  |  |  |
      +  +--+--+  +--+  +  +
      |  |              |  |
      +--+--+--+--+--+--+--+
      
      Searching for rooms...
      New room found at row 0, col 0.
      New room found at row 0, col 2.
      New room found at row 0, col 4.
      New room found at row 1, col 5.
      New room found at row 2, col 3.
      
      Total rooms: 5
      
      Press any key to quit...
      

      代码:

      class Program
      {
      
          static void Main(string[] args)
          {
              string data =
                  "4" + Environment.NewLine +
                  "7" + Environment.NewLine +
                  "11 6 11 6 3 10 6" + Environment.NewLine +
                  "7 9 6 13 5 15 5" + Environment.NewLine +
                  "1 10 12 7 13 7 5" + Environment.NewLine +
                  "13 11 10 8 10 12 13" + Environment.NewLine;
      
              string[] dataFromFile = data.Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries);
              Console.WriteLine("Castle Data:");
              foreach(string row in dataFromFile)
              {
                  Console.WriteLine(row);
              }
              Console.WriteLine();
      
              CastleRoom[,] castle = CastleFromData(dataFromFile);
              if (castle != null)
              {
                  Console.WriteLine("Castle:");
                  DisplayCastle(castle);
                  Console.WriteLine();
      
                  Console.WriteLine("Searching for rooms...");
                  int roomCount = 0;
                  for(int row=0; row<castle.GetLength(0);row++)
                  {
                      for(int col=0; col<castle.GetLength(1); col++)
                      {
                          if (!castle[row, col].Visited)
                          {
                              Console.WriteLine("New room found at row " + row + ", col " + col + ".");
                              roomCount++;
                              VisitRoomAt(castle, row, col);
                          }
                      }                    
                  }
                  Console.WriteLine();
      
                  Console.WriteLine("Total rooms: " + roomCount);
                  Console.WriteLine();
              }
      
              Console.Write("Press any key to quit...");
              Console.ReadKey();
          }
      
          static void VisitRoomAt(CastleRoom[,] castle, int row, int col)
          {
              if (castle!=null && row>=0 && row<=castle.GetUpperBound(0) && col>=0 && col<=castle.GetUpperBound(1))
              {
                  CastleRoom room = castle[row, col];
                  if (room!=null && !room.Visited)
                  {
                      room.Visited = true;
                      if (!room.HasWestWall) { VisitRoomAt(castle, row, col-1); }
                      if (!room.HasNorthWall) { VisitRoomAt(castle, row-1, col); }
                      if (!room.HasEastWall) { VisitRoomAt(castle, row, col + 1); }
                      if (!room.HasSouthWall) { VisitRoomAt(castle, row + 1, col); }
                  }                
              }
          }
      
          static void DisplayCastle(CastleRoom[,] castle)
          {
              for (int row = 0; row < castle.GetLength(0); row++)
              {
                  for (int col = 0; col < castle.GetLength(1); col++)
                  {
                      Console.Write("+" + (castle[row, col].HasNorthWall ? "--" : "  "));
                  }
                  Console.WriteLine("+");
                  for (int col = 0; col < castle.GetLength(1); col++)
                  {
                      Console.Write((castle[row, col].HasWestWall ? "|" : " ") + "  ");
                      if (col == (castle.GetLength(1) - 1) && castle[row, col].HasEastWall)
                      {
                          Console.Write("|");
                      }
                  }
                  Console.WriteLine();
                  if (row == (castle.GetLength(0)-1))
                  {
                      for (int col = 0; col < castle.GetLength(1); col++)
                      {
                          Console.Write("+" + (castle[row, col].HasSouthWall ? "--" : "  "));
                      }
                      Console.WriteLine("+");
                  }
              }
          }
      
          static CastleRoom[,] CastleFromData(string[] castleData)
          {
              int rows, columns;
              if (!int.TryParse(castleData[0], out rows) || !int.TryParse(castleData[1], out columns))
              {
                  return null;
              }
              if (rows <= 0 || columns <= 0)
              {
                  return null;
              }
              if (castleData.Length != (rows + 2))
              {
                  return null;
              }
      
              CastleRoom[,] castle = new CastleRoom[rows, columns];
              for (int row = 2; row < castleData.Length; row++)
              {
                  string[] rowData = castleData[row].Split();
                  if (rowData.Length != columns)
                  {
                      return null;
                  }
      
                  int roomValue;
                  for (int col = 0; col < rowData.Length; col++)
                  {
                      if (!int.TryParse(rowData[col], out roomValue))
                      {
                          return null;
                      }
                      if (roomValue < 0 || roomValue > 50)
                      {
                          return null;
                      }
                      castle[(row - 2), col] = new CastleRoom(roomValue);
                  }
              }
              return castle;
          }
      
      }
      
      class CastleRoom
      {
      
          [Flags] public enum CastleWalls
          {
              West = 1,
              North = 2,
              East = 4,
              South = 8
          }
      
          private CastleWalls _walls;
      
          public bool Visited = false;
      
          public CastleRoom(int walls)
          {
              _walls = (CastleWalls)walls;
          }
      
          public bool HasWestWall
          {
              get
              {
                  return _walls.HasFlag(CastleWalls.West);
              }
          }
      
          public bool HasNorthWall
          {
              get
              {
                  return _walls.HasFlag(CastleWalls.North);
              }
          }
      
          public bool HasEastWall
          {
              get
              {
                  return _walls.HasFlag(CastleWalls.East);
              }
          }
      
          public bool HasSouthWall
          {
              get
              {
                  return _walls.HasFlag(CastleWalls.South);
              }
          }
      
          public override string ToString()
          {
              return ((int)_walls).ToString();
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-21
        相关资源
        最近更新 更多