Skip to content

Instantly share code, notes, and snippets.

@chenclee
Created May 8, 2014
Embed
What would you like to do?
Problem - 377A - Codeforces
#include <stdio.h>
#include <stdlib.h>
void fillAfter (char **, int, int, int *);
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, -1, 0, 1};
int n, m;
int
main (int argc, char **argv)
{
int k, f;
int i, j;
int x, y;
int left;
char **grid;
f = 0;
fscanf (stdin, "%d %d %d ", &n, &m, &k);
grid = malloc (sizeof *grid * n);
for (i = 0; i < n; i++)
{
grid[i] = malloc (sizeof **grid * (m + 2));
fgets (grid[i], m + 2, stdin);
for (j = 0; j < m; j++)
{
if (grid[i][j] == '.')
{
f += 1;
x = i;
y = j;
}
}
}
left = f - k;
fillAfter (grid, x, y, &left);
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
if (grid[i][j] == 'V')
{
grid[i][j] = '.';
}
}
grid[i][m] = '\0';
fprintf (stdout, "%s\n", grid[i]);
free (grid[i]);
}
free (grid);
return EXIT_SUCCESS;
}
void fillAfter (char **grid, int cx, int cy, int *left)
{
char *temp;
int i,j;
if (cx < 0 || cx >= n || cy < 0 || cy >= m)
return;
temp = &grid[cx][cy];
if (*temp == '.')
{
*temp = (*left)-- > 0 ? 'V' : 'X';
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
fillAfter (grid, cx + dx[i], cy + dy[i], left);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment