zoukankan      html  css  js  c++  java
  • 迷官找出口

    要求:
    利用栈实现
    说明:0为墙,1为路
    举例:
    4X4的一个迷官
    1 0 0 0
    0 1 1 0
    0 0 0 1
    1 1 1 1
    入口(1,1) 出口(4,1)
    输出路径:
    (1,1)->(2,2)->(2,3)->(3,4)->(4,4)->(4,3)->(4,2)->(4,1)
    否则输出未找到此路。
    坐标系
    坐示系
    main.h

    #ifndef main_h__
    #define main_h__
    
    #include <stdio.h>
    #include "stack.h"
    typedef struct move_def{
        int y;
        int x;
    }Move;
    void printout(stack s);
    void process();
    void initMap(int m, int n);
    #endif // main_h__
    

    main.c

    #include "main.h"
    int map[256][256];
    
    int main(){
        process();
        return 0;
    }
    
    void printout(stack s){
        stack s_t;
        ElementType t;
        if (isEmpty(s)){
            puts("未找到相应路径.");
            return;
        }
        initStack(&s_t, s.sz);
        while (!isEmpty(s)){
            t = getTop(s);
            Push(&s_t, t);
            Pop(&s);
        }
    
        while (!isEmpty(s_t)){
            t = getTop(s_t);
            printf("(%d,%d)", t.x, t.y);
            Pop(&s_t);
            if (!isEmpty(s_t))
                printf("->");
        }
        printf("
    ");
        Destory(&s_t);
    }
    
    void initMap(int m, int n){
        // m 高度 n 宽度
        int i, j;
        for (i=0; i<m ;i++)
            for (j=0; j<n; j++){
                // 边缘置0
                if (j==0 || j == n-1 || i==0 || i== m-1){
                    map[i][j] = 0;
                    continue;
                }
                scanf("%d", &map[i][j]);
            }
    }
    
    void process(){
        Move move[8] = {
            {0,-1}, // 上
            {1,-1}, // 右上
            {1 ,0}, // 右
            {1,1},  // 右下
            {0 ,1}, // 下
            {-1,1}, // 左下
            {-1,0}, // 左
            {-1,-1} // 上
        };
        int i, j;               // 下次欲寻找的位置
        int start_x, start_y;  // 出口的位置
        int end_x, end_y;      // 结束的位置
        int flag = 0;          //找到出口  1 未找到出口 0
        int m, n;              // 高度 m 宽度 n
        stack s;
        ElementType t;
        // 迷官数据初始化
        printf("输入迷官高度m与宽度n:");
        scanf("%d%d", &m, &n);
        if (m+2 >256 || m < 0 || n+2>256 || n < 0){
            printf("对不起,您输入的数据有误!大小均在256之内!");
            exit(1);
        }
        // 左右 上下各加一行
        m += 2;
        n += 2;
        initMap(m, n);
    
        printf("请输入起点坐标x,y:");
        scanf("%d%d", &t.x, &t.y);
        t.d = 0;
        if (t.x<0 || t.x>n || t.y<0 || t.y> m){
            printf("对不起,您输入的数据有误!");
            exit(1);
        }
    
        printf("请输入出口位置x, y:");
        scanf("%d%d", &end_x, &end_y);
        if (end_x<0 || end_x>n || end_y<0 || end_y> m){
            printf("对不起,您输入的数据有误!");
            exit(1);
        }
        start_x = t.x; start_y = t.y;
        initStack(&s, m*n);
        Push(&s, t);
        while (!flag){
            t = getTop(s);
            map[t.x][t.y] = -1;
            while (t.d < 8){
                i = t.x + move[t.d].x;
                j = t.y + move[t.d].y;
    
                // 找到出口
                if (i == end_x && j == end_y){
                    t.x = i; t.y = j; t.d = 0;
                    Push(&s, t);
                    flag = 1; break;
                }
    
                // 未找到出口
                if (map[i][j] == 1){
                    t.x = i; t.y = j;t.d=0;
                    Push(&s,t);
                    break;
                }
                t.d++;
            }
            if (t.d == 8) {
                if (t.x == start_x && t.y == start_y)
                    flag = 1;
                Pop(&s);
            }
        }
        printout(s);
        Destory(&s);
    }

    stack.h

    #ifndef stack_h__
    #define stack_h__
    
    #include <stdio.h>
    #include <malloc.h>
    #include <stdlib.h>
    
    typedef enum boolean {FALSE, TRUE} BOOL;
    
    typedef struct ElementType_def{
        int x;
        int y;
        int d;
    }ElementType;
    
    typedef struct stack_def{
        ElementType* data;
        int top;
        int sz;
    }stack;
    void initStack(stack* s, int sz);
    BOOL isEmpty(stack s);
    BOOL isFull(stack s);
    void Pop(stack *s);
    void Push(stack* s, ElementType e);
    ElementType getTop(stack s);
    void Destory(stack* s);
    #endif // stack_h__
    

    stack.c

    #include "stack.h"
    
    void initStack(stack* s, int sz){
        if (sz < 0){
            puts("error:sz < 0");
            exit(1);
        }
        s->sz = sz;
        s->data = (ElementType*)malloc(sizeof(ElementType)*s->sz);
        s->top = -1;
    }
    
    BOOL isEmpty(stack s){
        return (BOOL)(s.top==-1);
    }
    
    BOOL isFull(stack s){
        return (BOOL)(s.top == s.sz);
    }
    
    
    void Pop(stack *s){
        if (isEmpty(*s)){
            puts("error: Pop stack is empty.");
            exit(1);
        }
        s->top--;
    }
    
    void Push(stack* s, ElementType e){
        if (isFull(*s)){
            puts("error: Push stack is Full.");
            exit(1);
        }
        s->data[++s->top] = e;
    }
    
    ElementType getTop(stack s){
        if (isEmpty(s)){
            puts("error: getTop stack is  empty.");
            exit(1);
        }
        return s.data[s.top];
    }
    
    void Destory(stack* s){
        free(s->data);
    }

    运行图1
    运行图2

  • 相关阅读:
    JS---数组(Array)处理函数整理
    xStream完美转换XML、JSON
    DD_belatedPNG解决IE6下PNG不透明问题
    转 DataTorrent 1.0每秒处理超过10亿个实时事件
    转 Apache Kafka:下一代分布式消息系统
    Selenium(ThoughtWorks公司开发的web自动化测试工具)
    oracle 拆分字符串并转换为表
    C# winform 使用DsoFramer 创建 显示office 文档
    visual studio 2014 新特性
    转 管线开发
  • 原文地址:https://www.cnblogs.com/laohaozi/p/8266615.html
Copyright © 2011-2022 走看看