Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > Program Design and Methods
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 06-20-2007, 06:41 AM   #1 (permalink)
p3tris
Recruit
 
Join Date: Jun 2007
Posts: 2
p3tris is on a distinguished road
Minimizing command string program

Hi i have been given this program as an assignement:

There is a robot, which accepts ONLY these 2 commands S to go straight one m
etre and R to turn left! The robot draws lines on the floor when it moves.

You are asked to write a program so that it will receive a string of command
s and give you the minimum string that will produce the same drawing!

*Assume that the robot always starts from the point (0,0) and the initial co
mmand string cannot be more than 1000 commands long.

i.e:
1) You are given the command string {s,s,r,r,s,s}.
The minimum command is {s,s}
2) You are given the command {r,r,r,r,r,r,r}
The minimum command is {}
3) You are given the command {r,r,r,r,r,r,s}
The minimum command is {r,r,s}

I've written a program that passes the whole "drawing" to a graph (an array
with the movements) and starts with a null string and continue adding comman
ds and testing all the possible combinations.
It works but it's not good towards it's complexity...

Any help for an algorythm or something appreciated!

Thanks
__________________
p3tris is offline   Reply With Quote
Old 06-20-2007, 10:03 AM   #2 (permalink)
DJMaze
Senior Contributor
 
DJMaze's Avatar
 
Join Date: Mar 2005
Posts: 632
DJMaze is on a distinguished road
the trick is to use points
{s} produces: {{0,0},{1,0}}
{r,s} produces: {{0,0},{0,1}}

an array is good but use a dynamic points array.

Code:
POINT points[] = { {0,0}, // start {2,0}, // s,s // r {2,2}, // s,s // r {0,2}, // s,s // r {0,0}, // s,s }
The main focus is on the 'r' which makes it turn.
So first find all double r's and substract s's from it, but this all depends on the whole string of commands.

Quote:
1) You are given the command string {s,s,r,r,s,s}.
The minimum command is {s,s}
what if the command is {s,s,r,r,s,s,s,s}?
There's no way you can shorten it because it results in the points.
{0,0},{2,0},{-2,0}
__________________

UT: Ultra-kill... God like!
DJMaze is offline   Reply With Quote
Old 06-20-2007, 10:44 AM   #3 (permalink)
p3tris
Recruit
 
Join Date: Jun 2007
Posts: 2
p3tris is on a distinguished road
Quote:
Originally Posted by DJMaze View Post
what if the command is {s,s,r,r,s,s,s,s}?
There's no way you can shorten it because it results in the points.
{0,0},{2,0},{-2,0}
How did you see that from the arrays? The problem is that the graph might be maybe 1000 commands. These many commands can result to a very complicated design (i tried giving out random commands and drawing them) In some of them you must get into a dead end and turn back or make a loop and deside the if you'll do it clock or anticlock-wise and many other tricks in order to get the minimum...

Can you explain me your thought a bit more?

Thanks.
__________________
p3tris is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help with copying backwards rogue Standard C, C++ 9 04-24-2005 04:39 PM
C++ Deadlock Detection Program Help... coolsc81 Standard C, C++ 2 10-26-2004 06:14 AM
Help on starting new program B00tleg Standard C, C++ 21 10-17-2004 12:58 PM
Need help on program B00tleg Standard C, C++ 1 10-12-2004 12:02 AM
Program Call to AS400 using JTOpen sde Java 0 05-12-2004 07:08 AM


All times are GMT -8. The time now is 05:00 AM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle