博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode 505 迷宫2
阅读量:4191 次
发布时间:2019-05-26

本文共 2033 字,大约阅读时间需要 6 分钟。

题目:

 

 

代码:

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

#define MAX_NUM (100 * 100)

#define MAX_ROW 100
#define MAX_COL 100
#define MIN(a,b) ((a) < (b) ? (a) : (b))
typedef struct loc {
    int row;
    int col;
} loc;

int g_len[MAX_ROW][MAX_COL];

loc g_start;
int g_mazesize;
int **g_maze = NULL;
int g_max_col = 0;

loc g_direction[4] = { {-1, 0}, { 1, 0 }, { 0, -1 }, {0, 1 } };

void init_data(void)

{
    for (int i = 0; i < MAX_ROW; i++) {
        for (int j = 0; j < MAX_COL; j++) {
            g_len[i][j] = INT_MAX;
        }
    }

    g_len[g_start.row][g_start.col] = 0;

}

void dfs(loc start, int len)

{
    int tmp_len = 0;
    int i;
    loc tmp_loc;
    int x;
    int y;

    // for 4 directions

    for (i = 0; i < 4; i++) {
        x = start.row + g_direction[i].row;
        y = start.col + g_direction[i].col;
        tmp_len = 0;

        while (x >= 0 && x < g_mazesize && y >= 0 & y < g_max_col && g_maze[x][y] == 0) {

            x = x + g_direction[i].row;
            y = y + g_direction[i].col;
            tmp_len++;
        }

        if (g_len[start.row][start.col] + tmp_len < g_len[x - g_direction[i].row][y - g_direction[i].col]) {

            g_len[x - g_direction[i].row][y - g_direction[i].col] = g_len[start.row][start.col] + tmp_len;
            tmp_loc.row = x - g_direction[i].row;
            tmp_loc.col = y - g_direction[i].col;
            dfs(tmp_loc, g_len[start.row][start.col] + tmp_len);
        } 
    }

}

// row = col ?  mazeSize = mazeColSize no 

int shortestDistance(int** maze, int mazeSize, int* mazeColSize, int* start, int startSize, int* destination, int destinationSize){
    g_start.row = start[0];
    g_start.col = start[1];
    g_mazesize = mazeSize; // max row
    g_max_col = mazeColSize[0];
    g_maze = maze;

    // printf("g_max_col %d\n", g_max_col);

    init_data();

    dfs(g_start,0);

    // solve_data();
    // get_result();

    if (g_len[destination[0]][destination[1]] == INT_MAX) {

        return -1;
    }
    return g_len[destination[0]][destination[1]];
}

总给&注意事项:

1. 建立对应元素的长度数组,并使用长度作为dfs不循环执行的判断条件
2. 数组行数和列数不一定会相等
3. return distance[dest[0]][dest[1]] == Integer.MAX_VALUE ? -1 : distance[dest[0]][dest[1]]; 减少代码行数
4. 使用x  y 表示行列, 不使用结构体,减少代码量
5. direction命名简洁化

6. 可以尝试使用bfs以及ditrista进行求解

转载地址:http://niloi.baihongyu.com/

你可能感兴趣的文章
救救网瘾父母
查看>>
苹果股价周一下跌4.17% 市值今年首次跌破2万亿美元
查看>>
“蚂蚁呀嘿”App被下架,相关商标早已被抢注
查看>>
手机越贵打车越贵?教授打车800次总结出规律,律师:属于违法欺诈行为
查看>>
汽车之家港股上市发行价定为176.3港元 募资35.6亿港元
查看>>
中国首富又换人了!
查看>>
996问题已经失控,委员建议进行监管!网友拍手称快...
查看>>
基金大跌,基民上闲鱼“卖货回血”了!支付宝深夜发文!真的没人买基了?...
查看>>
《阿凡达》3月12日内地重映:部分影院已开启预售
查看>>
官方正式预热小米10S:哈曼卡顿加持小米有史以来音质最好的手机
查看>>
电影《你好,李焕英》进入全球票房榜前100
查看>>
不用再更换整机了,苹果官方可修复iPhone 12系列破裂后盖玻璃
查看>>
消息称网易云音乐寻求在港上市 或于明年正式IPO
查看>>
力压腾讯!《原神》连续5个月成中国手游海外收入冠军
查看>>
小镇青年“魂断”博彩暴富梦
查看>>
小米10S继承“祖传”三重快充:50W有线+30W无线+10W反充
查看>>
华为公开“实现汽车中电子控制功能的系统”相关专利
查看>>
华为Mate40 RS保时捷设计推8+256GB版本:起售价便宜1000元
查看>>
美团股价盘中涨幅超7% 市值重回2万亿港元关口
查看>>
微信又更新了!支持上班摸鱼了
查看>>