Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    problema allocazione dinamica c++

    Buonasera a tutti ho un problema con questo sudoku in particolare quando lo compilo ma mi crasha. sono sicuro sia l' allocazione dinamica perchè lo stesso programma con i define e typedef funziona. dove sbaglio?
    codice:
     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include<fstream>
    #include<math.h>
    #define UNASSIGNED 0
    //#define dim 25
    //typedef int griglia[dim][dim];
    /*using std::cout;
    using std::cin;
    using std::endl;
    using std::ifstream;*/
    using namespace std;
    
    class sudoku{
    
        private:
            int row,col,num,boxStartRow,boxStartCol;
    
    
        public:
            bool FindUnassignedLocation(int **g, int &row, int &col,int n);
            bool isSafe(int **g, int row, int col, int num,int n);
            bool SolveSudoku(int **g,int n);
            bool UsedInRow(int **g, int row, int num,int n);
            bool UsedInCol(int **g, int col, int num,int n);
            bool UsedInBox(int **g, int boxStartRow, int boxStartCol, int num,int n);
            void printG(int **g,int n);
            void Crea_matrice(int **g,int &n);
    };
    
     bool sudoku::SolveSudoku(int **g,int n)
    
        {
            static int cont=1;
            cout<<"Il numero dei cicli di backtracking effettuati sono: "<<cont++<<endl;
            int row, col;
    
            if (!FindUnassignedLocation(g, row, col,n))
    
               return true;
    
            for (int num = 1; num <= n; num++)
    
            {
    
                if (isSafe(g, row, col, num,n))
    
                {
    
                    g[row][col] = num;
    
                    if (SolveSudoku(g,n))
    
                        return true;
    
                    g[row][col] = UNASSIGNED;
    
                }
    
            }
    
            return false;
    
        }
    
    
    
        /* Searches the grid to find an entry that is still unassigned. */
    
    bool sudoku::FindUnassignedLocation(int **g, int &row, int &col,int n)
    
        {
    
            for (row = 0; row < n; row++)
    
                for (col = 0; col < n; col++)
    
                    if (g[row][col] == UNASSIGNED)
    
                        return true;
    
            return false;
    
        }
    
    
    
        /* Returns whether any assigned entry n the specified row matches
    
           the given number. */
    
    bool sudoku::UsedInRow(int **g, int row, int num,int n)
    
        {
    
            for (int col = 0; col < n; col++)
    
                if (g[row][col] == num)
    
                    return true;
    
            return false;
    
        }
    
    
    
        /* Returns whether any assigned entry in the specified column matches
    
           the given number. */
    
    bool sudoku::UsedInCol(int **g, int col, int num,int n)
    
        {
    
            for (int row = 0; row < n; row++)
    
                if (g[row][col] == num)
    
                    return true;
    
            return false;
    
        }
    
    
    
        /* Returns whether any assigned entry within the specified 3x3 box matches
    
           the given number. */
    
    bool sudoku::UsedInBox(int **g, int boxStartRow, int boxStartCol, int num,int n)
    
        {
    
            for (int row = 0; row < (int)sqrt(n); row++) //IN SUDOKU 9*9 ERA row<3
                for (int col = 0; col < (int)sqrt(n); col++) //IN SUDOKU 9*9 ERA row<3
                    if (g[row+boxStartRow][col+boxStartCol] == num)
                        return true;
            return false;
    
        }
    
    
    
        /* Returns whether it will be legal to assign num to the given row,col location.
    
         */
    
    bool sudoku::isSafe(int **g, int row, int col, int num,int n)
    
        {
            return !UsedInRow(g, row, num,n) && !UsedInCol(g, col, num,n) &&
                   !UsedInBox(g, row - row % (int)sqrt(n) , col - col % (int)sqrt(n), num,n);
    
    
        }
    
    
    
        /* Stampa della griglia  */
    
    void sudoku::printG(int **g,int n)
    
        {
    
            for (int row = 0; row < n; row++)
    
            {
    
                for (int col = 0; col < n; col++)
    
                    cout<<g[row][col]<<"  ";
    
                cout<<endl;
    
            }
    
        }
    
    void sudoku::Crea_matrice(int **g,int &n){
                char *nomefile=new char[10];
                cout<<"Inserire il nome del file: ";
                cin>>nomefile;
                cout<<endl;
                ifstream fin(nomefile);
                fin>>n;
    
                g = new( int * );
                for(int i=0;i<n;i++)
                g[i] = new int;
                for (int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        fin>>g[i][j];
                    }
                }
                fin.close();
                delete [] nomefile;
                delete  g;
            }
    
    
    
    
    
     int main()
    
    
        {
                int n;
                sudoku s;
                int **g;
                //griglia g;
                //int n;
    
                s.Crea_matrice(g,n);
                cout<<"La soluzione del sudoku e': \n";
    
    
    
    
            if (s.SolveSudoku(g,n) == true)
    
            s.printG(g,n);
    
            else
    
                cout<<"Non esistono soluzioni."<<endl;
    
            return 0;
    
        }

  2. #2
    parlo logicamente del metodo Crea_matrice.

  3. #3
    Alcune inesattezze nel metodo Crea_matrice, l'errore effettivamente era nella creazione della matrice.

    codice:
    void sudoku::Crea_matrice(int **&g,int &n) // devi passare entrambe le variabili per riferimento
    {
        char *nomefile=new char[260]; // 10 char sono troppo pochi, 
        // per i nomi dei file meglio usarne almeno 260
        cout<<"Inserire il nome del file: ";
        cin>>nomefile;
        ifstream fin(nomefile);
        if( ! fin.is_open() ) // controllo che il file sia aperto
            return;
        fin>>n;
        
       // Quì ci vorrebbe un controllo sulla matrice **g, e se esiste già, cancellarla.
    
        g = new int* [n];
        for( int i=0; i<n; i++ )
            g[i] = new int [n];
            
        for( int i=0; i<n; i++ )
        {
            for( int j=0; j<n; j++ )
                fin>>g[i][j];
        }
        fin.close();
        delete [] nomefile;
        // delete  g; // la memoria di una matrice non rilascia così, ma poi... Perché cancellare la matrice appena creata ?
    }
    inizializza sempre le variabili, mi riferisco a **g e n nella main(), che non fa mai male.
    01010011 01100001 01101101 01110101 01100101 01101100 01100101 01011111 00110111 00110000
    All errors are undocumented features waiting to be discovered.

  4. #4
    grazie mille è proprio vero che in questo forum ci sono i migliori !

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.