首页 > 解决方案 > 如何在 C# 中打印 char[,]?

问题描述

using System;

namespace cis237_assignment2
{
class Program
{
    /// <summary>
    /// This is the main entry point for the program.
    /// You are free to add anything else you would like to this program,
    /// however the maze solving part needs to occur in the MazeSolver class.
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        // Starting Coordinates.
        const int X_START = 1;
        const int Y_START = 1;

        // The first maze that needs to be solved.
        // Note: You may want to make a smaller version to test and debug with.
        // You don't have to, but it might make your life easier.
        char[,] maze1 =
        { { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
        { '#', '.', '.', '.', '#', '.', '.', '.', '.', '.', '.', '#' },
        { '#', '.', '#', '.', '#', '.', '#', '#', '#', '#', '.', '#' },
        { '#', '#', '#', '.', '#', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '#', '#', '#', '.', '#', '.', '.' },
        { '#', '#', '#', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '#', '.', '#', '.', '#', '.', '#', '.', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '.', '.', '#', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '.', '#', '#', '#', '.', '#' },
        { '#', '.', '.', '.', '.', '.', '.', '#', '.', '.', '.', '#' },
        { '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' } };

        // Create a new instance of a mazeSolver.
        MazeSolver mazeSolver = new MazeSolver();

        Console.WriteLine("{0}", maze1);



        // Create the second maze by transposing the first maze
        char[,] maze2 = transposeMaze(maze1);

        // Solve the original maze.
        mazeSolver.SolveMaze(maze1, X_START, Y_START);

        // Solve the transposed maze.
        mazeSolver.SolveMaze(maze2, X_START, Y_START);

    }

    /// <summary>
    /// This method will take in a 2 dimensional char array and return
    /// a char array maze that is flipped along the diagonal, or in mathematical
    /// terms, transposed.
    /// ie. if the array looks like 1, 2, 3
    ///                             4, 5, 6
    ///                             7, 8, 9
    /// The returned result will be:
    ///                             1, 4, 7
    ///                             2, 5, 8
    ///                             3, 6, 9
    /// The current return statement is just a placeholder so the program
    /// doesn't complain about no return value.
    /// 
    /// It is important that you return a "new" char array as the transposed maze.
    /// If you do not, you could end up only solving the transposed maze.
    /// </summary>
    /// <param name="mazeToTranspose"></param>
    /// <returns>transposedMaze</returns>
    static char[,] transposeMaze(char[,] mazeToTranspose)
    {
        //Write code her to create a transposed maze.
        return new char[1, 1];
    }
}

}

标签: c#

解决方案


我假设您在询问如何迭代多维数组..

该链接可以为您提供很大的帮助,但是,简而言之,如果您知道它是二维的,那么它非常简单

///char[,] arr2D
for (int i=0; i<arr2D.GetLength(0); i++)
{
    for(int j=0; j<arr2D.GetLength(1); j++)
      Console.Write((j>0)?", ":"" + arr2D[i, j]);
    Conole.WriteLine();
}


推荐阅读