cses

cses solutions in pure c
git clone git://git.superpozycja.net/cses
log | files | refs

s.c (2013B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <stdint.h>
      5 
      6 struct payload {
      7 	int x;
      8 	int y;
      9 	int n;
     10 	int z;
     11 };
     12 
     13 struct node {
     14 	struct node *next;
     15 	void *payload;
     16 };
     17 
     18 struct node *new()
     19 {
     20 	struct node *n;
     21 
     22 	n = malloc(sizeof(struct node));
     23 	return n;
     24 }
     25 
     26 
     27 struct node *insert(struct node *tail, void *payload)
     28 {
     29 	struct node *node;
     30 	node = malloc(sizeof(struct node));
     31 	node->payload = payload;
     32 
     33 	tail->next = node;
     34 	return tail->next;
     35 }
     36 
     37 struct node *pop(struct node *head)
     38 {
     39 	struct node *new_head;
     40 
     41 	new_head = head->next;
     42 	free(head);
     43 
     44 	return new_head;
     45 }
     46 
     47 int *a;
     48 int *b;
     49 
     50 int solve()
     51 {
     52 	const int moves[8][2] = {
     53 		{-2, -1},
     54 		{-2, +1},
     55 		{-1, -2},
     56 		{-1, +2},
     57 		{+1, -2},
     58 		{+1, +2},
     59 		{+2, -1},
     60 		{+2, +1}
     61 	};
     62 
     63 	struct payload *payload;
     64 	struct node *head;
     65 	struct node *tail;
     66 	int i, j;
     67 	int n;
     68 
     69 	scanf("%d", &n);
     70 
     71 	payload = malloc(sizeof(struct payload));
     72 	payload->x = 0;
     73 	payload->y = 0;
     74 	payload->n = n;
     75 	payload->z = 0;
     76 
     77 	head = malloc(sizeof(struct node));
     78 	head->payload = payload;
     79 
     80 	tail = head;
     81 
     82 	a = calloc(n * n, sizeof(int));
     83 	b = calloc(n * n, sizeof(int));
     84 
     85 	b[0]= 1;
     86 
     87 	do {
     88 		payload = head->payload;
     89 		for (i = 0; i < 8; i++) {
     90 			if (payload->x + moves[i][0] >= 0
     91 			 && payload->x + moves[i][0] < n
     92 			 && payload->y + moves[i][1] >= 0
     93 			 && payload->y + moves[i][1] < n
     94 			 && b[n * (payload->x + moves[i][0]) + payload->y + moves[i][1]] != 1
     95 			 ) {
     96 				struct payload *tmp;
     97 				tmp = malloc(sizeof(struct payload));
     98 				a[n * (payload->x + moves[i][0]) + payload->y + moves[i][1]] = payload->z + 1;
     99 				b[n * (payload->x + moves[i][0]) + payload->y + moves[i][1]] = 1;
    100 				tmp->x = payload->x + moves[i][0];
    101 				tmp->y = payload->y + moves[i][1];
    102 				tmp->n = payload->n;
    103 				tmp->z = payload->z + 1;
    104 				tail = insert(tail, tmp);
    105 			}
    106 		}
    107 		head = head->next;
    108 	} while(head->next != NULL);
    109 
    110 	for (i = 0; i < n; i++) {
    111 		for (j = 0; j < n; j++)
    112 			printf("%d ", a[n * i + j]);
    113 		printf("\n");
    114 	}
    115 
    116 	return 0;
    117 }
    118 
    119 int main()
    120 {
    121 	int t = 1;
    122 	while (t--)
    123 		solve();
    124 }