数据结构上机实验3——图——外卖成本最优问题

在这里插入图片描述

graph.h

#ifndef GRAPH_H
#include <stdio.h>
#include <stdlib.h>
#define MaxSize 500
#define MaxPoint 10000
enum graph_val { hasmoved = -1, can = 0, block = 1, shop = 2, user = 3 };
int vis[MaxSize][MaxSize];//记录已走过
typedef struct {
	int node[MaxSize][MaxSize];//路图
	int milk_count;//奶茶店个数
	int user_count;//客户个数
	int block_count;//不可走个数
	int edge_count;//边长度(不含边框)
}Graph;
#endif // !GRAPH_H

queue.h

#ifndef QUEUE_H
#include "graph.h"
typedef struct
{
    int x, y, w;
}Point;
typedef struct
{
    Point data[MaxPoint];
    int front, rear;
    int count;//队列长度
}QuType;
void InitQueue(QuType*& q)
{
    q = (QuType*)malloc(sizeof(QuType));
    q->front = q->rear = 0;
    q->count = 0;
}
void DestroyQueue(QuType*& q)
{
    free(q);
}
bool QueueEmpty(QuType* q)
{
    if (q->count == 0) {
        return false;
    }
    else
        return true;
}
bool enQueue(QuType*& q, Point e)
{
    if (q->rear == MaxPoint - 1)
    {
        return false;
    }
    q->data[q->rear] = e;
    q->rear++;
    q->count++;
    return true;
}
Point deQueue(QuType*& q)
{
    Point tmp;
    tmp.x = -1;
    tmp.y = -1;
    tmp.w = -1;
    if (q->front == q->rear)
    {
        return tmp;
    }
    tmp = q->data[q->front];
    q->front++;
    q->count--;
    return tmp;
}
Point getTop(QuType* q) {
    return q->data[q->front];
}
#endif // !QUEUE_H

main.cpp

#include "queue.h"
QuType* qu;
int n, m, k, d;
int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
int bfs(Graph* g) {
    int sum = 0;
    while (QueueEmpty(qu)) {
        Point p = getTop(qu);
        deQueue(qu);
        int i;
        Point cur;
        for (i = 0; i < 4; i++) {
            int xx = p.x + dx[i];
            int yy = p.y + dy[i];
            if (xx >= 1 && xx <= g->edge_count && yy >= 1 && yy <= g->edge_count && !vis[xx][yy]) {
                sum += g->node[xx][yy] * (p.w + 1);
                vis[xx][yy] = 1;
                cur.x = xx;
                cur.y = yy;
                cur.w = p.w + 1;
                enQueue(qu, cur);
            }
        }
    }
    return sum;
}
int main() {
    Graph* g = (Graph*)malloc(sizeof(Graph));
	InitQueue(qu);
	printf("请分别输入方格图大小、奶茶分店数量、客户数量、不能经过的点的数量:");
	scanf("%d %d %d %d", &n, &m, &k, &d);
    g->edge_count = n;
    g->milk_count = m;
    g->user_count = k;
    g->block_count = d;
	//初始化内容
	for (int i = 1; i < n + 1; i++) {
		for (int j = 1; j < n + 1; j++) {
			g->node[i][j] = 0;
			vis[i][j] = 0;
		}
	}
	//初始化奶茶店
	for (int i = 0; i < g->milk_count; i++) {
		int x, y;
		printf("请输入第%d个奶茶店的x、y坐标:", i + 1);
		scanf("%d %d", &x, &y);
		Point shop;
		shop.x = x;
		shop.y = y;
		shop.w = 0;
		enQueue(qu, shop);
	}
	//初始化客户
	for (int i = 0; i < g->user_count; i++) {
		int x, y, count;
		printf("请输入第%d个客户的x、y坐标和订单数:", i + 1);
		scanf("%d %d %d", &x, &y, &count);
		g->node[x][y] += count;
	}
	//初始化不可走
	for (int i = 0; i < g->block_count; i++) {
		int x, y;
		printf("请输入第%d个不可走的x、y坐标:", i + 1);
		scanf("%d %d", &x, &y);
		vis[x][y] = 1;
	}
	printf("\n最低总成本为:%d\n", bfs(g));
	DestroyQueue(qu);
	return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值