Exemplo n.º 1
0
/* Try to find ALL the solutions, so we check every possible combination. */
void
next_queen (int (*board)[COL_MAX + 1], int row, List* list)
{
    int col;
    /* printf ("next_q board=%d\n", &board[0][0]);  */
    for (col = 0; col < COL_MAX + 1; col++) {
        /* printf ("row=%d,col=%d\n", row, col);  */
        if (!is_attacking(board, row, col)) {
            board[row][col] = occupied;
            if (row == ROW_MAX) {
                if (new_solution (board, list)) {
                    print_solution (board);
                }
            }
            else {
                next_queen (board, ++row, list);
                row--;
            }
        }
        board[row][col] = empty; /* Remove the queen to try the next col. */
    }
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
    char *filename;
    Puzzle *puz;
    SolutionList *sl;
    Solution *sol= NULL;
    line_t i,k;
    int left,rc;
    int dump= 0;
    line_t *pos, *bcl;

    if (argc != 5)
    {
        printf("usage: %s <file> [R|C] <n> [L|R]\n", argv[0]);
        exit(1);
    }

    filename= argv[1];
    k= ((argv[2][0] == 'r' || argv[2][0] == 'R') ? 0 : 1);
    i= atoi(argv[3]);
    left= (argv[4][0] == 'l' || argv[4][0] == 'L');

    puz= load_puzzle_file(filename, FF_UNKNOWN, 1);

    /* Print the name of the puzzle */
    if (puz->id != NULL || puz->title != NULL)
    {
        if (puz->id != NULL) printf("%s: ", puz->id);
        if (puz->seriestitle != NULL) printf("%s ",puz->seriestitle);
        if (puz->title != NULL) fputs(puz->title, stdout);
        putchar('\n');
    }

    if (dump) dump_puzzle(stdout,puz);

    for (sl= puz->sol; sl != NULL; sl= sl->next)
    {
        if (sl->type == STYPE_SAVED)
        {
            sol= &sl->s;
            printf("starting from saved grid\n");
            break;
        }
    }

    /* Start from a blank grid if we didn't start from a saved game */
    if (sol == NULL)
    {
        printf("starting from blank grid\n");
        sol= new_solution(puz);
    }

    if (left)
    {
        printf("LEFT SOLVING:\n");
        rc= left_solve(puz, sol, k, i, 0, &pos, &bcl);
    }
    else
    {
        printf("RIGHT SOLVING:\n");
        rc= right_solve(puz, sol, k, i, 0, &pos, &bcl);
    }

    if (rc)
        printf("CONTRADICTION\n");
    else
    {
        printf("SOLUTION:\n");
        for (i= 0; pos[i] >= 0; i++)
            printf("Block %d length %d at %d\n",i, bcl[i], pos[i]);
    }

    exit(0);
}
Exemplo n.º 3
0
static void read_q (BINARYIO *bf)
{
    int i, k, n, nk, nz, np, nv, nmax;
    int *indices;
    void *data;
    SOLUTION *sol;
    FIELD *flds;
    double qq, rho;
    static char *fldnames[] = {
        "Density",
        "MomentumX",
        "MomentumY",
        "MomentumZ",
        "EnergyStagnationDensity",
        "VelocityX",
        "VelocityY",
        "VelocityZ",
        "Pressure"
    };

    /* get number of grid blocks */

    if (mblock) {
        bf_getints (bf, 1, &nz);
        if (nz != nZones)
            FATAL ("read_q", "number of blocks not the same as the XYZ file");
    }

    /* read indices for grids */

    indices = (int *) malloc (3 * nZones * sizeof(int));
    if (NULL == indices)
        FATAL ("read_q", "malloc failed for grid indices");
    bf_getints (bf, 3 * nZones, indices);
    for (nz = 0; nz < nZones; nz++) {
        for (n = 0; n < 3; n++) {
            if (indices[3*nz+n] != Zones[nz].dim[n])
                FATAL ("read_q", "mismatch in block sizes");
        }
    }
    free (indices);

    /* create solution data arrays */

    for (nmax = 0, nz = 0; nz < nZones; nz++) {
        np = whole ? (int)Zones[nz].nverts : (int)(Zones[nz].dim[0] * Zones[nz].dim[1]);
        if (nmax < np) nmax = np;
        sol = new_solution (1);
        strcpy (sol->name, "FlowSolution");
        sol->location = CGNS_ENUMV(Vertex);
        sol->size = Zones[nz].nverts;
        sol->nflds = 5;
        sol->flds = new_field (5, sol->size);
        for (nv = 0; nv < 5; nv++) {
            strcpy (sol->flds[nv].name, fldnames[nv]);
            sol->flds[nv].datatype = is_double ? CGNS_ENUMV(RealDouble) : CGNS_ENUMV(RealSingle);
        }
        Zones[nz].nsols = 1;
        Zones[nz].sols = sol;
    }

    if (nmax < 4) nmax = 4;
    if (is_double)
        data = (void *) malloc (nmax * sizeof(double));
    else
        data = (void *) malloc (nmax * sizeof(float));
    if (NULL == data)
        FATAL ("read_q", "malloc failed for solution working array");

    /* read the solution data */

    for (nz = 0; nz < nZones; nz++) {
        printf ("reading block %d solution ...", nz+1);
        fflush (stdout);
        if (is_double) {
            bf_getdoubles (bf, 4, data);
            if (0 == nz) {
                for (n = 0; n < 4; n++)
                    reference[n] = ((double *)data)[n];
            }
        }
        else {
            bf_getfloats (bf, 4, data);
            if (0 == nz) {
                for (n = 0; n < 4; n++)
                    reference[n] = ((float *)data)[n];
            }
        }

        if (whole) {
            np = (int)Zones[nz].nverts;
            nk = 1;
        }
        else {
            np = (int)(Zones[nz].dim[0] * Zones[nz].dim[1]);
            nk = (int)Zones[nz].dim[2];
        }
        flds = Zones[nz].sols->flds;
        for (k = 0; k < nk; k++) {
            i = k * np;
            for (nv = 0; nv < 5; nv++) {
                if (is_double) {
                    bf_getdoubles (bf, np, data);
                    for (n = 0; n < np; n++)
                        flds[nv].data[n+i] = ((double *)data)[n];
                }
                else {
                    bf_getfloats (bf, np, data);
                    for (n = 0; n < np; n++)
                        flds[nv].data[n+i] = ((float *)data)[n];
                }
            }
        }

        if (convert) {
            for (nv = 1; nv < 5; nv++)
                strcpy (flds[nv].name, fldnames[4+nv]);
            for (n = 0; n < Zones[nz].nverts; n++) {
                rho = flds[0].data[n];
                for (qq = 0.0, nv = 1; nv < 4; nv++) {
                    flds[nv].data[n] /= rho;
                    qq += flds[nv].data[n] * flds[nv].data[n];
                }
                flds[4].data[n] = (gamma - 1.0) *
                    (flds[4].data[n] - 0.5 * rho * qq);
            }
        }
        puts (" done");
    }

    free (data);
}