Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct point {
- int x;
- int y;
- } point;
- void flood_fill(int N, int ar[N][N], point start) {
- if (start.x < 0 || start.x >=N || start.y < 0 || start.y >=N) //doesn't change anything if encountering already filled or
- return;
- if (ar[start.y][start.x] !=0)
- return;
- if (ar[start.y][start.x]==0) //if the point is equal to 0 then set it to 1
- ar[start.y][start.x] = 1;
- point right;
- if (start.x < N) { //it can move right then it does
- right.x = start.x + 1; right.y = start.y;}
- else { //if the point is within the array boundaries doesn't move past them
- right.x = start.x; right.y = start.y;}
- point left;
- if (start.x > 0) {
- left.x = start.x - 1; left.y = start.y;}
- else {
- left.x = start.x; left.y = start.y;}
- point up;
- if (start.y > 0) {
- up.x = start.x; up.y = start.y - 1; }
- else {
- up.x = start.x; up.y = start.y;}
- point down;
- if (start.y < N+1) {
- down.x = start.x; down.y = start.y + 1; }
- else {
- down.x = start.x; down.y = start.y; }
- flood_fill(N, ar, right); //recursively call each direction
- flood_fill(N, ar, left);
- flood_fill(N, ar, up);
- flood_fill(N, ar, down);
- }
- int main(void) {
- point start;
- start.x = 3;
- start.y = 4;
- int n = 7;
- int arr[7][7] = {{0, 0, 0, 2, 0, 0 ,0}, {0,0,0,2,0,0,0}, {0,0,2,0,0,0,0}, {2,2,0,0,0,2,2}, {0,0,0,0,2,0,0}, {0,0,0,2,0,0,0}, {0,0,0,2,0,0,0}};
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- printf("%d ", arr[i][j]);
- }
- printf("\n");
- }
- flood_fill(n, arr, start);
- printf("\n\n");
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- printf("%d ", arr[i][j]);
- }
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement