 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ivo Guest
|
Posted: Wed Dec 03, 2003 1:18 am Post subject: Task Problem!!!! |
|
|
Could someone please solve the following problem:
There is a given txt file like this (example):
00011100001000
00100011110100
00100000000010
00010000000001
00010000000010
00001000000100
00000111111000
We should read the txt file and...
The 1's make an outline (shape) - contour. This info should be placed
in
a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.
Please if someone solves the problem send it to me:
[email]ivo_malinkovski (AT) hotmail (DOT) com[/email]
|
|
| Back to top |
|
 |
TroLoo Guest
|
Posted: Wed Dec 03, 2003 1:28 am Post subject: Re: Task Problem!!!! |
|
|
On 2003-12-03 02:18, Ivo wrote :
| Quote: | a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.
Please if someone solves the problem send it to me:
[email]ivo_malinkovski (AT) hotmail (DOT) com[/email]
What's the problem? I assume that you don't have problem with reading a |
file and other simple stidio stuff. You just have to read row by row
(your matrix) and after finding first 1, replace all 0's with 1's until
finding the second 1. That's it, if you have problem with reading a file
let me know on priv.
--
--== T r o L o o ==--
Greetings
ICQ:83033632
|
|
| Back to top |
|
 |
TroLoo Guest
|
Posted: Wed Dec 03, 2003 1:31 am Post subject: Re: Task Problem!!!! |
|
|
On 2003-12-03 02:18, Ivo wrote :
| Quote: | a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.
Please if someone solves the problem send it to me:
[email]ivo_malinkovski (AT) hotmail (DOT) com[/email]
What's the problem? I assume that you don't have problem with reading a |
file and other simple stdio stuff. You just have to read row by row
(your matrix) and after finding first 1, replace all 0's with 1's until
finding the second 1. That's it, if you have problem with reading a file
let me know on priv.
--
--== T r o L o o ==--
Greetings
ICQ:8303363
|
|
| Back to top |
|
 |
Charles Harrison Caudill Guest
|
Posted: Wed Dec 03, 2003 3:15 am Post subject: Re: Task Problem!!!! |
|
|
Ivo <ivo_malinkovski (AT) hotmail (DOT) com> wrote:
| Quote: | | Could someone please solve the following problem:
|
Here's your mistake. comp.lang.c is not here to solve your algorithmic
problems for you. This newsgroup is for the C language. To find a function
to do your file io, try comp.std.c.
--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | v' I'm just a normal guy
|
|
| Back to top |
|
 |
Simon Biber Guest
|
Posted: Wed Dec 03, 2003 3:52 am Post subject: Re: Task Problem!!!! |
|
|
"Ivo" <ivo_malinkovski (AT) hotmail (DOT) com> wrote:
| Quote: | Could someone please solve the following problem:
There is a given txt file like this (example):
00011100001000
00100011110100
00100000000010
00010000000001
00010000000010
00001000000100
00000111111000
We should read the txt file and...
The 1's make an outline (shape) - contour. This info should be placed
in
a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.
|
you want something that detects the border of ones and does not change
them? Hint -- investigate recursive algorithms.
Here's some untested code to give you the gist:
void fill(int area[C][R], int x, int y, int fromcolour, int tocolour)
{
if(area[x][y] == fromcolour)
{
area[x][y] = tocolour;
fill(area, x + 1, y, fromcolour, tocolour);
fill(area, x - 1, y, fromcolour, tocolour);
fill(area, x, y + 1, fromcolour, tocolour);
fill(area, x, y - 1, fromcolour, tocolour);
}
}
With an initial call like
fill(area, 3, 1, 0, 1);
where the initial x,y point is inside the shape you want coloured.
--
Simon.
|
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Wed Dec 03, 2003 5:08 am Post subject: Re: Task Problem!!!! |
|
|
On 3 Dec 2003 03:15:07 GMT, Charles Harrison Caudill
<kungfoo (AT) guadeloupe (DOT) cc.gatech.edu> wrote in comp.lang.c:
| Quote: | Ivo <ivo_malinkovski (AT) hotmail (DOT) com> wrote:
Could someone please solve the following problem:
Here's your mistake. comp.lang.c is not here to solve your algorithmic
problems for you. This newsgroup is for the C language. To find a function
to do your file io, try comp.std.c.
|
Don't redirect people to newsgroups when you do not know their topic.
This post would be totally off-topic in comp.std.c.
[posted & mailed]
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
|
|
| Back to top |
|
 |
Charles Harrison Caudill Guest
|
Posted: Wed Dec 03, 2003 10:42 pm Post subject: Re: Task Problem!!!! |
|
|
Jack Klein <jackklein (AT) spamcop (DOT) net> wrote:
| Quote: | On 3 Dec 2003 03:15:07 GMT, Charles Harrison Caudill
[email]kungfoo (AT) guadeloupe (DOT) cc.gatech.edu[/email]> wrote in comp.lang.c:
Ivo <ivo_malinkovski (AT) hotmail (DOT) com> wrote:
Could someone please solve the following problem:
Here's your mistake. comp.lang.c is not here to solve your algorithmic
problems for you. This newsgroup is for the C language. To find a function
to do your file io, try comp.std.c.
Don't redirect people to newsgroups when you do not know their topic.
|
Then comp.std.c should post a topic and/or a suggestion should not be taken as
law.
| Quote: | This post would be totally off-topic in comp.std.c.
|
agreed, see above.
[posted & mailed]
--
Harrison Caudill | .^ www.hypersphere.org
Computer Science & Physics Double Major | | Me*Me=1
Georgia Institute of Technology | v' I'm just a normal guy
|
|
| Back to top |
|
 |
Peter Shaggy Haywood Guest
|
Posted: Mon Dec 08, 2003 11:58 pm Post subject: Re: Task Problem!!!! |
|
|
Groovy hepcat Simon Biber was jivin' on Wed, 3 Dec 2003 14:52:59 +1100
in comp.lang.c.
Re: Task Problem!!!!'s a cool scene! Dig it!
| Quote: | "Ivo" <ivo_malinkovski (AT) hotmail (DOT) com> wrote:
Could someone please solve the following problem:
There is a given txt file like this (example):
00011100001000
00100011110100
00100000000010
00010000000001
00010000000010
00001000000100
00000111111000
We should read the txt file and...
The 1's make an outline (shape) - contour. This info should be placed
in
a matrix [rxc] elements. The Task is to fill this shape (to color) the
contour with 1's - inside. Such colored matrix should be printed.
you want something that detects the border of ones and does not change
them? Hint -- investigate recursive algorithms.
|
Nonsense. This is an iterative problem, not a recursive one.
| Quote: | Here's some untested code to give you the gist:
void fill(int area[C][R], int x, int y, int fromcolour, int tocolour)
{
if(area[x][y] == fromcolour)
{
area[x][y] = tocolour;
fill(area, x + 1, y, fromcolour, tocolour);
fill(area, x - 1, y, fromcolour, tocolour);
fill(area, x, y + 1, fromcolour, tocolour);
fill(area, x, y - 1, fromcolour, tocolour);
}
}
|
I suggest you test it. You may experience a stack overflow problem
(on stack based implementations), especially with a large region to be
filled. This recursive solution is very bad. A better way of doing
what you suggest is iterative, or at least a mix of iteration and
recursion, like the following:
#include <stdio.h>
static void xfill(unsigned char *rgn, size_t w, size_t h, size_t x,
size_t y, int clr, int bg_clr)
{
long start, end;
/* don't go off edge of region */
if(x >= w || y >= h)
return;
/* don't go over different colour */
if(rgn[y * w + x] != bg_clr)
return;
for(end = x; end < w && rgn[y * w + end] == bg_clr; end++)
rgn[y * w + end] = clr;
for(start = x - 1; start >= 0 && rgn[y * w + start] == bg_clr;
start--)
rgn[y * w + start] = clr;
/* recurse for < y */
if(y > 0)
for(start++, x = start; start < end; start++)
xfill(rgn, w, h, start, y - 1, clr, bg_clr);
/* recurse for > y */
if(y < h - 1)
for(start = x; start < end; start++)
xfill(rgn, w, h, start, y + 1, clr, bg_clr);
}
void fill(unsigned char *rgn, size_t w, size_t h, size_t x, size_t y,
int clr)
{
unsigned char bg_clr = rgn[y * w + x];
/* don't bother to fill same colour */
if(clr != bg_clr)
xfill(rgn, w, h, x, y, clr, bg_clr);
}
#define MAP_WIDTH 15
#define MAP_HEIGHT (sizeof map / sizeof *map)
#define X_Fill_COORD 3
#define Y_FILL_COORD 1
#define FILL_CHAR '2'
int main(void)
{
char map[][MAP_WIDTH] =
{
"00011100001000",
"00100011110100",
"00100000000010",
"00010000000001",
"00010000000010",
"00001000000100",
"00000111111000"
};
int i;
puts("Before the fill:");
for(i = 0; i < MAP_HEIGHT; i++)
{
puts(map[i]);
}
fill((char *)map, MAP_WIDTH, MAP_HEIGHT, X_Fill_COORD, Y_FILL_COORD,
FILL_CHAR);
puts("nAfter the fill:");
for(i = 0; i < MAP_HEIGHT; i++)
{
puts(map[i]);
}
return 0;
}
However, that may be overkill for the OP's purposes. As TroLoo
suggested, all he has to do is scan each line until he finds the
outline, then store the fill character until he again encounters the
outline, then continue scanning the rest of the line. This is a
completely iterative solution to the problem, and wouldn't require the
OP to first find a set of coordinates inside the outline (as the
solutions you and I posted do).
--
Dig the even newer still, yet more improved, sig!
http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
|
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|