Şimdi Ara

labirent

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
2 Misafir - 2 Masaüstü
5 sn
2
Cevap
0
Favori
2.847
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
0 oy
Öne Çıkar
Sayfa: 1
Giriş
Mesaj
  • C++ da Yapmamız gereken bir labirent.Sayı ve sütun değerlerini kullanıcı belirleyecek. random fonk. ile 0 ve 1 lerden oluşacak bir labirent oluşturulacak.ve program 0 ların izlediği yolu ( Girişten çıkışa kadar ) belirleyecek. Bu nasıl olacak??



  • pofff bıkmadı hocalar labirent sormaktan

    C için hazırlanmış örnek kod aşağıda: Labirenti oluşturma, yolu bulma, labirenti ekrana çizdirme fonkisyonları mevcut...
    2. Stack yapısı -ki bu labirent çizme ve çözme için gerekli- ve fonksiyonlarına örnek...

    http://home.att.net/~srschmitt/script_maze_generator.html
    Bu da javascript için labirent çizdirme örneği. Algoritması daha anşlaşılır gibi; yardımcı olabilir...

    Algoritma anlatımı:http://www.mazeworks.com/mazegen/mazetut/
    Algoritma anlatımı:http://hereandabove.com/maze/maze.algorithm.html
    Bir başka algoritma anlatımı(üstteki ikisi ile aynı algoritma):http://www.sulaco.co.za/maze.htm

    Değişik yaklaşımlar:http://www.4ngel.net/article/18.htm,http://www.gamedev.net/reference/articles/article1637.asp#

    A`dan z`ye labirent:http://www.astrolog.org/labyrnth/algrithm.htm


    Kolay gelsin...



    #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>

    #define WIDTH 39
    #define HEIGHT 11

    #define UP 0
    #define RIGHT 1
    #define DOWN 2
    #define LEFT 3
    #ifdef TRUE
    #undef TRUE
    #endif /* TRUE */

    #define TRUE 1

    #define cell_empty(a) (!(a)->up && !(a)->right && !(a)->down && !(a)->left)

    typedef struct {
    unsigned int up : 1;
    unsigned int right : 1;
    unsigned int down : 1;
    unsigned int left : 1;
    unsigned int path : 1;
    unsigned int visited : 1;
    } cell_t;
    typedef cell_t *maze_t;

    void CreateMaze (maze_t maze, int width, int height);
    void SolveMaze (maze_t maze, int width, int height);
    void PrintMaze (maze_t maze, int width, int height);

    int main (int argc, char *argv [])
    {
    int width = WIDTH;
    int height = HEIGHT;
    maze_t maze;

    if (argc >= 2)
    width = atoi (argv [1]);

    if (argc >= 3)
    height = atoi (argv [2]);

    if (argc >= 4)
    srand (atoi (argv [3]));
    else
    srand ((int) time ((time_t *) NULL));

    if (width <= 0 || height <= 0) {
    (void) fprintf (stderr, "Illegal width or height value!\n");
    exit (EXIT_FAILURE);
    }
    maze = (maze_t) calloc (width * height, sizeof (cell_t));
    if (maze == NULL) {
    (void) fprintf (stderr, "Cannot allocate memory!\n");
    exit (EXIT_FAILURE);
    }
    CreateMaze (maze, width, height);

    PrintMaze (maze, width, height);

    (void) putchar ('\n');

    SolveMaze (maze, width, height);

    PrintMaze (maze, width, height);

    free (maze);
    exit (EXIT_SUCCESS);

    return (0);

    }/* main */


    void CreateMaze (maze_t maze, int width, int height)
    {
    maze_t mp, maze_top;
    char paths [4];
    int visits, directions;

    visits = width * height - 1;
    mp = maze;
    maze_top = mp + (width * height) - 1;

    while (visits) {
    directions = 0;

    if ((mp - width) >= maze && cell_empty (mp - width))
    paths [directions++] = UP;
    if (mp < maze_top && ((mp - maze + 1) % width) && cell_empty (mp + 1))
    paths [directions++] = RIGHT;
    if ((mp + width) <= maze_top && cell_empty (mp + width))
    paths [directions++] = DOWN;
    if (mp > maze && ((mp - maze) % width) && cell_empty (mp - 1))
    paths [directions++] = LEFT;

    if (directions) {
    visits--;
    directions = ((unsigned) rand () % directions);

    switch (paths [directions]) {
    case UP:
    mp->up = TRUE;
    (mp -= width)->down = TRUE;
    break;
    case RIGHT:
    mp->right = TRUE;
    (++mp)->left = TRUE;
    break;
    case DOWN:
    mp->down = TRUE;
    (mp += width)->up = TRUE;
    break;
    case LEFT:
    mp->left = TRUE;
    (--mp)->right = TRUE;
    break;
    default:
    break;
    }
    } else {
    do {
    if (++mp > maze_top)
    mp = maze;
    } while (cell_empty (mp));
    }
    }
    }/* CreateMaze */


    void SolveMaze (maze_t maze, int width, int height)
    {
    maze_t *stack, mp = maze;
    int sp = 0;

    stack = (maze_t *) calloc (width * height, sizeof (maze_t));
    if (stack == NULL) {
    (void) fprintf (stderr, "Cannot allocate memory!\n");
    exit (EXIT_FAILURE);
    }
    (stack [sp++] = mp)->visited = TRUE;

    while (mp != (maze + (width * height) - 1)) {

    if (mp->up && !(mp - width)->visited)
    stack [sp++] = mp - width;
    if (mp->right && !(mp + 1)->visited)
    stack [sp++] = mp + 1;
    if (mp->down && !(mp + width)->visited)
    stack [sp++] = mp + width;
    if (mp->left && !(mp - 1)->visited)
    stack [sp++] = mp - 1;

    if (stack [sp - 1] == mp)
    --sp;

    (mp = stack [sp - 1])->visited = TRUE;
    }
    while (sp--)
    if (stack [sp]->visited)
    stack [sp]->path = TRUE;

    free (stack);

    }/* SolveMaze */


    void PrintMaze (maze_t maze, int width, int height)
    {
    int w, h;
    char *line, *lp;

    line = (char *) calloc ((width + 1) * 2, sizeof (char));
    if (line == NULL) {
    (void) fprintf (stderr, "Cannot allocate memory!\n");
    exit (EXIT_FAILURE);
    }
    maze->up = TRUE;
    (maze + (width * height) - 1)->down = TRUE;

    for (lp = line, w = 0; w < width; w++) {
    *lp++ = '+';
    if ((maze + w)->up)
    *lp++ = ((maze + w)->path) ? '.' : ' ';
    else
    *lp++ = '-';
    }
    *lp++ = '+';
    (void) puts (line);
    for (h = 0; h < height; h++) {
    for (lp = line, w = 0; w < width; w++) {
    if ((maze + w)->left)
    *lp++ = ((maze + w)->path && (maze + w - 1)->path) ? '.' : ' ';
    else
    *lp++ = '|';
    *lp++ = ((maze + w)->path) ? '.' : ' ';
    }
    *lp++ = '|';
    (void) puts (line);
    for (lp = line, w = 0; w < width; w++) {
    *lp++ = '+';
    if ((maze + w)->down)
    *lp++ = ((maze + w)->path && (h == height - 1 ||
    (maze + w + width)->path)) ? '.' : ' ';
    else

    *lp++ = '-';
    }
    *lp++ = '+';
    (void) puts (line);
    maze += width;
    }
    free (line);

    }/* PrintMaze */







    #include<stdio.h> 

    typedef struct
    {
    float v[20];
    int top;
    } Stack;

    void push(Stack *S, float val);
    float pop(Stack *S);
    void init(Stack *S);
    int full(Stack *S);
    void MyStackPrint(Stack *S);

    void push(Stack *S, float val)
    {
    S->v[ S->top ] = val;
    (S->top)++;
    /* Equivalent to: S->v[ (S->top)++ ] = val; */
    }

    float pop(Stack *S)
    {
    (S->top)--;
    return (S->v[S->top]);
    /* Equivalent to: return (S->v[--(S->top)]); */
    }

    void init(Stack *S)
    {
    S->top = 0;
    }

    int full(Stack *S)
    {
    return (S->top >= 20);
    }

    void MyStackPrint(Stack *S)
    {
    int i;
    if (S->top == 0)
    printf("Stack is empty.\n");
    else
    {
    printf("Stack contents: ");
    for (i=0;i<S->top;i++)
    {
    printf("%g ",S->v[i]);
    }
    printf("\n");
    }
    }

    void main()
    {
    Stack S;

    init(&S);

    MyStackPrint(&S);

    push(&S, 2.31);
    push(&S, 1.19);

    MyStackPrint(&S);

    printf("Popped value is: %g\n",pop(&S));

    push(&S, 6.7);

    MyStackPrint(&S);

    push(&S, pop(&S) + pop(&S));

    MyStackPrint(&S);
    }




    < Bu mesaj bu kişi tarafından değiştirildi Guest -- 26 Kasım 2006; 3:51:56 >




  • 
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.