最小生成

5 5
0 0 0 0 0
0 0 1 0 0
0 1 2 1 0
0 0 1 0 0
0 0 0 0 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

#include <string>
#include <stack>
#include <map>

using namespace std;

#define CHCH 1

typedef struct tagPos {
    int x;
    int y;
} Pos;

typedef struct tagPoint {
    Pos pos;
    int step;
} Point;

enum {
    NOANS = -1,
    FREEPLACE = 0,
    STOREPLACE = 1,
    DEADPLACE = 2,

    NOPASS = 0,
    PASSED = 1,
    ONESTEP = 1,
};

int g_size = 0;
int g_linesize = 0;
int g_numOfStore = 0;
int g_min = INT_MAX;

int g_flag[1000][1000] = { { 0 } };

void SetPosMinValue(map<string, int> &posMap, Point point)
{
    string str = to_string(point.pos.x) + "-" + to_string(point.pos.y);
    if (posMap.find(str) != posMap.end()) {
        posMap[str] = posMap[str] < point.step ? posMap[str] : point.step;
    } else {
        posMap[str] = point.step;
    }
}

int GetPosValue(map<string, int> &posMap, Pos pos)
{
    string str = to_string(pos.x) + "-" + to_string(pos.y);
    if (posMap.find(str) != posMap.end()) {
        return posMap[str];
    }
    return INT_MAX;
}

int GetSum(map<string, int> &posMap)
{
    int tot = 0;
    for (map<string, int>::iterator it = posMap.begin(); it != posMap.end(); ++it) {
        tot += it->second;
    }
    return tot;
}

int GetDistanceP2MP(Point dis, int **grid)
{
    stack<Point> stackTemp;
    map<string, int> posMap;
    Point pointTemp;
    Point pointTemp2;
    memset(g_flag, NOPASS, sizeof(g_flag));

    dis.step = 0;
    stackTemp.push(dis);
    g_flag[dis.pos.x][dis.pos.y] = PASSED;
    while (!stackTemp.empty()) {
        pointTemp = stackTemp.top();
        stackTemp.pop();

        if (grid[pointTemp.pos.x][pointTemp.pos.y] == STOREPLACE) {
            SetPosMinValue(posMap, pointTemp);
            continue;
        }
        for (int x = pointTemp.pos.x - ONESTEP; x <= pointTemp.pos.x + ONESTEP; ++x) {
            for (int y = pointTemp.pos.y - ONESTEP; y <= pointTemp.pos.y + ONESTEP; ++y) {
                if ((x >= 0 && x < g_size) && (y >= 0 && y < g_size) &&
                    (x != pointTemp.pos.x && y != pointTemp.pos.y)) {
                    if ((grid[x][y] == STOREPLACE) || (grid[x][y] == FREEPLACE && g_flag[x][y] == NOPASS)) {
                        pointTemp2.pos.x = x;
                        pointTemp2.pos.y = y;
                        pointTemp2.step = pointTemp.step + ONESTEP;
                        if ((grid[x][y] == STOREPLACE && GetPosValue(posMap, pointTemp2.pos) >= pointTemp2.step) ||
                            grid[x][y] == FREEPLACE) {
                            g_flag[x][y] == PASSED;
                            stackTemp.push(pointTemp2);
                        }
                    }
                }
            }
        }
    }

    return posMap.size() == g_numOfStore ? GetSum(posMap) : INT_MAX;
}

int ShortestDistance(int **grid)
{
    g_min = INT_MAX;
    int distanceSum = 0;
    Point pointDis;
    for (int i = 0; i < g_size; ++i) {
        for (int j = 0; j < g_linesize; ++j) {
            if (grid[i][j] == FREEPLACE) {
                pointDis.pos.x = i;
                pointDis.pos.y = j;
                distanceSum = GetDistanceP2MP(pointDis, grid);
                g_min = distanceSum < g_min ? distanceSum : g_min;
            }
        }
    }

    return g_min;
}

int main(void)
{
#if CHCH
    scanf_s("%d", &g_size);
    scanf_s("%d", &g_linesize);
#else
    scanf("%d", &g_size);
    scanf("%d", &g_linesize);
#endif
    g_numOfStore = 0;
    int **grid = (int **)malloc(g_size * sizeof(int **));
    for (int i = 0; i < g_size; i++) {
        grid[i] = (int *)malloc(g_linesize * sizeof(int));
    }
    for (int i = 0; i < g_size; i++) {
        for (int j = 0; j < g_linesize; j++) {
#if CHCH
            scanf_s("%d", &grid[i][j]);
#else
            scanf("%d", &grid[i][j]);
#endif
            if (grid[i][j] == STOREPLACE) {
                g_numOfStore++;
            }
        }
    }

    int result = ShortestDistance(grid);
    if (result == INT_MAX) {
        printf("%d", NOANS);
    } else {
        printf("%d", result);
    }

    return 0;
}

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

#include <string>
#include <queue>

using namespace std;

#define CHCH 1

typedef struct tagPos {
    int x;
    int y;
} Pos;

typedef struct tagPoint {
    Pos pos;
    int step;
    bool operator<(const struct tagPoint &s) const
    {
        return this->step > s.step;
    }
} Point;

enum {
    NOANS = -1,
    FREEPLACE = 0,
    STOREPLACE = 1,
    DEADPLACE = 2,

    NOPASS = 0,
    PASSED = 1,
    ONESTEP = 1,
};

int g_size = 0;
int g_linesize = 0;
int g_numOfStore = 0;
int g_min = INT_MAX;

int g_flag[1000][1000] = { { 0 } };

void PushDueue(Point pointTemp, priority_queue<Point> &pointQue)
{
    if ((pointTemp.pos.x >= 0 && pointTemp.pos.x < g_size) && (pointTemp.pos.y >= 0 && pointTemp.pos.y < g_linesize) &&
        g_flag[pointTemp.pos.x][pointTemp.pos.y] == NOPASS) {
        g_flag[pointTemp.pos.x][pointTemp.pos.y] = PASSED;
        pointQue.push(pointTemp);
    }
    return;
}

int GetDistanceP2MP(Point freePos, int **grid)
{
    int count = 0;
    int totalDis = 0;
    priority_queue<Point> pointQue;
    Point pointTemp;
    Point pointTemp2;
    memset(g_flag, NOPASS, sizeof(g_flag));
    g_flag[freePos.pos.x][freePos.pos.y] = PASSED;
    freePos.step = 0;
    pointQue.push(freePos);
    while (!pointQue.empty()) {
        pointTemp = pointQue.top();
        pointQue.pop();

        if (grid[pointTemp.pos.x][pointTemp.pos.y] == STOREPLACE) {
            count++;
            totalDis += pointTemp.step;
            continue;
        }

        pointTemp2.step = pointTemp.step + ONESTEP;

        pointTemp2.pos.x = pointTemp.pos.x;
        pointTemp2.pos.y = pointTemp.pos.y - ONESTEP;
        PushDueue(pointTemp2, pointQue);
        pointTemp2.pos.y = pointTemp.pos.y + ONESTEP;
        PushDueue(pointTemp2, pointQue);

        pointTemp2.pos.y = pointTemp.pos.y;
        pointTemp2.pos.x = pointTemp.pos.x - ONESTEP;
        PushDueue(pointTemp2, pointQue);
        pointTemp2.pos.x = pointTemp.pos.x + ONESTEP;
        PushDueue(pointTemp2, pointQue);
    }

    return count == g_numOfStore ? totalDis : INT_MAX;
}

int ShortestDistance(int **grid)
{
    g_min = INT_MAX;
    int distanceSum = 0;
    Point freePos;
    for (int i = 0; i < g_size; ++i) {
        for (int j = 0; j < g_linesize; ++j) {
            if (grid[i][j] == FREEPLACE) {
                freePos.pos.x = i;
                freePos.pos.y = j;
                distanceSum = GetDistanceP2MP(freePos, grid);
                g_min = distanceSum < g_min ? distanceSum : g_min;
            }
        }
    }

    return g_min;
}

int main(void)
{
#if CHCH
    scanf_s("%d", &g_size);
    scanf_s("%d", &g_linesize);
#else
    scanf("%d", &g_size);
    scanf("%d", &g_linesize);
#endif
    g_numOfStore = 0;
    int **grid = (int **)malloc(g_size * sizeof(int **));
    for (int i = 0; i < g_size; i++) {
        grid[i] = (int *)malloc(g_linesize * sizeof(int));
    }
    for (int i = 0; i < g_size; i++) {
        for (int j = 0; j < g_linesize; j++) {
#if CHCH
            scanf_s("%d", &grid[i][j]);
#else
            scanf("%d", &grid[i][j]);
#endif
            if (grid[i][j] == STOREPLACE) {
                g_numOfStore++;
            }
        }
    }

    int result = ShortestDistance(grid);
    if (result == INT_MAX) {
        printf("%d", NOANS);
    } else {
        printf("%d", result);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值