1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| #include <stdlib.h> #include <stdbool.h> #include "snake.h"
int get_arr_size(int *ptr){ ptr++; int size = 1; while(*ptr != 0){ size++, ptr++; } return size; }
int identify_column(int *ptr){ for(int i = 0;; i++){ if(ptr[i] != i) return i; } return -1; }
int snake(int *ptr, int *row, int *column) { int size = get_arr_size(ptr); int c = identify_column(ptr); int r = size / c;
int* correct = (int*) malloc((size + 1) * sizeof(int));
int now_num = 0; for(int i = 0; i < r; i++){ if(i % 2 == 0){ for(int j = 0; j < c; j++){ correct[i * c + j] = now_num; now_num++; } } else{ for(int j = c - 1; j >= 0; j--){ correct[i * c + j] = now_num; now_num++; } } } correct[size] = 0;
for(int i = 0; i < size + 1; i++){ if(ptr[i] != correct[i]){ *row = i / r; if((*row) % 2 == 0){ *column = i % r; } else{ *column = c - (i % r) - 1; } return 0; } }
*row = r; *column = c; return 1; }
|