spielplatz:student:42:rusg1
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| spielplatz:student:42:rusg1 [2026/02/14 18:44] – angelegt 89.246.128.70 | spielplatz:student:42:rusg1 [2026/02/14 18:45] (aktuell) – 89.246.128.70 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| + | <code c rush1> | ||
| + | /* ************************************************************************** */ | ||
| + | /* */ | ||
| + | /* ::: :::::::: | ||
| + | /* | ||
| + | /* +:+ +:+ | ||
| + | /* By: dkrkalic < | ||
| + | /* +# | ||
| + | /* | ||
| + | /* | ||
| + | /* */ | ||
| + | /* ************************************************************************** */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | #define SIZE 4 | ||
| + | |||
| + | void ft_putstr(char *str) | ||
| + | { | ||
| + | while (*str) | ||
| + | write(1, str++, 1); | ||
| + | } | ||
| + | |||
| + | void ft_putchar(char c) | ||
| + | { | ||
| + | write(1, &c, 1); | ||
| + | } | ||
| + | |||
| + | int parse_clues(char *input, int clues[16]) | ||
| + | { | ||
| + | int i; | ||
| + | |||
| + | i = 0; | ||
| + | while (*input && i < 16) | ||
| + | { | ||
| + | if (*input == ' ') | ||
| + | input++; | ||
| + | else if (*input >= ' | ||
| + | { | ||
| + | clues[i] = *input - ' | ||
| + | i++; | ||
| + | input++; | ||
| + | } | ||
| + | else | ||
| + | return (0); | ||
| + | } | ||
| + | if (i != 16 || *input) | ||
| + | return (0); | ||
| + | return (1); | ||
| + | } | ||
| + | |||
| + | int is_unique(int grid[SIZE][SIZE], | ||
| + | { | ||
| + | int i; | ||
| + | |||
| + | i = 0; | ||
| + | while (i < SIZE) | ||
| + | { | ||
| + | if (grid[row][i] == num || grid[i][col] == num) | ||
| + | return (0); | ||
| + | i++; | ||
| + | } | ||
| + | return (1); | ||
| + | } | ||
| + | |||
| + | int check_line(int grid[SIZE][SIZE], | ||
| + | { | ||
| + | int max; | ||
| + | int vis; | ||
| + | int i; | ||
| + | |||
| + | max = 0; | ||
| + | vis = 0; | ||
| + | i = (dir % 2 == 0) ? 0 : SIZE - 1; | ||
| + | while (i >= 0 && i < SIZE) | ||
| + | { | ||
| + | if ((dir < 2 && grid[idx][i] > max) || (dir > 1 && grid[i][idx] > max)) | ||
| + | { | ||
| + | max = (dir < 2) ? grid[idx][i] : grid[i][idx]; | ||
| + | vis++; | ||
| + | } | ||
| + | i += (dir % 2 == 0) ? 1 : -1; | ||
| + | } | ||
| + | if (dir == 0) | ||
| + | return (vis == clues[8 + idx]); | ||
| + | if (dir == 1) | ||
| + | return (vis == clues[12 + idx]); | ||
| + | if (dir == 2) | ||
| + | return (vis == clues[idx]); | ||
| + | return (vis == clues[4 + idx]); | ||
| + | } | ||
| + | |||
| + | int check_all(int grid[SIZE][SIZE], | ||
| + | { | ||
| + | int i; | ||
| + | |||
| + | i = 0; | ||
| + | while (i < SIZE) | ||
| + | { | ||
| + | if (!check_line(grid, | ||
| + | || !check_line(grid, | ||
| + | return (0); | ||
| + | i++; | ||
| + | } | ||
| + | return (1); | ||
| + | } | ||
| + | |||
| + | int solve(int grid[SIZE][SIZE], | ||
| + | { | ||
| + | int row; | ||
| + | int col; | ||
| + | int n; | ||
| + | |||
| + | if (pos == SIZE * SIZE) | ||
| + | return (check_all(grid, | ||
| + | row = pos / SIZE; | ||
| + | col = pos % SIZE; | ||
| + | n = 1; | ||
| + | while (n <= SIZE) | ||
| + | { | ||
| + | if (is_unique(grid, | ||
| + | { | ||
| + | grid[row][col] = n; | ||
| + | if (solve(grid, | ||
| + | return (1); | ||
| + | grid[row][col] = 0; | ||
| + | } | ||
| + | n++; | ||
| + | } | ||
| + | return (0); | ||
| + | } | ||
| + | |||
| + | void print_grid(int grid[SIZE][SIZE]) | ||
| + | { | ||
| + | int r; | ||
| + | int c; | ||
| + | |||
| + | r = 0; | ||
| + | while (r < SIZE) | ||
| + | { | ||
| + | c = 0; | ||
| + | while (c < SIZE) | ||
| + | { | ||
| + | ft_putchar(grid[r][c] + ' | ||
| + | if (c < SIZE - 1) | ||
| + | ft_putchar(' | ||
| + | c++; | ||
| + | } | ||
| + | ft_putchar(' | ||
| + | r++; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | int main(int argc, char **argv) | ||
| + | { | ||
| + | int grid[SIZE][SIZE]; | ||
| + | int clues[16]; | ||
| + | int r; | ||
| + | int c; | ||
| + | |||
| + | r = 0; | ||
| + | while (r < SIZE) | ||
| + | { | ||
| + | c = 0; | ||
| + | while (c < SIZE) | ||
| + | { | ||
| + | grid[r][c] = 0; | ||
| + | c++; | ||
| + | } | ||
| + | r++; | ||
| + | } | ||
| + | if (argc != 2 || !parse_clues(argv[1], | ||
| + | ft_putstr(" | ||
| + | else if (solve(grid, | ||
| + | print_grid(grid); | ||
| + | else | ||
| + | ft_putstr(" | ||
| + | return (0); | ||
| + | } | ||
| + | </ | ||