课程设计(迷宫)C语言

本次的课程设计为数据结构课程设计,有3个题目可选,分别是:

1.Huffman编译码器

2.迷宫问题

3.校园导游图

作为一个菜鸟当然是好吃的先吃啊,选简单的啊,于是就选了Huffman编译码器和迷宫问题

为了方便多条路径寻找最短路径利用了c++stl中的队列进行操作

迷宫问题:

main.cpp

#include"bfs.h"
#include"iostream"
#include"stdio.h"
#include"in.h"
#include"out.h"
using namespace std;
int a[100][100],book[100][100],n,m;
extern void in();
int main()
{
    cout<<"please input the rows and the columns :";
    cin>>m>>n;
    in();
    bfs();
    print(m-1, n-1);
    out();
    return 0;
}

in.cpp

#include"in.h"
#include"stdio.h"
#include"iostream"
#include"string.h"
using namespace std;
char name[100];
extern int n,m;
extern int a[100][100];
void in()
{
    int i,j;
    cout<<"the name of the maze :";
    scanf("%s",name);
    FILE *INFILE = fopen(name,"w");
    cout<<"please input the maze:"<<endl;
    for(i = 0; i < m; i++)
    {
        for(j = 0; j < n; j++)
        {

            scanf("%1d",&a[i][j]);
            fprintf(INFILE,"%2d",a[i][j]);
        }
        fprintf(INFILE,"\n");
    }

}

bfs.cpp

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
struct node
{
    int x,y;
} vis[100][100];
int oper[4][2] =
{
    {0,1},
    {1,0},
    {0,-1},
    {-1,0}
};
extern int book[100][100],a[100][100];
extern int m,n;

void bfs()
{
    queue<node> q;
    node now;
    now.x = now.y = 0;
    book[0][0] = 1;
    q.push(now);
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        for(int i = 0; i < 4; i++)
        {
            node next;
            next.x = now.x + oper[i][0];
            next.y = now.y + oper[i][1];
            if(next.x>=0 && next.x<m && next.y>=0 && next.y<n && !book[next.x][next.y] && a[next.x][next.y]!=1)
            {
                book[next.x][next.y] = 1;
                vis[next.x][next.y] = now;
                q.push(next);
            }
            if(next.x == m-1 && next.y == n-1)
                return;
        }
    }
}

out.cpp

#include"out.h"
#include"stdio.h"
#include"windows.h"
extern int a[100][100];
extern struct node
{
    int x,y;
} vis[100][100];
FILE *pathinfile;
extern int m,n;
char maze[100][100];
void print(int x, int y)
{
    if(x == 0 && y == 0)
        printf("(0, 0)->");
    else
    {
        print(vis[x][y].x, vis[x][y].y);
        maze[x][y] = '*';
        if(x==m-1&&y==n-1)
        {
            pathinfile = fopen("path//theshortestpath.txt","at");
            fprintf(pathinfile,"(%d, %d)\n",x,y);
        }
        else
        {
            pathinfile = fopen("path//theshortestpath.txt","at");
            fprintf(pathinfile,"(%d, %d)->",x,y);
        }
    }
}

void out()
{
    maze[0][0] = '*';
    system("cls");
    system("color e7");
    for(int i = 0; i < m; i++)
    {
        for(int j = 0; j < n; j++)
        {
            if(maze[i][j]=='*'){
                printf("\033[47;34m%c\033[0m",'*');
            }
            else if(a[i][j]==0)
            {
                printf("\033[47;34m%c\033[0m",' ');
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }
}

bfs.h

扫描二维码关注公众号,回复: 9890161 查看本文章
#ifndef BFS_H_INCLUDED
#define BFS_H_INCLUDED
#include"in.h"
void bfs();
void print(int x,int y);
#endif // BFS_H_INCLUDED

in.h 

#ifndef IN_H_INCLUDED
#define IN_H_INCLUDED
#include"bfs.h"
void in();

#endif // IN_H_INCLUDED

out.h

#ifndef OUT_H_INCLUDED
#define OUT_H_INCLUDED
void init();
void out();

#endif // OUT_H_INCLUDED
发布了55 篇原创文章 · 获赞 9 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_42612338/article/details/104079748