简体   繁体   English

在 C# 中使用秒表

[英]Using Stopwatch in C#

Does anyone know how I can add a timer function to this code that solves sudoku puzzles?有谁知道我如何向这段解决数独谜题的代码添加计时器功能? I've tried the Stopwatch class but I can't get it to work because I don't know exactly where to put it.我试过 Stopwatch 类,但我无法让它工作,因为我不知道该把它放在哪里。 I've tried several places but always get errors.我试过几个地方,但总是出错。 The main purpose of the code works but I'd like to add a timer function to see how long the code ran for.该代码的主要目的是有效的,但我想添加一个计时器函数来查看代码运行了多长时间。

using System;

namespace SuDoKu
{
  public class SuDoKu
  {
    private int[,] grid;

    public SuDoKu()
    { 
        grid = new int[9, 9];
    } 
     static void Main(string[] args)
     {
        SuDoKu sdk = new SuDoKu();

        int[,] grd = {
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };

        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                sdk.EnterToGrid(grd[i, j], new Point(i, j));

        Console.WriteLine("Original Grid"); 

        sdk.Display(); 

        sdk.Solve(sdk.NextAvailableCell(), new Point()); 

        Console.WriteLine("\n\n\nSolved Grid"); 

        sdk.Display(); 

        Console.WriteLine();
    }

    public void Display()
    {
        for(int i = 0; i < 9; i++)
        {
            Console.WriteLine();

            for(int j = 0; j < 9; j++)
            {
                if(ShowFromGrid(new Point(i, j)) == 0) {
                    Console.Write("0 ");
                }
                else
                {
                    Console.Write(ShowFromGrid(new Point(i, j)) + " ");
                }

                if(j == 2 || j == 5)
                {
                    Console.Write("| ");
                }
            }
            if(i == 2 || i == 5) {

                Console.Write("\n------|-------|------ ");
            }
        }
    }
    public void EnterToGrid(int num, Point pos) {
        grid[pos.X, pos.Y] = num;
    }

    public int ShowFromGrid(Point pos) {
        return grid[pos.X, pos.Y];
    }

    public void Solve(Point pos, Point prevPos) {

        if(pos.X < 9 && pos.Y < 9)
        {
            Point posPrev = new Point();

            if(grid[pos.X, pos.Y] == 0)
            {
                for(int i = 1; i <= 9; i++)
                {
                    if(IsThisNumberPossibleInTheGrid(i, pos))
                    {
                        grid[pos.X, pos.Y] = i;

                        posPrev.X = pos.X;

                        posPrev.Y = pos.Y;

                        Point posNext = NextAvailableCell();

                        if(!posNext.Equals(new Point()))

                            Solve(posNext, posPrev);
                    }
                }
                if(grid[pos.X, pos.Y] == 0)
                {
                    if(!prevPos.Equals(new Point()))

                        grid[prevPos.X, prevPos.Y] = 0; 

                    return;
                }
            }
        }
        else
        {
            Point posNext = NextAvailableCell();

            if(!posNext.Equals(new Point()))
                Solve(posNext, pos);
        }
    }

    public Point NextAvailableCell()
    {
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                if(grid[i, j] == 0)
                    return new Point(i, j);

        return new Point();
    }

    public bool IsThisNumberPossibleInTheGrid(int num, Point pos) {

        if(IsThisNumberPossibleInTheBlock(num, pos))
        {
            for(int i = 0; i < 9; i++)
            {
                if(grid[i, pos.Y] == num && pos.X != i)
                {
                    return false;
                }

                if(grid[pos.X, i] == num && pos.Y != i)
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public bool IsThisNumberPossibleInTheBlock(int num, Point pos)
    {
        Point Grid = new Point();

        Grid.X = (pos.X >= 0 && pos.X <= 2) ? 0 : ((pos.X >= 3 && pos.X <= 5) ? 3 : ((pos.X >= 6 && pos.X <= 8) ? 6 : -1));
        Grid.Y = (pos.Y >= 0 && pos.Y <= 2) ? 0 : ((pos.Y >= 3 && pos.Y <= 5) ? 3 : ((pos.Y >= 6 && pos.Y <= 8) ? 6 : -1));

        if(!Grid.Equals(new Point()))
        {
            for(int i = Grid.X; i < Grid.X + 3; i++)
            {
                for(int j = Grid.Y; j < Grid.Y + 3; j++)
                {
                    if(grid[i, j] == num && !pos.Equals(new Point(i, j)))
                        return false;
                }
            }
            return true;
        }
        return false;
    }
}

  public class Point
  {
    public int X;

    public int Y;

    public Point()
    {
        this.X = -1;
        this.Y = -1;
    }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    public bool Equals(Point p)
    {
        return (this.X == p.X && this.Y == p.Y) ? true : false;
    }
  }
}

Does anyone know how I can add a timer function to this code that solves sudoku puzzles?有谁知道我如何向这个解决数独谜题的代码添加一个计时器功能? I've tried the Stopwatch class but I can't get it to work because I don't know exactly where to put it.我试过秒表课,但我无法让它工作,因为我不知道把它放在哪里。 I've tried several places but always get errors.我试过好几个地方,但总是出错。 The main purpose of the code works but I'd like to add a timer function to see how long the code ran for.代码的主要目的有效,但我想添加一个计时器功能来查看代码运行了多长时间。

using System;

namespace SuDoKu
{
  public class SuDoKu
  {
    private int[,] grid;

    public SuDoKu()
    { 
        grid = new int[9, 9];
    } 
     static void Main(string[] args)
     {
        SuDoKu sdk = new SuDoKu();

        int[,] grd = {
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };

        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                sdk.EnterToGrid(grd[i, j], new Point(i, j));

        Console.WriteLine("Original Grid"); 

        sdk.Display(); 

        sdk.Solve(sdk.NextAvailableCell(), new Point()); 

        Console.WriteLine("\n\n\nSolved Grid"); 

        sdk.Display(); 

        Console.WriteLine();
    }

    public void Display()
    {
        for(int i = 0; i < 9; i++)
        {
            Console.WriteLine();

            for(int j = 0; j < 9; j++)
            {
                if(ShowFromGrid(new Point(i, j)) == 0) {
                    Console.Write("0 ");
                }
                else
                {
                    Console.Write(ShowFromGrid(new Point(i, j)) + " ");
                }

                if(j == 2 || j == 5)
                {
                    Console.Write("| ");
                }
            }
            if(i == 2 || i == 5) {

                Console.Write("\n------|-------|------ ");
            }
        }
    }
    public void EnterToGrid(int num, Point pos) {
        grid[pos.X, pos.Y] = num;
    }

    public int ShowFromGrid(Point pos) {
        return grid[pos.X, pos.Y];
    }

    public void Solve(Point pos, Point prevPos) {

        if(pos.X < 9 && pos.Y < 9)
        {
            Point posPrev = new Point();

            if(grid[pos.X, pos.Y] == 0)
            {
                for(int i = 1; i <= 9; i++)
                {
                    if(IsThisNumberPossibleInTheGrid(i, pos))
                    {
                        grid[pos.X, pos.Y] = i;

                        posPrev.X = pos.X;

                        posPrev.Y = pos.Y;

                        Point posNext = NextAvailableCell();

                        if(!posNext.Equals(new Point()))

                            Solve(posNext, posPrev);
                    }
                }
                if(grid[pos.X, pos.Y] == 0)
                {
                    if(!prevPos.Equals(new Point()))

                        grid[prevPos.X, prevPos.Y] = 0; 

                    return;
                }
            }
        }
        else
        {
            Point posNext = NextAvailableCell();

            if(!posNext.Equals(new Point()))
                Solve(posNext, pos);
        }
    }

    public Point NextAvailableCell()
    {
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                if(grid[i, j] == 0)
                    return new Point(i, j);

        return new Point();
    }

    public bool IsThisNumberPossibleInTheGrid(int num, Point pos) {

        if(IsThisNumberPossibleInTheBlock(num, pos))
        {
            for(int i = 0; i < 9; i++)
            {
                if(grid[i, pos.Y] == num && pos.X != i)
                {
                    return false;
                }

                if(grid[pos.X, i] == num && pos.Y != i)
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public bool IsThisNumberPossibleInTheBlock(int num, Point pos)
    {
        Point Grid = new Point();

        Grid.X = (pos.X >= 0 && pos.X <= 2) ? 0 : ((pos.X >= 3 && pos.X <= 5) ? 3 : ((pos.X >= 6 && pos.X <= 8) ? 6 : -1));
        Grid.Y = (pos.Y >= 0 && pos.Y <= 2) ? 0 : ((pos.Y >= 3 && pos.Y <= 5) ? 3 : ((pos.Y >= 6 && pos.Y <= 8) ? 6 : -1));

        if(!Grid.Equals(new Point()))
        {
            for(int i = Grid.X; i < Grid.X + 3; i++)
            {
                for(int j = Grid.Y; j < Grid.Y + 3; j++)
                {
                    if(grid[i, j] == num && !pos.Equals(new Point(i, j)))
                        return false;
                }
            }
            return true;
        }
        return false;
    }
}

  public class Point
  {
    public int X;

    public int Y;

    public Point()
    {
        this.X = -1;
        this.Y = -1;
    }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    public bool Equals(Point p)
    {
        return (this.X == p.X && this.Y == p.Y) ? true : false;
    }
  }
}

Does anyone know how I can add a timer function to this code that solves sudoku puzzles?有谁知道我如何向这个解决数独谜题的代码添加一个计时器功能? I've tried the Stopwatch class but I can't get it to work because I don't know exactly where to put it.我试过秒表课,但我无法让它工作,因为我不知道把它放在哪里。 I've tried several places but always get errors.我试过好几个地方,但总是出错。 The main purpose of the code works but I'd like to add a timer function to see how long the code ran for.代码的主要目的有效,但我想添加一个计时器功能来查看代码运行了多长时间。

using System;

namespace SuDoKu
{
  public class SuDoKu
  {
    private int[,] grid;

    public SuDoKu()
    { 
        grid = new int[9, 9];
    } 
     static void Main(string[] args)
     {
        SuDoKu sdk = new SuDoKu();

        int[,] grd = {
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                     { 0, 0, 0, 0, 0, 0, 0, 0, 0 } };

        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                sdk.EnterToGrid(grd[i, j], new Point(i, j));

        Console.WriteLine("Original Grid"); 

        sdk.Display(); 

        sdk.Solve(sdk.NextAvailableCell(), new Point()); 

        Console.WriteLine("\n\n\nSolved Grid"); 

        sdk.Display(); 

        Console.WriteLine();
    }

    public void Display()
    {
        for(int i = 0; i < 9; i++)
        {
            Console.WriteLine();

            for(int j = 0; j < 9; j++)
            {
                if(ShowFromGrid(new Point(i, j)) == 0) {
                    Console.Write("0 ");
                }
                else
                {
                    Console.Write(ShowFromGrid(new Point(i, j)) + " ");
                }

                if(j == 2 || j == 5)
                {
                    Console.Write("| ");
                }
            }
            if(i == 2 || i == 5) {

                Console.Write("\n------|-------|------ ");
            }
        }
    }
    public void EnterToGrid(int num, Point pos) {
        grid[pos.X, pos.Y] = num;
    }

    public int ShowFromGrid(Point pos) {
        return grid[pos.X, pos.Y];
    }

    public void Solve(Point pos, Point prevPos) {

        if(pos.X < 9 && pos.Y < 9)
        {
            Point posPrev = new Point();

            if(grid[pos.X, pos.Y] == 0)
            {
                for(int i = 1; i <= 9; i++)
                {
                    if(IsThisNumberPossibleInTheGrid(i, pos))
                    {
                        grid[pos.X, pos.Y] = i;

                        posPrev.X = pos.X;

                        posPrev.Y = pos.Y;

                        Point posNext = NextAvailableCell();

                        if(!posNext.Equals(new Point()))

                            Solve(posNext, posPrev);
                    }
                }
                if(grid[pos.X, pos.Y] == 0)
                {
                    if(!prevPos.Equals(new Point()))

                        grid[prevPos.X, prevPos.Y] = 0; 

                    return;
                }
            }
        }
        else
        {
            Point posNext = NextAvailableCell();

            if(!posNext.Equals(new Point()))
                Solve(posNext, pos);
        }
    }

    public Point NextAvailableCell()
    {
        for(int i = 0; i < 9; i++)
            for(int j = 0; j < 9; j++)
                if(grid[i, j] == 0)
                    return new Point(i, j);

        return new Point();
    }

    public bool IsThisNumberPossibleInTheGrid(int num, Point pos) {

        if(IsThisNumberPossibleInTheBlock(num, pos))
        {
            for(int i = 0; i < 9; i++)
            {
                if(grid[i, pos.Y] == num && pos.X != i)
                {
                    return false;
                }

                if(grid[pos.X, i] == num && pos.Y != i)
                {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public bool IsThisNumberPossibleInTheBlock(int num, Point pos)
    {
        Point Grid = new Point();

        Grid.X = (pos.X >= 0 && pos.X <= 2) ? 0 : ((pos.X >= 3 && pos.X <= 5) ? 3 : ((pos.X >= 6 && pos.X <= 8) ? 6 : -1));
        Grid.Y = (pos.Y >= 0 && pos.Y <= 2) ? 0 : ((pos.Y >= 3 && pos.Y <= 5) ? 3 : ((pos.Y >= 6 && pos.Y <= 8) ? 6 : -1));

        if(!Grid.Equals(new Point()))
        {
            for(int i = Grid.X; i < Grid.X + 3; i++)
            {
                for(int j = Grid.Y; j < Grid.Y + 3; j++)
                {
                    if(grid[i, j] == num && !pos.Equals(new Point(i, j)))
                        return false;
                }
            }
            return true;
        }
        return false;
    }
}

  public class Point
  {
    public int X;

    public int Y;

    public Point()
    {
        this.X = -1;
        this.Y = -1;
    }

    public Point(int x, int y)
    {
        this.X = x;
        this.Y = y;
    }

    public bool Equals(Point p)
    {
        return (this.X == p.X && this.Y == p.Y) ? true : false;
    }
  }
}

In .NET 7.0 there were some improvements in the Stopwatch class..NET 7.0中, Stopwatch类有一些改进。 There is a better way to use Stopwatch that will be more accurate, slightly faster, and won't use memory allocation at all!有一种更好的使用Stopwatch的方法,它会更准确、速度稍快,而且根本不会使用内存分配!

You should use the methods Stopwatch.GetTimeStamp() & Stopwatch.GetElapsedTime(long) to make the improvements.您应该使用Stopwatch.GetTimeStamp()Stopwatch.GetElapsedTime(long)方法进行改进。

An example would be:一个例子是:

long startTime = Stopwatch.GetTimestamp();
// Do Something...
TimeSpan elapsedTime = Stopwatch.GetElapsedTime(startTime);

For more info, read the Stopwatch in .NET 7.0 documentation .有关详细信息,请阅读.NET 7.0 文档中的秒表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM