I have this code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
long result = LONG_MAX;
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int hasConnection(int *array, int arrayIndex, int maxBound, int *rules, int size) {
int connection;
for (int i = 0; i < (size - 1)*2; i++) {
if (rules[i] == array[arrayIndex]) {
if (i % 2) {
connection = rules[i - 1];
} else {
connection = rules[i + 1];
}
for (int j = maxBound - 1; j >= 0; j--) {
if (array[j] == connection) {
return 1;
}
}
}
}
return 0;
}
int isCrossed(int *array, int outConnectionIndex, int inConnectionIndex, int *rules, int size) {
for (int i = inConnectionIndex + 1; i < outConnectionIndex; i++) {//sweep trough indexes in between
if (hasConnection(array, i, inConnectionIndex, rules, size)) {//array[i] has connection with index lower than inConnectionIndex
return 1;
}
}
return 0;
}
int isWiredInsideAndCrossed(int *array, int arrayIndex, int *rules, int size) {
int connection;
for (int i = 0; i < 2 * (size - 1); i++) {
if (rules[i] == array[arrayIndex]) {
if (i % 2) {
connection = rules[i - 1];
} else {
connection = rules[i + 1];
}
for (int j = 1; j < arrayIndex - 1; j++) {
if (array[j] == connection) {
if (isCrossed(array, arrayIndex, j, rules, size)) {
return 1;
}
}
}
}
}
return 0;
}
void trySequence(int * array, int size, int *priceMap, int *rules) {
int ret = 0;
for (int i = 0; i < size; i++) {
ret = ret + priceMap[i * size + array[i]];
if (ret >= result || isWiredInsideAndCrossed(array, i, rules, size)) {
return;
}
}
result = ret;
}
void permute(int *array, int i, int size, int *priceMap, int *rules) {
if (size == i) {
trySequence(array, size, priceMap, rules);
return;
}
int j = i;
for (j = i; j < size; j++) {
swap(array + i, array + j);
permute(array, i + 1, size, priceMap, rules);
swap(array + i, array + j);
}
return;
}
int main(int argc, char** argv) {
int size;
fscanf(stdin, "%d", &size);
int *priceMap = malloc(sizeof (int)*size * size);
int *rules = malloc(sizeof (int)*(size - 1)*2);
int i = 0;
int squaredSize = size*size;
while (i < squaredSize) {
scanf("%d", priceMap + i);
i++;
}
i = 0;
int rulesSize = (size - 1)*2;
while (i < rulesSize) {
scanf("%d", rules + i);
i++;
}
int arrayToPermute [size];
for (int j = 0; j < size; j++) {
arrayToPermute[j] = j;
}
permute(arrayToPermute, 0, size, priceMap, rules);
printf("%ld\n", result);
return (EXIT_SUCCESS);
}
It's supposed to do this
EDIT: Basicaly the task is to find the cheapest combination of N devices placed in N slots on the edge of a circle, on the input there is a cost matrix that tells what each device would cost to install in each slot, devices are also connected to each other by wires that mustn't cross. There are always N-1 connections. Details and examples are on the link above.
My problem is that my solution is way too slow. I need it to solve a drill head of size 13 generally in less than two seconds. Too be precise: I need this input solved in less than 1s:
12
27 25 21 27 25 30 27 26 22 28 27 26
21 22 26 30 25 28 21 21 22 23 22 30
20 21 22 20 30 30 30 22 30 26 23 26
27 30 24 21 20 24 26 24 22 22 24 22
29 26 20 29 22 23 27 28 23 28 30 27
21 21 20 30 20 22 25 29 22 29 27 24
26 21 30 24 23 23 29 29 29 28 23 22
25 27 21 24 20 24 27 23 27 28 25 26
26 27 23 27 23 27 29 30 25 24 20 23
20 22 25 20 23 26 20 29 21 24 25 20
27 28 25 20 25 22 26 23 24 21 26 23
23 21 28 23 26 30 22 30 25 26 26 20
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
(solution is 262) and this in less than 2s:
13
52 9 42 65 54 47 16 62 35 47 63 2 48
25 4 12 25 58 12 45 62 70 60 40 17 33
28 64 64 62 1 28 3 26 56 15 59 64 17
7 23 70 20 57 70 46 5 6 1 21 12 40
62 53 5 15 22 43 57 15 26 42 51 16 38
20 13 64 3 51 22 28 1 18 27 4 36 9
11 20 41 65 29 63 54 28 31 63 27 59 41
44 21 42 16 59 10 60 11 3 53 52 53 37
41 51 18 4 38 6 22 49 15 51 54 61 7
54 6 5 24 47 35 46 11 26 17 53 37 25
34 42 6 54 40 47 59 25 53 53 37 9 64
69 63 68 5 37 16 17 61 33 51 19 39 44
6 47 4 6 21 17 23 24 13 29 34 54 33
0 1
0 2
0 3
1 4
1 5
1 6
2 7
2 8
2 9
3 10
3 11
3 12
.(solution is 165) So I'm wondering if anybody would have an idea how to do this, I'm completely lost?