Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Walker coordinate system
01-07-2010, 04:52 AM
Post: #1
Walker coordinate system
One little Boy likes to play chess. However, one day he got bored with ordinary chess and decided to make a new game to play on a chess board.

After a long time, the Boy has finally made a game and he wants you to play with him.

The rules are as follows: The chess board, where the game is played, is infinitely large. In the game you use only one man - a pawn. The game lasts N+1 seconds. At the begging the pawn is standing at the position (0, 0) for one second, and after that Boy says where the pawn has to go every second, and that is:
'up' - If the pawn has to move up one position
'down' - If the pawn has to move down one position
'left' - If the pawn has to move left one position
'right' - If the pawn has to move right one position
'stay' - If the pawn stays at the same position for one more second

After that you need to determine which position the pawn spent the greatest amount of time on. It may be possible to have more than one position that answers this question, but Boy only wants you to tell him how long the pawn spent at the position.

Input
The first line of the standard input contains one number N representing the number of commands Boy says. Remember that the game lasts N+1 seconds because the pawn stays at the position (0, 0) before Boy's first command, and after each command the pawn stays at that position for exactly one second.

Output
To the standard output write one number that represents maximum time spent at one position.

Sample Input
5
left
up
left
right
right

Sample Output
2

Explanation

On interval 0..1 the pawn is at the position (0, 0), after that Mr. Little Z is giving the commands:
On interval 1..2 the pawn is at the position (-1, 0)
On interval 2..3 the pawn is at the position (-1, 1)
On interval 3..4 the pawn is at the position (-2, 1)
On interval 4..5 the pawn is at the position (-1, 1)
On interval 5..6 the pawn is at the position (0, 1), and the game is over.

And the pawn spent 2 seconds at the position (-1, 1)

Solution
It is pretty simple i just stored everything into array of points (x,y) and at the end found the point in which pawn was the most of the time.

So here is the code in TPW :
Code:
program walker;
uses wincrt;
type
  tpoint=record
    x,y:integer;
  end;
var
a:array[1..100] of tpoint;
n,i,x,y,j,br,max:integer;
s:string;
begin
x:=0;
y:=0;
a[1].x:=x;
a[1].y:=y;
readln(n);
n:=n+1;
for i:=2 to n do
begin
  readln(s);
  if s='up' then
    begin
      y:=y+1;
      a[i].x:=x;
      a[i].y:=y;
    end;
  if s='down' then
    begin
      y:=y-1;
      a[i].x:=x;
      a[i].y:=y;
    end;
  if s='left' then
    begin
      x:=x-1;
      a[i].x:=x;
      a[i].y:=y;
    end;
  if s='right' then
    begin
      x:=x+1;
      a[i].x:=x;
      a[i].y:=y;
    end;
  if s='stay' then
    begin
      a[i].x:=x;
      a[i].y:=y;
    end;
    writeln(x,' ',y);
end;

br:=0;
max:=0;
for i:=1 to n do
begin
   br:=0;
  for j:=1 to n do
      if (a[i].x=a[j].x) and (a[i].y=a[j].y) then
        br:=br+1;
        writeln(i,' = ',br);
  if br>max then
    max:=br;
end;
writeln(max);      
end.

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: