[Python] How to translate python recursivity into c#? [closed]

Discussão em 'Python' iniciado por Stack, Março 10, 2025.

  1. Stack

    Stack Membro Participativo

    I wanted to translate this program written in python in c# but I have a problem with recursivity !

    Here is python program:

    grid = [[1,0,5,0,0,0,4,6,9],
    [0,0,6,9,0,0,0,0,0],
    [9,8,2,0,0,6,3,0,7],
    [2,0,8,0,0,0,6,0,0],
    [6,9,1,7,0,8,0,0,0],
    [7,5,0,0,6,0,0,0,1],
    [0,1,0,6,9,0,7,0,0],
    [0,6,0,0,3,0,0,0,5],
    [0,2,9,0,0,0,0,0,6]]

    def n_valide(y, x, n):

    global grid
    #On détermine si le nombre est valide sur sa ligne
    for x0 in range(len(grid)):
    if grid[y][x0] == n:
    return False

    #On détermine si le nombre est valide sur sa colonne
    for y0 in range(len(grid)):
    if grid[y0][x] == n:
    return False

    x0 = (x//3) * 3
    y0= (y//3) * 3

    for i in range(0,3):
    for j in range(0,3):
    if grid[y0+i][x0+j] == n:
    return False
    return True

    def solve():
    global grid
    for y in range(9):
    for x in range(9):
    if grid[y][x] == 0:
    for n in range(1,10):
    if n_valide(y, x, n):
    grid[y][x] = n
    solve()
    grid[y][x] = 0
    return
    for i in range(9):
    for j in range(9):
    print(grid[j], end="")
    print()
    exit(0)

    solve()


    This program works fine ! Here is my translation :

    class Program
    {
    static int[,] grid = new int[9, 9] {
    {1,0,5,0,0,0,4,6,9},
    {0,0,6,9,0,0,0,0,0},
    {9,8,2,0,0,6,3,0,7},
    {2,0,8,0,0,0,6,0,0},
    {6,9,1,7,0,8,0,0,0},
    {7,5,0,0,6,0,0,0,1},
    {0,1,0,6,9,0,7,0,0},
    {0,6,0,0,3,0,0,0,5},
    {0,2,9,0,0,0,0,0,6}
    };
    static void Main(string[] args)
    {


    solve();
    display();
    Console.ReadKey();
    }


    static void solve()
    {
    bool numberInGrid;
    for (int l = 0; l < 9; l++)
    {
    for (int c = 0; c < 9; c++)
    {
    if (grid[l, c] == 0)
    {
    numberInGrid = true;
    for (int val = 1; val < 10; val++)
    {
    numberInGrid = IsInGrid(l, c, val);
    if (!numberInGrid)
    {
    grid[l, c] = val;
    solve();
    grid[l, c] = 0;
    }
    }
    return;
    }
    }
    }
    }

    static bool IsInGrid(int line, int column, int val)
    {
    bool result = false;

    for (int i = 0; i < 9; i++)
    {
    int n = grid[line, i];
    if (n == val)
    {
    result = true;
    break;
    }
    }
    if (!result)
    {
    for (int i = 0; i < 9; i++)
    {
    if (grid[i, column] == val)
    {
    result = true;
    break;
    }
    }
    }
    if (!result)
    {
    int beginColumnSquare = (column / 3) * 3;
    int beginLineSquare = (line / 3) * 3;
    for (int c = 0; c < 3; c++)
    {
    for (int l = 0; l < 3; l++)
    {
    if (grid[beginLineSquare + c, beginColumnSquare + l] == val)
    {
    result = true;
    break;
    }
    }
    }
    }

    return result;
    }
    static void display()
    {
    for (int l = 0; l < 9; l++)
    {
    for (int c = 0; c < 9; c++)
    {
    Console.Write(grid[l, c]);
    }
    Console.WriteLine();
    }
    }
    }


    I think that the error is when I call solve() at the bad place because the line grid[l, c] = 0; is systematicaly executed so even a number has been written in the grid it will be replaced by a 0 My program doesn't work :-( Any idea ? Thanks for your help :)

    Continue reading...

Compartilhe esta Página