Thanks to visit codestin.com
Credit goes to gist.github.com

Skip to content

Instantly share code, notes, and snippets.

@fikarme
Created July 19, 2025 09:32
Show Gist options
  • Select an option

  • Save fikarme/c9db9bab3911657fbfffd4cf310dcc11 to your computer and use it in GitHub Desktop.

Select an option

Save fikarme/c9db9bab3911657fbfffd4cf310dcc11 to your computer and use it in GitHub Desktop.
//gcc -Wall -Wextra -Werror -std=c98 -o meys biggest_square.c
//echo -e "5.ox\n.....\n.....\noooo.\n.....\n....." | ./meys
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int min3(int a, int b, int c) {
int r = a;
if (b < r) r = b;
if (c < r) r = c;
return (r);
}
int err(FILE *file, char *line) {
printf("ERROR\n");
if (file && file != stdin) fclose(file);
if (line) free(line);
return 1;
}
int main(int ac, char **av) {
if (ac > 2)
return err(NULL, NULL);
FILE *file = NULL;
if (ac == 1)
file = stdin;
if (ac == 2)
file = fopen(av[1], "r");
if (!file)
return err(file, NULL);
char *line = NULL;
size_t len = 0;
ssize_t bytes = getline(&line, &len, file);
if (bytes == -1)
return err(file, line);
int row = 0, i = 0;
while (line[i] >= '0' && line[i] <= '9')
row = row * 10 + (line[i++] - '0');
if (i == 0 || row <= 0 || bytes < i + 3)
return err(file, line);
char empt = line[i++];
char obst = line[i++];
char full = line[i++];
if (empt < 32 || empt > 126 ||
obst < 32 || obst > 126 ||
full < 32 || full > 126 ||
empt == obst || empt == full || obst == full)
return err(file, line);
free(line);
line = NULL;
char grid[row][1001];
int dp[row][1001];
int col = 0, max_size = 0, max_row = 0, max_col = 0;
for (int r = 0; r < row; r++) {
bytes = getline(&line, &len, file);
if (bytes < 0 || line[bytes - 1] != '\n')
return err(file, line);
line[bytes - 1] = '\0';
bytes--;
if (r == 0) col = bytes;
else if (bytes != col || col <= 0)
return err(file, line);
for (int c = 0; c < col; c++) {
if (line[c] != empt && line[c] != obst)
return err(file, line);
grid[r][c] = line[c];
}
grid[r][col] = '\0';
for (int c = 0; c < col; c++) {
if (grid[r][c] == obst)
dp[r][c] = 0;
else if (r == 0 || c == 0)
dp[r][c] = 1;
else
dp[r][c] = 1 + min3(dp[r-1][c], dp[r][c-1], dp[r-1][c-1]);
if (dp[r][c] > max_size) {
max_size = dp[r][c];
max_row = r;
max_col = c;
}
}
}
free(line);
if (file != stdin) fclose(file);
for (int r = max_row - max_size + 1; r <= max_row; r++)
for (int c = max_col - max_size + 1; c <= max_col; c++)
grid[r][c] = full;
for (int r = 0; r < row; r++)
printf("%s\n", grid[r]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment