Home > Back-end >  Data structure in the image dyeing
Data structure in the image dyeing

Time:09-27

assumption in two-dimensional array g (1.. M, 1.. N) said an image area, g (I, j] said the region point (I, j) is the color, the value of an integer from 0 to k. Write algorithm replacement point (i0, j0) the color of the area. Conventions and (i0 j0) adjacent points up and down or so of the same color as the same color area of the point. And why do run results (see picture), and counsel,
 
//this is CPP file
#include
# include "chang_color. H"
# define m_max 50
# define n_max 50
Int main ()
{
Int I, j, m, n, i0, j0, new_color;
Printf (" input color area the height and width of the: ");
The scanf (" % d % d ", & amp; M, & amp; N);
Int area [m_max + 1] [n_max + 1];
Void chang_color (int array [m_max + 1], [n_max + 1] int m, int n, int i0, int j0, int new_color);
Printf (" \ n please enter the color of each point: \ n ");
for(i=1; i<=m; I++)
{
For (j=1; J<=n; J++)
{
The scanf (" % d ", & amp; Area [I] [j]);
}
}
Printf (" \ n input want to change the color of the point and a new color value: ");
The scanf (" % d % d % % d ", & amp; I0, & amp; J0, & amp; New_color);
Chang_color (area, m, n, i0 j0, new_color);
Printf (" after the change color: \ n ");
for(i=1; i<=m; I++)
{
For (j=1; J<=n; J++)
{
Printf (" % d \ t ", area [I] [j]);
}
Putchar (");
}
Putchar (");
Return (0);
}

//this is chang_color. H
# include "stack_function. H"
#include
# define m_max 50
# define n_max 50
Void chang_color (int array [m_max + 1], [n_max + 1] int m, int n, int i0, int j0, int new_color)
{
If (i0 & lt; 1 | | i0 & gt; M | | j0 & lt; 1 | | j0 & gt; N) {printf (" this position does not exist "); exit(0); }
Int color_init=array [i0] [j0], x, y;
Sqstack s;
Initstack (s);
Push (s, ");
Push (s, j0);
Do
{
Pop (s, y);
Pop (s, x);
If (x - 1 & gt; 0 & amp; & Array [1] x [y]==color_init)
{
Array [1] x [y]=new_color;
Push (s, x - 1);
Push (s, y);
}
If (y - 1 & gt; 0 & amp; & Array [x] [1] y==color_init)
{
Array [x] [1] y=new_color;
Push (s, x);
Push (s, y - 1);
}
If (x + 1 & lt;=m& & Array [x + 1] [y]==color_init)
{
Array [x + 1] [y]=new_color;
Push (s, x + 1);
Push (s, y);
}
If (y + 1 & lt;=n& & Array [x] [y + 1]==color_init)
{
Array [x] [y + 1]=new_color;
Push (s, x);
Push (s, y + 1);
}
} while (! Stackempty (s));
}



//this is I write stack operation function header file
#include
#include
# define stack_max_size 20
# define stack_increase_size 10
# define ElemType int
Typedef struct
{
ElemType * top;
ElemType * base;
Int stack_allow_size;
} Sqstack;
Void Initstack (Sqstack& S)
{
S.t op=s.b ase=(ElemType *) malloc (stack_max_size * sizeof (ElemType));
if(! S.b ase)
{
Printf (" failed to create the stack ");
exit(0);
}
S.s tack_allow_size=stack_max_size;
}
Void Destroystack (Sqstack & amp; S)
{
Free (s.b ase);
S.b ase=NULL;
S.t op=NULL;
S.s tack_allow_size=0;
}
Void Clearstack (Sqstack& S)
{
S.t op=s.b ase;
}
Bool Stackempty (Sqstack s)
{
If (s.b ase==s.t op)
Return (true);
The else return (false);
}
Int stacklength Sqstack (s)
{
Return (s.t op - s.b ase);
}
Void gettop (Sqstack s ElemType& E)
{
If (s.t op==s.b ase) printf (" this stack is empty, cannot take the stack elements ");
E=* (s.t op - 1);
}
Void push (Sqstack& S, ElemType e)
{
If ((s.t op - s.b ase) & gt;=s.s tack_allow_size)
{
S.b ase=(ElemType *) realloc (s.b ase, (s.s tack_allow_size + stack_increase_size) * sizeof (ElemType));
if(! S.b ase) {printf (" space expanding failure "); exit(0); }
S.t op=s.b ase + s.s tack_allow_size;
S.s tack_allow_size +=stack_increase_size;
}
* s.t op++=e;
}
Pop (Sqstack& is void; S, ElemType& E)
{
If (s.t op==s.b ase)
{
Printf (" this stack is empty, delete the stack elements operation cannot be ");
}
The else e=* (-- s.t op);
}
  • Related