TIL(today i learned)/C++

Flood-Fill algorithm

코딩의 숲 2020. 9. 11. 23:17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include<iostream>
#include<vector>
using namespace std;
int counter = 0;
int x = 1, y = 1;
vector<int> number;
bool flood_fill[10][10= { 0 };
void calculator(int x, int y);
void alpha()
{
 if (counter)
 {
  number.push_back(counter);
 }
 counter = 0;
 calculator(x, y);
 if (x == 8&&y==8)
 {
  return;
 }
 else if (y < 8)
 {
  y++;
 }
 else
 {
  x++;
  y = 1;
 }
 alpha();
}
void calculator(int x,int y)
{
 if (flood_fill[x][y])
 {
  flood_fill[x][y] = false;
  counter++;
  calculator(x + 1, y);
  calculator(x - 1, y);
  calculator(x, y + 1);
  calculator(x, y - 1);
 }
}
int main()
{
 for (int i = 1; i < 9; i++)
 {
  for (int j = 1; j < 9; j++)
  {
   if (rand() % 2 == 0)
   {
    flood_fill[i][j] = true;
   }
  }
 }
 for (int i = 0; i < 10; i++)
 {
  for (int j = 0; j < 10; j++)
  {
   if (flood_fill[i][j])
   {
    cout << 'w' << " ";
   }
   else
   {
    cout << 'b' << " ";
   }
  }
  cout << endl;
 }
 alpha();
 cout << number.size() << "white area of ";
 for (vector<int>::size_type i = 0; i < number.size()-1; i++)
 {
  cout << number[i] << ",";
 }
 cout << "and "<<number[number.size()-1]<<" cells";
 return 0;
}
cs