Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Enchente
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Harbor Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Vytor Calixto
Enchente
Commits
bc0b4155
Commit
bc0b4155
authored
8 years ago
by
Vytor Calixto
Browse files
Options
Downloads
Patches
Plain Diff
Add implementação exemplo
parent
4defb8db
Branches
Branches containing commit
No related tags found
1 merge request
!1
Develop
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
floodit_exemplo.c
+126
-0
126 additions, 0 deletions
floodit_exemplo.c
with
126 additions
and
0 deletions
floodit_exemplo.c
0 → 100644
+
126
−
0
View file @
bc0b4155
#include
<stdlib.h>
#include
<stdio.h>
#include
<time.h>
typedef
struct
{
int
nlinhas
;
int
ncolunas
;
int
ncores
;
int
**
mapa
;
}
tmapa
;
void
gera_mapa
(
tmapa
*
m
,
int
semente
)
{
int
i
,
j
;
if
(
semente
<
0
)
srand
(
time
(
NULL
));
else
srand
(
semente
);
m
->
mapa
=
(
int
**
)
malloc
(
m
->
nlinhas
*
sizeof
(
int
*
));
for
(
i
=
0
;
i
<
m
->
nlinhas
;
i
++
)
{
m
->
mapa
[
i
]
=
(
int
*
)
malloc
(
m
->
ncolunas
*
sizeof
(
int
));
for
(
j
=
0
;
j
<
m
->
ncolunas
;
j
++
)
m
->
mapa
[
i
][
j
]
=
1
+
rand
()
%
m
->
ncores
;
}
}
void
carrega_mapa
(
tmapa
*
m
)
{
int
i
,
j
;
scanf
(
"%d"
,
&
(
m
->
nlinhas
));
scanf
(
"%d"
,
&
(
m
->
ncolunas
));
scanf
(
"%d"
,
&
(
m
->
ncores
));
m
->
mapa
=
(
int
**
)
malloc
(
m
->
nlinhas
*
sizeof
(
int
*
));
for
(
i
=
0
;
i
<
m
->
nlinhas
;
i
++
)
{
m
->
mapa
[
i
]
=
(
int
*
)
malloc
(
m
->
ncolunas
*
sizeof
(
int
));
for
(
j
=
0
;
j
<
m
->
ncolunas
;
j
++
)
scanf
(
"%d"
,
&
(
m
->
mapa
[
i
][
j
]));
}
}
void
mostra_mapa
(
tmapa
*
m
)
{
int
i
,
j
;
printf
(
"%d %d %d
\n
"
,
m
->
nlinhas
,
m
->
ncolunas
,
m
->
ncores
);
for
(
i
=
0
;
i
<
m
->
nlinhas
;
i
++
)
{
for
(
j
=
0
;
j
<
m
->
ncolunas
;
j
++
)
if
(
m
->
ncores
>
10
)
printf
(
"%02d "
,
m
->
mapa
[
i
][
j
]);
else
printf
(
"%d "
,
m
->
mapa
[
i
][
j
]);
printf
(
"
\n
"
);
}
}
void
mostra_mapa_cor
(
tmapa
*
m
)
{
int
i
,
j
;
char
*
cor_ansi
[]
=
{
"
\x1b
[0m"
,
"
\x1b
[31m"
,
"
\x1b
[32m"
,
"
\x1b
[33m"
,
"
\x1b
[34m"
,
"
\x1b
[35m"
,
"
\x1b
[36m"
,
"
\x1b
[37m"
,
"
\x1b
[30;1m"
,
"
\x1b
[31;1m"
,
"
\x1b
[32;1m"
,
"
\x1b
[33;1m"
,
"
\x1b
[34;1m"
,
"
\x1b
[35;1m"
,
"
\x1b
[36;1m"
,
"
\x1b
[37;1m"
};
if
(
m
->
ncores
>
15
)
{
mostra_mapa
(
m
);
return
;
}
printf
(
"%d %d %d
\n
"
,
m
->
nlinhas
,
m
->
ncolunas
,
m
->
ncores
);
for
(
i
=
0
;
i
<
m
->
nlinhas
;
i
++
)
{
for
(
j
=
0
;
j
<
m
->
ncolunas
;
j
++
)
if
(
m
->
ncores
>
10
)
printf
(
"%s%02d%s "
,
cor_ansi
[
m
->
mapa
[
i
][
j
]],
m
->
mapa
[
i
][
j
],
cor_ansi
[
0
]);
else
printf
(
"%s%d%s "
,
cor_ansi
[
m
->
mapa
[
i
][
j
]],
m
->
mapa
[
i
][
j
],
cor_ansi
[
0
]);
printf
(
"
\n
"
);
}
}
void
pinta
(
tmapa
*
m
,
int
l
,
int
c
,
int
fundo
,
int
cor
)
{
m
->
mapa
[
l
][
c
]
=
cor
;
if
(
l
<
m
->
nlinhas
-
1
&&
m
->
mapa
[
l
+
1
][
c
]
==
fundo
)
pinta
(
m
,
l
+
1
,
c
,
fundo
,
cor
);
if
(
c
<
m
->
ncolunas
-
1
&&
m
->
mapa
[
l
][
c
+
1
]
==
fundo
)
pinta
(
m
,
l
,
c
+
1
,
fundo
,
cor
);
if
(
l
>
0
&&
m
->
mapa
[
l
-
1
][
c
]
==
fundo
)
pinta
(
m
,
l
-
1
,
c
,
fundo
,
cor
);
if
(
c
>
0
&&
m
->
mapa
[
l
][
c
-
1
]
==
fundo
)
pinta
(
m
,
l
,
c
-
1
,
fundo
,
cor
);
}
void
pinta_mapa
(
tmapa
*
m
,
int
cor
)
{
if
(
cor
==
m
->
mapa
[
0
][
0
])
return
;
pinta
(
m
,
0
,
0
,
m
->
mapa
[
0
][
0
],
cor
);
}
int
main
(
int
argc
,
char
**
argv
)
{
int
cor
;
tmapa
m
;
int
semente
;
if
(
argc
<
4
||
argc
>
5
)
{
printf
(
"uso: %s <numero_de_linhas> <numero_de_colunas> <numero_de_cores> [<semente_aleatoria>]
\n
"
,
argv
[
0
]);
exit
(
1
);
}
m
.
nlinhas
=
atoi
(
argv
[
1
]);
m
.
ncolunas
=
atoi
(
argv
[
2
]);
m
.
ncores
=
atoi
(
argv
[
3
]);
if
(
argc
==
5
)
semente
=
atoi
(
argv
[
4
]);
else
semente
=
-
1
;
gera_mapa
(
&
m
,
semente
);
mostra_mapa_cor
(
&
m
);
scanf
(
"%d"
,
&
cor
);
while
(
cor
>
0
&&
cor
<=
m
.
ncores
)
{
pinta_mapa
(
&
m
,
cor
);
mostra_mapa_cor
(
&
m
);
// para mostrar sem cores use mostra_mapa(&m);
scanf
(
"%d"
,
&
cor
);
}
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment