厦大小学期C语言程序设计实践(四)

所有实践项目的源程序可到我的Github上下载:https://github.com/liulizhi1996/C-Programming-Practice-in-short-semester


以下实验二选一。

1、使用队列实现迷宫算法,找到最短路径。

main.cpp

#include <iostream>
#include <queue>
#include <map>
#include <stack>

#define MAX_SIZE 30

using namespace std;

class Point
{
public:
    int x;
    int y;

public:
    Point()
    { }

    Point(int a, int b)
    {
        x = a, y = b;
    }

    bool operator <(const Point &pos) const
    {
        if (x < pos.x)
            return true;
        else if (x == pos.x)
        {
            if (y < pos.y)
                return true;
        }
        return false;
    }

    bool operator ==(const Point &pos) const
    {
        return x == pos.x && y == pos.y;
    }

    bool operator !=(const Point &pos) const
    {
        return x != pos.x || y != pos.y;
    }
};

int maze[MAX_SIZE][MAX_SIZE] = { 0 };
queue<Point> open_table;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};

void print_path(map<Point, Point> come_from, int N)
{
    Point pos = Point(N, N);
    stack<Point> path;

    while (pos != Point(1, 1))
    {
        path.push(pos);
        pos = come_from[pos];
    }

    cout << "(1, 1)";
    while (!path.empty())
    {
        pos = path.top();
        cout << "->(" << pos.x << ", " << pos.y << ")";
        path.pop();
    }
    cout << endl;
}

void solve_maze(int N)
{
    map<Point, Point> come_from;

    open_table.push(Point(1, 1));

    while (!open_table.empty())
    {
        Point pos = open_table.front();
        open_table.pop();
        maze[pos.x][pos.y] = -1;

        if (pos.x == N && pos.y == N)
        {
            printf("Path: ");
            print_path(come_from, N);
            return;
        }

        for (int i = 0; i < 4; ++i)
        {
            Point new_pos = Point(pos.x+dx[i], pos.y+dy[i]);
            if (new_pos.x < 1 || new_pos.x > N || new_pos.y < 1 || new_pos.y > N || maze[new_pos.x][new_pos.y] != 0)
                continue;
            open_table.push(new_pos);
            come_from[new_pos] = pos;
        }
    }

    cout << "No path!" << endl;
}

int main()
{
    int N;

    cout << "Please enter the size of maze: ";
    cin >> N;

    cout << "Please enter the maze: " << endl;
    for (int i = 1; i <= N; ++i)
    {
        for (int j = 1; j <= N; ++j)
        {
            cin >> maze[i][j];
        }
    }
    for (int i = 0; i <= N+1; ++i)
    {
        maze[0][i] = maze[N+1][i] = maze[i][0] = maze[i][N+1] = 1;
    }

    solve_maze(N);

    return 0;
}
测试数据:

Test 1:
8
0 0 1 0 0 0 1 0
0 0 1 0 0 0 1 0
0 0 0 0 1 1 0 0
0 1 1 1 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 1 0 0
0 1 1 1 0 1 1 0
1 0 0 0 0 0 0 0

Test 2:
2
1 1
1 1

Test 3:
5
0 1 1 1 1
0 0 0 1 1
1 1 1 1 0
0 0 0 0 1
1 1 0 1 0

Test 4:
3
0 0 1
0 1 1
0 0 1

Test 5:
10
0 0 1 0 0 0 0 1 0 1
1 0 1 0 1 1 0 1 1 0
1 0 0 0 1 1 0 0 0 1
0 1 0 1 1 0 0 1 1 1
0 1 0 0 1 1 0 1 0 0
1 0 1 0 1 1 0 0 1 0
0 1 1 0 0 0 1 0 1 1
0 1 0 1 1 0 0 0 0 0
0 1 1 0 0 1 1 1 1 0
1 0 1 0 1 1 0 0 1 0

Test 6:
8
0 0 1 1 1 0 1 0
1 0 0 1 1 1 0 1
1 1 0 0 0 0 0 1
1 1 0 1 1 1 0 1
1 1 0 1 1 1 0 1
1 1 0 0 0 0 0 1
1 1 1 1 1 1 0 1
1 1 0 0 0 0 0 0

2、实现顺序队列和链队列的所有基本操作,InitQueue(&Q);DestroyQueue(&Q);ClearQueue(&Q);QueueEmpty(Q);QueueLength(Q);GetHead(Q, &e); EnQueue(&Q, e);DeQueue(&Q,&e);QueueTraverse(Q, visit())。

1)顺序队列(循环队列)

queue.h

#ifndef QUEUE_H
#define QUEUE_H

#define MAX_SIZE 5

struct SqQueue
{
    int *data;
    int front;
    int rear;
};

void InitQueue(struct SqQueue *Q);
void DestroyQueue(struct SqQueue *Q);
void ClearQueue(struct SqQueue *Q);
int QueueEmpty(struct SqQueue Q);
int QueueLength(struct SqQueue Q);
int GetHead(struct SqQueue Q, int *e);
int EnQueue(struct SqQueue *Q, int e);
int DeQueue(struct SqQueue *Q, int *e);
void QueueTraverse(struct SqQueue Q);

#endif //QUEUE_H
queue.cpp
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

void InitQueue(struct SqQueue *Q)
{
    Q->data = (int *)malloc(sizeof(int) * MAX_SIZE);
    Q->front = Q->rear = 0;
}

void DestroyQueue(struct SqQueue *Q)
{
    free(Q->data);
    Q->front = Q->rear = 0;
}

void ClearQueue(struct SqQueue *Q)
{
    Q->front = Q->rear = 0;
}

int QueueEmpty(struct SqQueue Q)
{
    if (Q.front == Q.rear)
        return 1;
    else
        return 0;
}

int QueueLength(struct SqQueue Q)
{
    return (Q.rear - Q.front + MAX_SIZE) % MAX_SIZE;
}

int GetHead(struct SqQueue Q, int *e)
{
    if (!QueueEmpty(Q))
    {
        *e = Q.data[Q.front];
        return 1;
    }
    else
        return 0;
}

int EnQueue(struct SqQueue *Q, int e)
{
    if ((Q->rear + 1) % MAX_SIZE == Q->front)
        return 0;
    else
    {
        Q->data[Q->rear] = e;
        Q->rear = (Q->rear + 1) % MAX_SIZE;
        return 1;
    }
}

int DeQueue(struct SqQueue *Q, int *e)
{
    if (QueueEmpty(*Q))
        return 0;
    else
    {
        *e = Q->data[Q->front];
        Q->front = (Q->front + 1) % MAX_SIZE;
        return 1;
    }
}

void QueueTraverse(struct SqQueue Q)
{
    for (int i = Q.front; i != Q.rear; i = (i + 1) % MAX_SIZE)
    {
        printf("%d ", Q.data[i]);
    }
    printf("\n");
}

main.cpp

#include <stdio.h>
#include "queue.h"

int main()
{
    struct SqQueue Q;
    int e;

    InitQueue(&Q);
    printf("QueueEmpty: %d\n", QueueEmpty(Q));

    printf("EnQueue 3.\n");
    if (!EnQueue(&Q, 3))
        printf("    EnQueue 3. Error!\n");
    printf("EnQueue 5.\n");
    if (!EnQueue(&Q, 5))
        printf("    EnQueue 5. Error!\n");
    printf("EnQueue 2.\n");
    if (!EnQueue(&Q, 2))
        printf("    EnQueue 2. Error!\n");

    printf("Length: %d\n", QueueLength(Q));
    printf("QueueEmpty: %d\n", QueueEmpty(Q));

    DeQueue(&Q, &e);
    printf("DeQueue %d.\n", e);
    GetHead(Q, &e);
    printf("GetHead: %d.\n", e);

    printf("EnQueue 9.\n");
    if (!EnQueue(&Q, 9))
        printf("    EnQueue 9. Error!\n");
    printf("EnQueue 4.\n");
    if (!EnQueue(&Q, 4))
        printf("    EnQueue 4. Error!\n");
    printf("EnQueue 7.\n");
    if (!EnQueue(&Q, 7))
        printf("    EnQueue 7. Error!\n");

    printf("QueueTraverse: ");
    QueueTraverse(Q);

    printf("ClearQueue\n");
    ClearQueue(&Q);
    if (!DeQueue(&Q, &e))
        printf("    DeQueue Error.\n");

    printf("EnQueue 11.\n");
    if (!EnQueue(&Q, 11))
        printf("    EnQueue 11. Error!\n");
    printf("EnQueue 14.\n");
    if (!EnQueue(&Q, 14))
        printf("    EnQueue 14. Error!\n");
    printf("EnQueue 17.\n");
    if (!EnQueue(&Q, 17))
        printf("    EnQueue 17. Error!\n");
    printf("QueueTraverse: ");
    QueueTraverse(Q);

    DestroyQueue(&Q);

    return 0;
}
2)链式队列

queue.h

#ifndef QUEUE_H
#define QUEUE_H

struct QueueNode
{
    int data;
    struct QueueNode *next;
};

struct LinkQueue
{
    struct QueueNode *front;
    struct QueueNode *rear;
};

void InitQueue(struct LinkQueue *Q);
void DestroyQueue(struct LinkQueue *Q);
void ClearQueue(struct LinkQueue *Q);
int QueueEmpty(struct LinkQueue Q);
int QueueLength(struct LinkQueue Q);
int GetHead(struct LinkQueue Q, int *e);
void EnQueue(struct LinkQueue *Q, int e);
int DeQueue(struct LinkQueue *Q, int *e);
void QueueTraverse(struct LinkQueue Q);

#endif //QUEUE_H
queue.cpp
#include <stdio.h>
#include <stdlib.h>
#include "queue.h"

void InitQueue(struct LinkQueue *Q)
{
    Q->front = (struct QueueNode *)malloc(sizeof(struct QueueNode));
    Q->front->next = NULL;
    Q->rear = Q->front;
}

void DestroyQueue(struct LinkQueue *Q)
{
    struct QueueNode *p = Q->front, *q;

    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }

    Q->front = Q->rear = NULL;
}

void ClearQueue(struct LinkQueue *Q)
{
    struct QueueNode *p = Q->front->next, *q;

    while (p)
    {
        q = p->next;
        free(p);
        p = q;
    }

    Q->rear = Q->front;
}

int QueueEmpty(struct LinkQueue Q)
{
    if (Q.front == Q.rear)
        return 1;
    else
        return 0;
}

int QueueLength(struct LinkQueue Q)
{
    struct QueueNode *p = Q.front->next;
    int len = 0;

    while (p)
    {
        ++len;
        p = p->next;
    }

    return len;
}

int GetHead(struct LinkQueue Q, int *e)
{
    if (!QueueEmpty(Q))
    {
        *e = Q.front->next->data;
        return 1;
    }
    else
        return 0;
}

void EnQueue(struct LinkQueue *Q, int e)
{
    struct QueueNode *p = (struct QueueNode *)malloc(sizeof(struct QueueNode));
    p->data = e;
    p->next = NULL;
    Q->rear->next = p;
    Q->rear = p;
}

int DeQueue(struct LinkQueue *Q, int *e)
{
    if (!QueueEmpty(*Q))
    {
        struct QueueNode *p = Q->front->next;
        Q->front->next = p->next;
        if (!p->next)
            Q->rear = Q->front;
        *e = p->data;
        free(p);
        return 1;
    }
    else
        return 0;
}

void QueueTraverse(struct LinkQueue Q)
{
    if (!QueueEmpty(Q))
    {
        struct QueueNode *p = Q.front->next;

        while (p)
        {
            printf("%d ", p->data);
            p = p->next;
        }
    }
    printf("\n");
}
main.cpp
#include <stdio.h>
#include "queue.h"

int main()
{
    struct LinkQueue Q;
    int e;

    InitQueue(&Q);
    printf("QueueEmpty: %d\n", QueueEmpty(Q));

    printf("EnQueue 3.\n");
    EnQueue(&Q, 3);
    printf("EnQueue 5.\n");
    EnQueue(&Q, 5);
    printf("EnQueue 2.\n");
    EnQueue(&Q, 2);

    printf("Length: %d\n", QueueLength(Q));
    printf("QueueEmpty: %d\n", QueueEmpty(Q));

    DeQueue(&Q, &e);
    printf("DeQueue %d.\n", e);
    GetHead(Q, &e);
    printf("GetHead: %d.\n", e);

    printf("EnQueue 9.\n");
    EnQueue(&Q, 9);
    printf("EnQueue 4.\n");
    EnQueue(&Q, 4);
    printf("EnQueue 7.\n");
    EnQueue(&Q, 7);

    printf("QueueTraverse: ");
    QueueTraverse(Q);

    printf("ClearQueue\n");
    ClearQueue(&Q);
    if (!DeQueue(&Q, &e))
        printf("    DeQueue Error.\n");

    printf("EnQueue 11.\n");
    EnQueue(&Q, 11);
    printf("EnQueue 14.\n");
    EnQueue(&Q, 14);
    printf("EnQueue 17.\n");
    EnQueue(&Q, 17);
    printf("QueueTraverse: ");
    QueueTraverse(Q);

    DestroyQueue(&Q);

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值