Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   C++ Builder (https://www.clubdelphi.com/foros/forumdisplay.php?f=13)
-   -   Bloques De Numeros Consecutivos (https://www.clubdelphi.com/foros/showthread.php?t=37473)

dany2006 14-11-2006 16:36:37

Bloques De Numeros Consecutivos
 
Necesito Un Algoritmo Para Encontrar Todos Los Elementos Consecutivos De Una Matriz Que Tengan El Mismo Numero. Ej
1 2 5 6 7 8
4 2 2 2 3 1
3 2 2 1 1 3
5 4 3 2 1 3

Al Seleccionar (1,0) Tendria Que Encontrar
(1,1)(1,2)(2,1)(2,2)(3,2)
Gracias.

seoane 14-11-2006 16:41:04

¿Estas intentando hacer el buscaminas? :D

De todas formas te digo que este es un problema clásico de recursividad. Intenta crear una función recursiva, y si no puedes, nos cuentas donde te quedaste atascado.

dany2006 15-11-2006 16:32:13

algoritmo
 
jaja! no. En realidad estoy haciendo el SameGame (http://www.oopixel.com/samegame/) Lo que estoy haciendo es comparando el elemento seleccionado con los vecinos y guardando las coincidencias en un vector, pero no logro que termine el algoritmo (podria encontrar siempre nuevos elementos coincidentes). si me pueden dar una idea de como resolverlo les agradezco. quiza me estoy complicando y podria ser mas simple. gracias.

dany2006 15-11-2006 20:38:53

Sale
 
Ok. Me Salio. Las Cosas Que Uno Puede Lograr Cuando Se Pone A Pensar Un Poco No?

roman 15-11-2006 20:41:53

y, ¿no nos merecemos conocer la respuesta? :rolleyes:

// Saludos

dany2006 15-11-2006 21:06:16

Respuesta
 
CLARO Q SI. CUANDO TERMINE EL JUEGUITO LO MANDO COMPLETO PARA Q SE ENTIENDA MEJOR.
Código Delphi [-]
void TForm1::calculo_grupo(){
    if (vecinos_examinados==0) {
        grupo->Cols[0]->Clear();
        grupo->Cols[1]->Clear();
        grupo->Cells[0][0]=IntToStr(X);
        grupo->Cells[1][0]=IntToStr(Y);
    }
    if (X+1<=10)
    if (matriz_color->Cells[X][Y]==matriz_color->Cells[X+1][Y]) {
        bool repetido=false;
        for (int i=0; i <= 20; i++) {
            if ( (grupo->Cells[0][i]==IntToStr(X+1)) &&
                 (grupo->Cells[1][i]==IntToStr(Y)) ){
                        repetido=true;
            }
        }
        if (repetido==false) {
            cant_vecinos++;
            grupo->Cells[0][cant_vecinos]=IntToStr(X+1);
            grupo->Cells[1][cant_vecinos]=IntToStr(Y);
        }
    }
    if (X-1>=0)
    if (matriz_color->Cells[X][Y]==matriz_color->Cells[X-1][Y]) {
        bool repetido=false;
        for (int i=0; i <= 20; i++) {
            if ( (grupo->Cells[0][i]==IntToStr(X-1)) &&
                 (grupo->Cells[1][i]==IntToStr(Y)) ){
                        repetido=true;
            }
        }
        if (repetido==false) {
            cant_vecinos++;
            grupo->Cells[0][cant_vecinos]=IntToStr(X-1);
            grupo->Cells[1][cant_vecinos]=IntToStr(Y);
        }
    }
    if (Y+1<=10)
    if (matriz_color->Cells[X][Y]==matriz_color->Cells[X][Y+1]) {
        bool repetido=false;
        for (int i=0; i <= 20; i++) {
            if ( (grupo->Cells[0][i]==IntToStr(X)) &&
                 (grupo->Cells[1][i]==IntToStr(Y+1)) ){
                        repetido=true;
            }
        }
        if (repetido==false) {
            cant_vecinos++;
            grupo->Cells[0][cant_vecinos]=IntToStr(X);
            grupo->Cells[1][cant_vecinos]=IntToStr(Y+1);
        }
    }
    if (Y-1>=0)
    if (matriz_color->Cells[X][Y]==matriz_color->Cells[X][Y-1]) {
        bool repetido=false;
        for (int i=0; i <= 20; i++) {
            if ( (grupo->Cells[0][i]==IntToStr(X)) &&
                 (grupo->Cells[1][i]==IntToStr(Y-1)) ){
                        repetido=true;
            }
        }
        if (repetido==false) {
            cant_vecinos++;
            grupo->Cells[0][cant_vecinos]=IntToStr(X);
            grupo->Cells[1][cant_vecinos]=IntToStr(Y-1);
        }
    }
    if (cant_vecinos>0 && cant_vecinos>vecinos_examinados) {
        vecinos_examinados++;
        X=StrToInt(grupo->Cells[0][vecinos_examinados]);
        Y=StrToInt(grupo->Cells[1][vecinos_examinados]);
        calculo_grupo();
    }
}


La franja horaria es GMT +2. Ahora son las 07:02:03.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi