Программа поиска кратчайшего пути в лабиринте

Автор работы: Пользователь скрыл имя, 12 Января 2013 в 19:31, курсовая работа

Краткое описание

Полное название разработки ”Поиск кратчайшего пути”. Данная разработка предназначена для использования в учебных заведениях. Она выполняет нахождение кратчайшего пути между входом в лабиринт и его выходом. Также возможно использование для самопроверки решения, принятого человеком.

Содержание

1 Техническое задание ……………………………………………………….8
1.1 Введение ……………………………………………………………..8
1.2 Основания для разработки ………………………………………….8
1.3 Функциональное и эксплуатационное назначение изделия ……...8
1.4 Методические ограничения ………………………………………...9
1.4.1 Стандарты …………………………………………………...9
1.4.2 Программная совместимость ……………………………... 9
1.4.3 Требования к составу и параметрам технических
средств …………………………………………………………….10
1.4.4 Входные данные ……………………………………………10
1.4.5 Выходные данные …………………………………………..10
1.4.6 Безопасность и секретность ………………………………..10
1.4.7 Мобильность ………………………………………………..10
1.5 Стадии и этапы разработки ………………………………………..11
1.6 Технико-экономические показатели разработки ………………...11
2 Пояснительная записка ………………………………………………………12
2.1 Функциональные и эксплуатационные характеристики ………...12
2.2 Удобство эксплуатации …………………………………………….12
2.3 Описание программы ……………………………………………….12
2.3.1 Функциональное описание ………………………………….13
2.3.2 Форма программы в проекте ……………………………14-15
2.3.3 Описание среды программирования для реализации
практических частей курсовой работы …………………….16
2.3.4 Интерфейс программного продукта ……………………17-20
Приложение: текст программы с комментариями, блок-схемы основных алгоритмов, список литературы

Вложенные файлы: 1 файл

курсач.docx

— 305.03 Кб (Скачать файл)

                                                        i_prev = i_2;

                                                        j_prev = j_2;

                                                }

                                                if(Pole[i_2][j_2].v == j+1){

                                                        i_next = i_2;

                                                        j_next = j_2;

                                                }

                                        }

                                }

                                image->Canvas->Pen->Color = clRed;

                                if(i_next > i_prev){

 

                                        image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+40,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+30,j_prev*40+15);

                                        image->Canvas->MoveTo(i_prev*40+40,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+30,j_prev*40+25);

                                }

                                if(i_next < i_prev){

 

                                        image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+5,j_prev*40+15);

                                        image->Canvas->MoveTo(i_prev*40,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+5,j_prev*40+25);

                                }

                                if(j_next > j_prev){

 

                                        image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+20,j_prev*40+40);

                                        image->Canvas->LineTo(i_prev*40+15,j_prev*40+35);

                                        image->Canvas->MoveTo(i_prev*40+20,j_prev*40+40);

                                        image->Canvas->LineTo(i_prev*40+25,j_prev*40+35);

                                }

                                if(j_next < j_prev){

                                       

                                        image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                        image->Canvas->LineTo(i_prev*40+20,j_prev*40);

                                        image->Canvas->LineTo(i_prev*40+15,j_prev*40+5);

                                        image->Canvas->MoveTo(i_prev*40+20,j_prev*40);

                                        image->Canvas->LineTo(i_prev*40+25,j_prev*40+5);

                                       

                                }

 

                        }

                }

        }

}

//===============================================

clearHelpMas(){

        for(int i=0; i<100; i++){

                HelpMas[i] = -1;

        }

}

 

//================================================

addHelpMas(int n){

        for(int i=0; i<100; i++){

                if(HelpMas[i] == -1){

                        HelpMas[i] = n;

                        break;

                }

        }

}

//================================================

int Rec(int v, int vn){

        int k=0;

        int Flag = -1;

        int Flag2 = -1;

        int Flag3 = -1;

        int pp = 0;

        for(int i=0; i<100; i++){

                if(mas[v][i] == 1){

                        Flag3 = 1;

                }

        }

 

        if(Flag3 == -1){

                //ShowMessage("Flag3");

                for(int i=0;i<100; i++){

                        if(Deic[i].visible == true){

                                Flag3 = 1;

                                v = i;

                        }

                }

                if(Flag3 <0){

                        ShowMessage("Net puti");

                        return 0;

                }

        }

 

        for(int i=0; i<100; i++){

                if(Deic[i].sum != 9999 && Deic[i].visible == true){

                        Flag = 1;

                }

        }

        if(Flag != 1){

                ShowMessage("Net puti");

                return 0;

        }

        Deic[v].visible = false;

        for(int i=0; i<100; i++){

                if(mas[v][i]==1 && Deic[i].visible == true){

                        Flag2 =1;

                        if(Deic[i].sum > Deic[v].sum + mas[v][i]){

                                Deic[i].sum = Deic[v].sum + mas[v][i];

                                Deic[i].prev = v;

                                k = i;

                        }

                }

        }

 

        /*if(Flag2 <0){

                ShowMessage("Нет пути");

                return 0;

        } */

 

        if(Deic[k].visible == false){

                for(int i=0; i<100; i++){

                        if(Deic[i].visible ==true){

                                pp = i;

                                break;

                        }

                }

                k = pp;

        }

 

        int Min;

 

        Min = Deic[k].sum;

        for(int i=0; i<100; i++){

                if(Deic[i].visible == true &&

                Deic[i].sum < Min){

                        k = i;

                }

        }

 

        if(k != vn){

                Rec(k,vn);

        }else{

        //ShowMessage("OK"+IntToStr(Deic[vn].prev));

        }

}

//================================================

String Deicstr(int V1, int Vn, TImage* image){

        Deic[V1].sum = 0;

        Deic[V1].value =0;

        Deic[V1].visible = true;

        if(Rec(V1, Vn)!=0){

        String st;

        clearHelpMas();

        //st = IntToStr(Vn+1);

        addHelpMas(Vn+1);

        while(Vn !=V1){

                //st = IntToStr(Deic[Vn].prev+1)+" "+st;

                addHelpMas(Deic[Vn].prev+1);

                if(Deic[Vn].prev == V1){

                        Vn = 0;

                        break;

                }

                Vn = Deic[Vn].prev;

        }

 

 

        for(int k=0; k<100; k++){

                for(int i=0; i<100; i++){

                        for(int j=0; j<100; j++){

                                if(Pole[i][j].v == HelpMas[k]){

                                        Pole[i][j].value = 9;

                                }

                        }

                }

        }

        for(int i=0; i<100; i++){

                for(int j=0; j<100; j++){

                        if(Pole[i][j].value == 9){

                                image->Canvas->Pen->Width = 2;

                                image->Canvas->Pen->Color = clYellow;

                                image->Canvas->Brush->Color = clGreen;

                                image->Canvas->Rectangle((i+1)*40-40,(j+1)*40-40,(i+1)*40,(j+1)*40);

                        }

                }

        }

        return st;

        }

 

}

//================================================

bool CreateLine(int x, int y, TImage *image){

        int i;

        int j;

        i = x/40;

        j = y/40;

        image->Canvas->Pen->Width = 4;

        image->Canvas->Pen->Color = clRed;

        if(N_1 < 0){

                if(Pole[i][j].value == 1){

                        N_1 = Pole[i][j].v;

                        i_prev = i;

                        j_prev = j;                }

                if(Pole[i][j].value == 0){

                        return false;

                }

        }else{

                if(i_prev == i && j_prev ==j){

                        return false;

                }

                if(i_prev != i && j_prev != j){

                        return false;

                }

                if(i>i_prev){

                        if(i-i_prev ==1){

                                image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+40,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+30,j_prev*40+15);

                                image->Canvas->MoveTo(i_prev*40+40,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+30,j_prev*40+25);

                        }else{

                                ShowMessage("Error");

                        }

                }

 

                if(i<i_prev){

                        if(i_prev-i == 1){

                                image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+5,j_prev*40+15);

                                image->Canvas->MoveTo(i_prev*40,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+5,j_prev*40+25);

                        }else{

                                ShowMessage("Error");

                        }

                }

                if(j<j_prev){

                        if(j_prev-j == 1){

                                image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+20,j_prev*40);

                                image->Canvas->LineTo(i_prev*40+15,j_prev*40+5);

                                image->Canvas->MoveTo(i_prev*40+20,j_prev*40);

                                image->Canvas->LineTo(i_prev*40+25,j_prev*40+5);

                        }else{

                                ShowMessage("Error");

                        }

                }

                if(j>j_prev){

                        if(j-j_prev == 1){

                                image->Canvas->MoveTo(i_prev*40+20,j_prev*40+20);

                                image->Canvas->LineTo(i_prev*40+20,j_prev*40+40);

                                image->Canvas->LineTo(i_prev*40+15,j_prev*40+35);

                                image->Canvas->MoveTo(i_prev*40+20,j_prev*40+40);

                                image->Canvas->LineTo(i_prev*40+25,j_prev*40+35);

                        }else{

                                ShowMessage("Error");

                        }

                }

                        i_prev = -1;

                        j_prev = -1;

                        N2 = Pole[i][j].v;

                        mas[N_1-1][N2-1] = 1;

                        N_1 = -1;

 

        }

        return true;

}

//================================================

bool CreateRoom(int x, int y){

        int i;

        int j;

        i = x/40;

        j = y/40;

        if(Pole[i][j].value != 0){

                return false;

        }

        vN = vN+1;

        Pole[i][j].value = 1;

        Pole[i][j].v = vN;

        Pole[i][j].x = i;

        Pole[i][j].y = j;

        if(i+1<10){

                Pole[i+1][j].value = 5;

        }

        if(i-1>-1){

                Pole[i-1][j].value = 5;

        }

        if(j+1<10){

                Pole[i][j+1].value = 5;

        }

        if(j-1>-1){

                Pole[i][j-1].value = 5;

        }

        if(i+1<10 && j+1<10){

                Pole[i+1][j+1].value = 5;

        }

        if(i+1<10 && j-1>-1){

                Pole[i+1][j-1].value = 5;

        }

        if(i-1>-1 && j-1>-1){

                Pole[i-1][j-1].value = 5;

        }

        if(i-1>-1 && j+1<10){

                Pole[i-1][j+1].value = 5;

        }

        return true;

}

//================================================

void MakeNullPole(){

        int k=0;

        for(int i=0; i<razmPolya; i++){

                for(int j=0; j<razmPolya; j++){

                        k++;

                        Pole[i][j].value = 1; //esli 0 to chisto

                        Pole[i][j].v = k;

                }

        }

}

 

//================================================

void ShowPole(TImage *image){

        image->Canvas->Pen->Width = 1;

        image->Canvas->Pen->Color = clBlack;

        for(int i=0; i<razmPolya; i++){

                for(int j=0; j<razmPolya; j++){

                        if(Pole[i][j].value == 0){

                                image->Canvas->Brush->Color = clGreen;

                                image->Canvas->Rectangle((i+1)*40-40,(j+1)*40-40,(i+1)*40,(j+1)*40);

                        }

 

                        if(Pole[i][j].value == 1){

                                image->Canvas->Brush->Color = clWhite;

                                image->Canvas->Rectangle((i+1)*40-40,(j+1)*40-40,(i+1)*40,(j+1)*40);

                        }

 

                        if(Pole[i][j].value == 5){

                                image->Canvas->Brush->Color = clRed;

                                image->Canvas->Rectangle((i+1)*40-40,(j+1)*40-40,(i+1)*40,(j+1)*40);

                        }

                       

                }

        }

}

 

//===============================================

/*chertim pole razmerom 10 * 10 kletok*/

void CreatePole(TImage *image){

        for(int i=0; i<=400; i+=40){

                image->Canvas->MoveTo(i,0);

                image->Canvas->LineTo(i,400);

        }

 

        for(int i=0; i<=400; i+=40){

                image->Canvas->MoveTo(0,i);

                image->Canvas->LineTo(400,i);

        }

        /*int k=0;

        for(int i=0; i<25; i++){

                for(int j=0; j<25; j++){

                        k++;

                        Pole[i][j].v = k;

                        Pole[i][j].value = 1;

                }

        } */

}

//===============================================

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

        : TForm(Owner)

Информация о работе Программа поиска кратчайшего пути в лабиринте