Skip to content
Snippets Groups Projects
Verified Commit 3fbeb3bc authored by mirabilos's avatar mirabilos Committed by mirabilos
Browse files

cleaner Dijkstra

parent a0e73aa5
No related branches found
No related tags found
No related merge requests found
#!/bin/mksh
set -e
function doone {
print -r -- 'static const struct ipt I[] = {'
while IFS= read -r line; do
print -r -- " {$line},"
done <"$1"
print '};'
}
exec >inputs.h
print 'struct ipt {'
print ' unsigned x;'
print ' unsigned y;'
print '};'
print
print '#ifndef PROD'
print
if [[ -s c2 ]]; then
print '#if defined(EX2)'
doone c2
for t in 3 4 5 6 7 8 9; do
if [[ -s c$t ]]; then
print "#elif defined(EX$t)"
doone c$t
fi
done
print '#else'
doone c
print '#endif'
else
doone c
fi
print
print '#else'
print
doone i
print
print '#endif'
exec >/dev/null
exit 0
#include <err.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "inputs.h"
#ifndef PROD
#define W 7
#define Z 12
#define SX 0
#define SY 0
#define EX 6
#define EY 6
#else
#define W 71
#define Z 1024
#define SX 0
#define SY 0
#define EX 70
#define EY 70
#endif
struct node {
struct node *next;
struct node *neigh[4];
unsigned long long cost;
unsigned char ok:1;
};
#define INFCOST ((unsigned long long)-1)
static struct node NN[W][W];
#define N(y,x) (&(NN[y][x]))
static struct node *start;
static struct node *finish;
int
main(void)
{
unsigned int x, y, d;
struct node *cur, *unvis, *prev;
unsigned long long newcost;
for (y = 0; y < W; ++y)
for (x = 0; x < W; ++x) {
cur = N(y,x);
cur->neigh[0] = y == 0 ? NULL : N(y-1,x);
cur->neigh[1] = x == W-1 ? NULL : N(y,x+1);
cur->neigh[2] = y == W-1 ? NULL : N(y+1,x);
cur->neigh[3] = x == 0 ? NULL : N(y,x-1);
cur->cost = INFCOST;
cur->ok = 1;
}
for (d = 0; d < Z; ++d) {
cur = N(I[d].y, I[d].x);
cur->ok = 0;
for (x = 0; x < 4; ++x)
if (cur->neigh[x])
cur->neigh[x]->neigh[x ^ 2U] = NULL;
}
if (!((start = N(SY,SX))->ok))
errx(1, "no start position");
if (!((finish = N(EY,EX))->ok))
errx(1, "no end position");
/* Dijkstra 1 */
unvis = start;
prev = start;
for (y = 0; y < W; ++y)
for (x = 0; x < W; ++x) {
cur = N(y,x);
if (cur->ok) {
prev->next = cur;
prev = cur;
}
}
/* Dijkstra 2 */
start->cost = 0;
/* Dijkstra 3 initial */
cur = start;
prev = NULL;
/* Dijkstra 4 */
step4:
for (d = 0; d < 4; ++d)
if (cur->neigh[d]) {
newcost = cur->cost + 1;
if (newcost < cur->neigh[d]->cost)
cur->neigh[d]->cost = newcost;
}
/* Dijkstra 5 */
if (prev == NULL)
unvis = cur->next;
else
prev->next = cur->next;
/* Dijkstra 3 */
if (unvis == NULL)
goto step6;
newcost = unvis->cost;
prev = NULL;
cur = unvis;
while (cur->next) {
if (cur->next->cost < newcost) {
newcost = cur->next->cost;
prev = cur;
}
cur = cur->next;
}
cur = prev ? prev->next : unvis;
if (newcost < INFCOST)
goto step4;
/* Dijkstra 6 */
step6:
if (finish->cost == INFCOST)
errx(1, "no solution");
printf("%llu\n", finish->cost);
return (0);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment