[ create a new paste ] login | about

Link: http://codepad.org/wOb9ZcOx    [ raw code | output | fork ]

◆6WD5LB4sFVj5 - C, pasted on Feb 7:
/*
Route search program (using depth-limited-search)
author: ◆6WD5LB4sFVj5
*/

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


#define N 47
#define M 7


const char* ken[N] = {
"北海道","青森県","岩手県","宮城県","秋田県","山形県","福島県","茨城県","栃木県","群馬県","埼玉県","千葉県","東京都","神奈川県","新潟県","富山県","石川県","福井県","山梨県","長野県","岐阜県","静岡県","愛知県","三重県","滋賀県","京都府","大阪府","兵庫県","奈良県","和歌山県","鳥取県","島根県","岡山県","広島県","山口県","徳島県","香川県","愛媛県","高知県","福岡県","佐賀県","長崎県","熊本県","大分県","宮崎県","鹿児島県","沖縄県"
};

const int map[N][N] = {
{  0,426, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{426,  0,187, -1,190, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1,187,  0,193,108, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1,193,  0,258, 72, 84, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1,190,108,258,  0,212, -1, -1, -1, -1, -1, -1, -1, -1,169, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, 72,212,  0,102, -1, -1,275, -1, -1, -1, -1,189, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, 84, -1,102,  0,203,172,275,116,128, -1, -1,189, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1,203,  0, 76,109,100,116,128, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1,172, 76,  0,109,103, -1, -1, -1,220, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1,275,275,109,109,103,  0, 69, 24,220, -1, -1, -1, -1,151,151, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1,116,100,103,  0, 69, 24, 50,  0, -1, -1, -1,157,215,220, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1,128,116, -1, 69, 24, 50,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1,128, -1, 24, 50,  0, 37, -1, -1, -1, -1,133,126, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1,220,  0, -1, -1, -1,  0,134, -1, -1,240, -1, -1,167, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1,169,189,189, -1,220, -1, -1, -1, -1,  0,250,250, -1, -1,208,208, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,134,250,  0, 61, -1, -1,196,294, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 61,  0, 83, -1, -1,235, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,157, -1,133, -1, -1, -1, 83,  0, -1, -1,160, -1, -1, -1,176,188, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1,151,215, -1,126,240,208, -1, -1, -1,  0,162, -1,109, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1,151,220, -1, -1, -1,208,196, -1, -1,162,  0,295,271,272, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,294,235,160, -1,295,  0, -1, 43,113,128, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,167, -1, -1, -1, -1,109,271, -1,  0,181, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,272, 43,181,  0, 82, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,113, -1, 82,  0, 95,107, -1, -1, 91, 91, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,176, -1, -1,128, -1, -1, 95,  0, 14, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,188, -1, -1, -1, -1, -1,107, 14,  0, 49, 75, 48, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 49,  0, 45, 33, 80, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 75, 45,  0, -1, -1,180, -1,139, -1, -1,115, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 91, -1, 48, 33, -1,  0, 98, -1, 80, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 91, -1, -1, 80, -1, 98,  0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,180, -1, -1,  0,128,167,296, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 80, -1,128,  0, -1,212, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,139, -1, -1,167, -1,  0,165, -1, -1, 70, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,296,212,165,  0,131, -1, -1,190, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,131,  0,250, -1, -1, -1,165, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,115, -1, -1, -1, -1, -1, -1,250,  0, 73,191,161, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 70, -1, -1, 73,  0,156, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,190, -1,191,156,  0,156, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,161, -1,156,  0, -1, -1, -1, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,165, -1, -1, -1, -1,  0, 61, -1,117,159, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 61,  0,109, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,109,  0, -1, -1, -1, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,117, -1, -1,  0,218,192,187, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,159, -1, -1,218,  0,181, -1, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,192,181,  0,158, -1},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,187, -1,158,  0,733},
{ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,733,  0}
};




int next_neighbor(int node, int seq)
{
    int i;
    for (i=seq; i<N; i++) {
        if (0 < map[node][i])  return i;
    }
    return -1;
}


int route_dist(int* route, int hops)
{
    int dist = 0;
    int i;
    for (i=0; i<hops; i++)  dist += map[ route[i] ][ route[i+1] ];
    return dist;
}


void route_print(int* route, int hops)
{
    int i;
    for (i=0; i<hops; i++)  printf("%s --> ",  ken[ route[i] ]);
    printf("%s", ken[ route[hops] ]);
}


void search(int start, int goal)
{
    int route[M] = {0};
    int seq[M] = {0};
    int dist = 0;
    int min_route[M] = {0};
    int min_dist = INT_MAX;
    int min_hops = -1;
    int i;

    route[0] = start;
    i = 1;

    printf("search route from [%s] to [%s].\n", ken[start], ken[goal]);
    if (start == goal) {
        route_print(route, 0);
        puts("");
        return;
    }

    while (0 < i) {
        route[i] = next_neighbor( route[i-1], seq[i]);
        seq[i] = route[i] + 1;  /* 0-cleared if not found node */

        if (route[i] == -1) {
            /* back track */
            i -= 1;
            continue;
        }

        if (route[i] == goal) {
            dist = route_dist(route, i);
            if (dist < min_dist) {
                printf("%d --> %d found (", min_dist, dist);
                route_print(route, i);
                printf(").\n");
                min_hops = i;
                min_dist = dist;
                memcpy(min_route, route, sizeof(route[0]) * (i+1));
            }
            /* back track */
            seq[i] = 0;
            i -= 1;
            continue;
        }

        i += 1;
        if (i == M)  i -= 1;
    }

    if (0 <= min_hops) {
        route_print(min_route, min_hops);
        puts("");
    }
}


int verify_map(const int map[N][N])
{
    int i, j;
    for (i=0; i<N; i++) {
        for (j=i; j<N; j++) {
            if (map[i][j] != map[j][i])  return 0;
        }
    }
    return 1;
}


int main(const int argc_, const char** argv_)
{
    // for codepad
    const int argc = 3;
    const char* argv[] = {"program", "22", "12"};

    int here, dest;
    int err = 0;

    if (argc != 3) {
        printf("%s (here) (destination)\n", argv[0]);
        exit(1);
    }

    here = atoi(argv[1]);
    dest = atoi(argv[2]);

    if (here < 0 || N <= here || dest < 0 || N <= dest) {
        printf("(here) and (destination) require 0~46\n");
        exit(1);
    }

    search(here, dest);

    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
search route from [愛知県] to [東京都].
2147483647 --> 1131 found (愛知県 --> 長野県 --> 群馬県 --> 山形県 --> 福島県 --> 茨城県 --> 東京都).
1131 --> 1099 found (愛知県 --> 長野県 --> 群馬県 --> 山形県 --> 福島県 --> 群馬県 --> 東京都).
1099 --> 966 found (愛知県 --> 長野県 --> 群馬県 --> 山形県 --> 福島県 --> 埼玉県 --> 東京都).
966 --> 933 found (愛知県 --> 長野県 --> 群馬県 --> 福島県 --> 埼玉県 --> 埼玉県 --> 東京都).
933 --> 864 found (愛知県 --> 長野県 --> 群馬県 --> 福島県 --> 埼玉県 --> 東京都).
864 --> 812 found (愛知県 --> 長野県 --> 群馬県 --> 茨城県 --> 栃木県 --> 茨城県 --> 東京都).
812 --> 741 found (愛知県 --> 長野県 --> 群馬県 --> 茨城県 --> 栃木県 --> 群馬県 --> 東京都).
741 --> 665 found (愛知県 --> 長野県 --> 群馬県 --> 茨城県 --> 群馬県 --> 東京都).
665 --> 660 found (愛知県 --> 長野県 --> 群馬県 --> 茨城県 --> 東京都).
660 --> 653 found (愛知県 --> 長野県 --> 群馬県 --> 群馬県 --> 群馬県 --> 東京都).
653 --> 550 found (愛知県 --> 長野県 --> 群馬県 --> 群馬県 --> 東京都).
550 --> 447 found (愛知県 --> 長野県 --> 群馬県 --> 東京都).
447 --> 410 found (愛知県 --> 岐阜県 --> 福井県 --> 埼玉県 --> 東京都).
410 --> 336 found (愛知県 --> 岐阜県 --> 福井県 --> 東京都).
愛知県 --> 岐阜県 --> 福井県 --> 東京都


Create a new paste based on this one


Comments: