Discussione: piccola sfida
View Single Post
Old 15-11-2003, 09:58   #37
Sephiroth83
Member
 
L'Avatar di Sephiroth83
 
Iscritto dal: May 2003
Città: Torino
Messaggi: 63
Per gli appassionati della forza bruta (e del C) ecco un programmino da me realizzato e collaudato.
Partendo dalla posizione (0,0) non ho ancora trovato niente, mentre dalla posizione (5,5) ho già qualche soluzione. Questo programma non è niente di speciale, non ho neanche avuto il tempo di ottimizzarlo (pensavo di togliere la ricorsione) quindi è anche abbastanza lento, però sembra funzionare.
Ecco qualche soluzione trovata:

94 85 69 62 86 23 6 89 22 13
82 72 96 83 71 52 35 25 51 34
98 67 93 54 68 61 91 14 5 90
95 84 70 63 87 24 7 88 21 12
81 73 97 60 92 53 36 26 50 33
99 66 79 55 38 1 43 15 4 28
77 57 40 64 45 17 8 30 20 11
80 74 47 59 42 48 37 27 49 32
100 65 78 56 39 2 44 16 3 29
76 58 41 75 46 18 9 31 19 10

94 74 69 62 75 23 6 78 22 13
71 83 96 72 82 52 35 25 51 34
98 67 93 54 68 61 80 14 5 79
95 73 70 63 76 24 7 77 21 12
92 84 97 60 81 53 36 26 50 33
99 66 90 55 38 1 43 15 4 28
88 57 40 64 45 17 8 30 20 11
91 85 47 59 42 48 37 27 49 32
100 65 89 56 39 2 44 16 3 29
87 58 41 86 46 18 9 31 19 10

purtroppo qui gli spazi sono più corti dei caratteri quindi non si leggono molro bene...

Visto che non si possono allegare i file .c ecco il codice:
Codice:
#include <stdio.h>
#include <stdlib.h>

#define MAX_X 10
#define MAX_Y 10
#define NUM_POSIZ 8
#define TOT MAX_X * MAX_Y

int posiz[NUM_POSIZ][2] = {{3,0}, {2,2}, {0,3}, {-2,2},
                           {-3,0}, {-2,-2}, {0,-3}, {2,-2}};
int num_posiz[MAX_X][MAX_Y];
int next_x[MAX_X][MAX_Y][NUM_POSIZ];
int next_y[MAX_X][MAX_Y][NUM_POSIZ];

int x_inizio, y_inizio;
int mappa[MAX_X][MAX_Y];
int max = MAX_X * MAX_Y - 10;
unsigned long counter = 0;


void stampa()
{
	int x, y;

	for(y = 0; y < MAX_Y; y++) {
		putchar('\n');

		for(x = 0; x < MAX_X; x++)
			printf("%4d", mappa[y][x]);
	}

	return;
}

/*INIZIALIZZA UNA MATRICE CHE ASSOCIA AD OGNI PUNTO TUTTI I POSSIBILI
SPOSTAMENTI SUCCESSIVI (OCCUPA MEMORIA MA DOVREBBE OTTIMIZZARE UN PO')*/
void init_coord()
{
    int i, tx, ty, x, y;

    for(x = 0; x < MAX_X; x++) {
        for(y = 0; y < MAX_Y; y++) {
            mappa[x][y] = 0; /*INIZIALIZZA LA MAPPA */
            num_posiz[x][y] = 0; /* NUM DI POSIX SUCCESSIVE VALIDE */

            /* CONTROLLA LE POSIZIONI SUCCESSIVE VALIDE */
        	for(i = 0;  i < NUM_POSIZ; i++) {
        		if((tx = x + posiz[i][0]) >= 0 && tx < MAX_X &&
        			(ty = y + posiz[i][1]) >= 0 && ty < MAX_Y) {
                        next_x[x][y][num_posiz[x][y]] = tx;
                        next_y[x][y][num_posiz[x][y]] = ty;
                        num_posiz[x][y]++;
                }
            }
        }
    }


    return;
}

void visita(int x, int y, int n)
{
    int i;
    int tx, ty;
    int temp = num_posiz[x][y];

	mappa[x][y] = n;
    counter++;


	/* SE TROVA LA FINE */
    if(n == TOT) {
        fprintf(stderr, "."); /* PER UTENTE QUANDO SI REDIRIGE OUTPUT */
		putchar('\n');
/*        getchar();*/
		stampa();
	}

    if(n > max) {
        max = n;
        fprintf(stderr, "\nMAX: %d  %ld\n", max, counter);
    }

	/* PUSH INTORNO */
	for(i = 0;  i < temp; i++)
        if(!mappa[tx = next_x[x][y][i]][ty = next_y[x][y][i]])
				visita(tx, ty, n + 1);

/* VECCHIO CICLO
    for(i = 0; i < NUM_POSIZ; i++)
		if(((tx = x + posiz[i][0]) >= 0 && tx < MAX_X &&
			(ty = y + posiz[i][1]) >= 0 && ty < MAX_Y &&
			(!mappa[tx][ty])))
				visita(tx, ty, n + 1);
*/
    mappa[x][y] = 0;
    
    return;
}


int main(void)
{
    init_coord();

	printf("\n\nInserire le coordinate del punto di partenza [x y]: ");
	scanf("%d%d", &x_inizio, &y_inizio);

    counter = 0;

    visita(x_inizio, y_inizio, 1);

	return EXIT_SUCCESS;
}
__________________
AMD K6 450 MHz, Epox MVP3G2 FSB 100 MHz, 320 MB RAM, HDD IBM 7200 60 GB, VooDoo III (è vecchia ma per diablo II va ancora bene )

Ultima modifica di Sephiroth83 : 15-11-2003 alle 15:00.
Sephiroth83 è offline   Rispondi citando il messaggio o parte di esso