Hello everyone, I've got a problem. It's about the fill function, if you ever have heard about it. This is the programm I need your help with:
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
|
#include <iostream>
#include <fstream>
using namespace std;
int a[100][100],n,m,i,j,c=1;
void color(int i,int j)
{if(a[i][j]==1)
{a[i][j]=c;
color(i,j+1);
color(i,j-1);
color(i+1,j);
color(i-1,j);}}
int main()
{ifstream f("colors.in");
ofstream g("colors.out");
f>>n;
f>>m;
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
f>>a[i][j];
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
if(a[i][j]==1)
{c++;
color(i,j);}
for(i=1;i<=n;i++)
{for(j=1;j<=m;j++)
g<<a[i][j]<<" ";
g<<endl;}
g<<"\t";
return 0;}
|
It actually works properly, but i can't understand how to pass through the 'color' function. At most not exactly. Here you have an example i passed through and what exactly my trouble consists of.
Example:
In the 'g' folder, it appears this:
, which is good but i can't get the way it is passed.
I tried to do it on my own but i get stuck everytime at one point:
[code]
Step 1. a[1][1]=1 =>
c=2;
step a: a[1][1]=1 => a[1][1]=2;
step b: a[1][2]=1 => a[1][2]=2;
step c: a[1][3]!=1=> get out of function and return to 'for' ?
Step 2. a[1][2]!=1;
Step 3. a[1][3]!=1;
Step 4. a[1][4]!=1;
Step 5. a[2][1]=1=>
c=3;
step a: a[2][1]=1=>a[2][1]=3;
step b: a[2][2]=1=>a[2][2]=3;
step c: a[2][3]!=1 => again, does it get out of function or something?
Step 6. a[2][2]!=1;
Step 7. a[2][3]!=1;
Step 8. a[2][4]=1=>
c=4;
step a: a[2][4]=1=>a[2][4]=4;
step b: a[2][5]!=1 bc of margins which are all 0.
And after me, the g folder would look like:
2 2 0 0
3 3 0 4
, which is not right. Where have i mistaken? What's the matter? I really can't understand.