Chinaunix首页 | 论坛 | 博客
  • 博客访问: 554115
  • 博文数量: 92
  • 博客积分: 2511
  • 博客等级: 少校
  • 技术积分: 932
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-19 10:10
文章分类
文章存档

2011年(6)

2010年(27)

2009年(37)

2008年(22)

我的朋友

分类: LINUX

2009-06-08 22:48:55

       帮一个朋友写的小程序,是一个以数组表示的迷宫,1代表通路,0不通,要找到不同的路径。先用STL实现以下,然后写个用栈实现的c语言版本

#include<vector>
#include<iostream>
using namespace std;
#define m 5//行

#define n 4//列


struct xy
{
    int x;
    int y;
};
int main()
{

    int path[m+1][n+1] = {0};
    int col = 0,row = 0;
    int i = 0,j = 0;
    int temp_col = 0,temp_row = 0,t_col = 0,t_row = 0;
    struct xy t_pair;
    vector<struct xy> A,B;

    for(;i<m;i++)
        for(j=0;j<n;j++)
            {
                cout<<"输入0或者1"<<endl;
                cin>>path[i][j];
            }
    i = j = 0;

    if(path[0][0] == 0 || path[m-1][n-1] == 0)
    {
        cout<<"迷宫没有通路\n";
        return 1;
    }
    while(1)
    {
        //检验是否已经到达末尾

        if(col == n-1 && row == m-1 && path[row][col] == 1)
        {
            //到达尾端,意味着找到一条路

            cout<<"Find a way,it's"<<endl;
            for(vector<struct xy>::iterator iter = A.begin();
             iter < A.end();iter++)
                    cout<<"("<<iter->y<<","<<iter->x<<")"<<endl;
            cout<<"("<<m-1<<","<<n-1<<")"<<endl;
            if(B.size() != 0)
            {
                vector<struct xy>::iterator iter = B.end() - 1;
                temp_col = iter->x;
                temp_row = iter->y;
                iter = A.end()-1;
                for(;iter != A.begin();iter--)
                    if(iter->x = temp_col && iter->y == temp_row)
                        break;
                    else
                        A.pop_back();
                col = temp_col + 1;
                row = temp_row;
                B.pop_back();

            }
            else
                return 1;
         }

        else//还没有到末尾的情况

        {
            if(path[row + 1][col] == 1 && path[row][col + 1] == 1)
            {
                t_pair.x = col;t_pair.y = row;
                A.push_back(t_pair);
                B.push_back(t_pair);
                row++;
                continue;
            }
            //下面不是右边也不是

            if(path[row + 1][col] == 0 && path[row][col + 1] == 0)
            {
                if(B.size())
                {
                    vector<struct xy>::iterator iter = B.end() - 1;
                    col = iter->x + 1;row = iter->y;//回到上一次分叉处,搜索右侧路径

                    A.pop_back();
                    B.pop_back();
                    continue;
                }
                else
                    return 1;
            }
            //下面是,右边不是

            if(path[row + 1][col] == 1 && path[row][col + 1] == 0)
            {
                t_pair.x = col;t_pair.y = row;
                A.push_back(t_pair);
                row++;
                continue;
            }
            //下面不是,右边是

            if(path[row + 1][col] == 0 && path[row][col + 1] == 1)
            {
                t_pair.x = col;t_pair.y = row;
                A.push_back(t_pair);
                col++;
                continue;
            }
            

        }
    }

    return 0;
}

c语言版本:

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

#define m 4//行

#define n 4//列


struct xy
{
    int x;
    int y;
};
typedef struct stack
{
    struct xy coordinate;
    struct stack* next;
}stack;

void init(stack* p)
{
    
    p->next = NULL;
}

void push(stack* p,struct xy cdnt)
{
    stack* temp = p;
    while(temp->next != NULL)
        temp = temp->next;
    stack* newValue = (stack*)malloc(sizeof(struct stack)*1);
    newValue->coordinate = cdnt;
    newValue->next = temp->next;
    temp->next = newValue;
}

void pop(stack* p)
{
    stack* tempp = p;
    stack* temp = p->next;
    while(temp->next != NULL)
        temp = temp->next,tempp = tempp->next;
    tempp->next = NULL;
    free(temp);
}

void browse(stack* p)
{
    stack* temp = p->next;
    while(temp != NULL)
        printf("(%d,%d)\n",temp->coordinate.y,temp->coordinate.x),temp = temp->next;
}

struct xy getEnd(struct stack* p)
{
    stack* temp = p;
    while(temp->next != NULL)
        temp = temp->next;
    return temp->coordinate;
}

int getSize(stack* p)
{
    int size = 0;
    stack* temp = p->next;
    while(temp != NULL)
    {
        size++;
        temp = temp->next;
    }
    return size;
}
int main()
{

    int path[m+1][n+1] = {0};
    int col = 0,row = 0;
    int i = 0,j = 0;
    int temp_col = 0,temp_row = 0,t_col = 0,t_row = 0;
    int flag = 0;
    struct xy t_pair;
    //stack A,B;

    stack* Ahead = (stack*)malloc(sizeof(struct stack)*1);
    stack* Bhead = (stack*)malloc(sizeof(struct stack)*1);
    init(Ahead); init(Bhead);

    for(;i<m;i++)
        for(j=0;j<n;j++)
            {
                printf("input 0 or 1\n");
                scanf("%d",&path[i][j]);
            }
    i = j = 0;

    if(path[0][0] == 0 || path[m-1][n-1] == 0)
    {
        printf("There is no way\n");
        return 1;
    }
    while(1)
    {
        //检验是否已经到达末尾

        if(col == n-1 && row == m-1 && path[row][col] == 1)
        {
            //到达尾端,意味着找到一条路

            flag = 1;
            printf("Find a way,it's\n");
            browse(Ahead);
            printf("(%d,%d)\n",m-1,n-1);
            if(getSize(Bhead) != 0)
            {
                
                temp_col = getEnd(Bhead).x;
                temp_row = getEnd(Bhead).y;

                while(1)
                    if(getEnd(Ahead).x == temp_col && getEnd(Ahead).y == temp_row)
                        break;
                    else
                        pop(Ahead);
                col = temp_col + 1;
                row = temp_row;
                pop(Bhead);

            }
            else
                return 1;
         }

        else//还没有到末尾的情况

        {
            if(path[row + 1][col] == 1 && path[row][col + 1] == 1)
            {
                t_pair.x = col;t_pair.y = row;
                push(Ahead,t_pair);
                push(Bhead,t_pair);
                row++;
                continue;
            }
            //下面不是右边也不是

            if(path[row + 1][col] == 0 && path[row][col + 1] == 0)
            {
                if(getSize(Bhead))
                {
                    //vector::iterator iter = B.end() - 1;

                    col = getEnd(Bhead).x + 1;row = getEnd(Bhead).y;//回到上一次分叉处,搜索右侧路径

                    pop(Ahead);
                    pop(Bhead);
                    continue;
                }
                else
                    return 1;
            }
            //下面是,右边不是

            if(path[row + 1][col] == 1 && path[row][col + 1] == 0)
            {
                t_pair.x = col;t_pair.y = row;
                push(Ahead,t_pair);
                row++;
                continue;
            }
            //下面不是,右边是

            if(path[row + 1][col] == 0 && path[row][col + 1] == 1)
            {
                t_pair.x = col;t_pair.y = row;
                push(Ahead,t_pair);
                col++;
                continue;
            }
            

        }
    }
    if(!flag)
        printf("There is no way\n");
    return 0;
}

阅读(938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~