#include #include #include #include #include #include #include //from list of nodes not in tree, find next smallest dist node int next_nearest (int *dist, bool *in_spf, int n) { int i; int min = INT_MAX; int min_index = -1; for (i = 0; i < n; i++) { if (in_spf[i] == false && dist[i] < min) { min = dist[i]; min_index = i; } } return min_index; } void print_spf (int *dist, int n, int src) { int i; printf("printing spf for src: %d\n", src); for (i = 0; i < n; i++) { if (i == src) continue; if (dist[i] == INT_MAX) printf("-1 "); else printf("%d ", dist[i]); } printf("\n"); } void Dijkstra (int **graph, int n, int src) { int i,j; int *dist; bool *in_spf; int u, v; dist = malloc(sizeof(int) * n); in_spf = malloc(sizeof(bool) * n); for (i = 0; i < n; i++ ) { dist[i] = INT_MAX; in_spf[i] = false; } dist[src] = 0; for (i = 0; i < n-1; i++) { u = next_nearest (dist, in_spf, n); in_spf[u] = true; if (u == -1) { //printf("why u -1 ??\n"); break; } for (v = 0; v < n; v++) { if (in_spf[v]) continue; if (graph[u][v] && dist[u] != INT_MAX && (dist[u] + graph[u][v] < dist[v])) dist[v] = dist[u] + graph[u][v]; } } //printf("in djk;; printing \n"); //print_spf(dist, n, src); //printf("%d ", dist[n-1]); if (dist[n-1] == INT_MAX) printf("-1 "); else printf("%d ", dist[n-1]); //free(dist); //free(in_spf); } void clear_graph (int **graph, int n) { int i, j; for (i = 0; i < n; i++) for (j = 0; j < n; j++) graph[i][j] = 0; } void show_graph (int **graph, int a, int b, int n) { int i, j; printf("printing graph fpr a: %d b: %d\n", a, b); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%d ", graph[i][j]); } printf("\n"); } } //given coordinate, get the vertex number int coordinate_to_vertex (int x, int y, int n){ if (x >= n || x < 0) return -1; if (y >= n || y < 0) return -1; //printf("N : %d vertex count is: %d\n", n, x*n + y); return (x*n + y); } //for each node; the next possible move forms a link..set to 1 //x2 = x1 +- a y2 = y1 +- b void populate_graph (int **graph, int a, int b, int n){ int i, j; int src_node, dest_node; int V = n*n; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { src_node = coordinate_to_vertex(i, j, n); dest_node = coordinate_to_vertex(i+a, j+b, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i+a, j-b, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i-a, j+b, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i-a, j-b, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i+b, j+a, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i+b, j-a, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i-b, j+a, n); if (dest_node != -1) graph[src_node][dest_node] = 1; dest_node = coordinate_to_vertex(i-b, j-a, n); if (dest_node != -1) graph[src_node][dest_node] = 1; } } } int main(){ int n; scanf("%d",&n); // your code goes here int a, b, i, V; int **graph; V = n*n; graph = malloc(sizeof(int *) * V); for (i = 0; i < V; i++) graph[i] = malloc(sizeof(int) * V); for (a = 1; a < n; a++) { for (b = 1; b < n; b++) { clear_graph(graph, V); populate_graph(graph, a, b, n); //show_graph(graph, a, b, V); Dijkstra(graph, V, 0); } printf("\n"); } for (i = 0; i < V; i++) free(graph[i]); free(graph); return 0; }