ClavinJune

Simple floodfill

Apr 24th, 2017
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.21 KB | None | 0 0
  1. #include "stdio.h"
  2. #define h 20
  3. #define w 20
  4. #define floodY 10
  5. #define floodX 10
  6. struct node{
  7.     char content;
  8.     bool visited;
  9. }map[h][w];
  10.  
  11. void create(){
  12.     for(int i=0;i<h;i++)
  13.         for(int j=0;j<w;j++){
  14.             if(i == 0 || i == 19 || j == 0 || j == 19){
  15.                 map[i][j].content ='#';
  16.                 map[i][j].visited = true;
  17.             }else{
  18.                 map[i][j].content =' ';
  19.                 map[i][j].visited = false;
  20.             }
  21.         }
  22.         for(int i=0;i<=9;i++){
  23.             map[i][9].content = '#';
  24.             map[9][i].content = '#';
  25.         }
  26.         for(int i=11;i<=18;i++){
  27.             map[i][10].content = '#';
  28.             map[10][i].content = '#';
  29.         }
  30. }
  31.  
  32. void view(){
  33.     for(int i=0;i<h;i++){
  34.         for(int j=0;j<w;j++){
  35.             printf("%c", map[i][j].content);
  36.         }
  37.         puts("");
  38.     }
  39. }
  40.  
  41. void floodfill(int x, int y){
  42.     if(x > 0 && y > 0 && x <19 && y < 19 && !map[x][y].visited){
  43.         if(map[x][y].content == '#') return;
  44.         map[x][y].content = 'x';
  45.         map[x][y].visited = true;
  46.         floodfill(x+1,y);
  47.         floodfill(x,y+1);
  48.         floodfill(x-1,y);
  49.         floodfill(x,y-1);
  50.     }
  51. }
  52.  
  53. void check(){
  54.     for(int i=0;i<h;i++)
  55.         for(int j=0;j<w;j++){
  56.             if(!map[i][j].visited){
  57.                 printf("Flood");
  58.                 return;
  59.             }
  60.         }
  61. }
  62.  
  63. int main(){
  64.     create();
  65.     floodfill(floodX,floodY);
  66.     view();
  67.     check();
  68.     getchar();
  69.     return 0;
  70. }
Add Comment
Please, Sign In to add comment