USACO MAR2011 bronze 我的程序

**********************************************************************
                           BRONZE PROBLEMS
**********************************************************************
                  Three problems numbered 11 through 13
**********************************************************************

Problem 11: Lucky Charms [Rob Kolstad, 2011]

Bessie has a lovely charm bracelet whose length is L (4 <= L <=
32,768) mm.  Hanging from this bracelet are C (1 <= C <= 512) charms,
each at a unique integer distance from the bracelet's left side.
Charm i dangles on the end of a string whose length is S_i mm (1
<= S_i <= 25) and which is located P_i (0 <= P_i <= L) mm from the
bracelet's left side.
Margaret snatches the bracelet from Bessie and nails it (with a
zero-width nail) to a fencepost. The nail is located N mm (1 <= N
<= L-1) from the left side of the bracelet, and the bracelet itself
thus hangs left and right of the nail, with gravity pulling the
bracelet and charms straight down.

Bessie is curious: How far is each charm from the nail in the fencepost?

By way of example, consider a bracelet of length 16 mm with three
charms. The schematic diagram below shows + signs which are each
separated by 1 mm and vertical bars which each represent 1mm of an
attached string. The charms are defined to be 4, 7, and 3 mm from
the bracelet.
    
                        1 1 1 1 1 1 1
    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
            |         |             |
            |         |             |
            |         |             *
            *         |
                      |
                      |
                      *

When the bracelet is nailed to the fencepost with the nail at
location 5, it droops like this (please ignore the left-right spread
which is shown for clarity):

Droop  Bracelet         Bracelet
dist.  location         location 
  0      5         +        5    <---- nail is here
  1      4        + +       6
  2      3      | + +       7
  3      2      | + +       8              D
  4      1      | + +       9              O
  5      0      * + + |    10              W
  6                 + |    11              N
  7                 + |    12              |
  8                 + |    13              |
  9                 + |    14              V
 10                 + |    15
 11                 + *    16
 12                     |
 13                     |
 14                     *

As you can see, the first charm droops down 5 mm from the nail; the
second charm droops to 11 mm and the third charm all the way down
to 14 mm from the nail.

Calculate the charm droop distance for each charm given.

PROBLEM NAME: charms

INPUT FORMAT:

* Line 1: Three space-separated integers: L, C, and N

* Lines 2..C+1: Line i+1 describes charm i with two space-separated
        integers: S_i and P_i

SAMPLE INPUT (file charms.in):

16 3 5
4 4
7 9
3 16

OUTPUT FORMAT:

* Lines 1..C: Line i contains the distance from charm i to the nail

SAMPLE OUTPUT (file charms.out):

5
11
14
1 {
2 PROG: charms
3 LANG: PASCAL
4 ID: lxyweb1
5 }
6 var
7   m,n,x,i,place,len:longint;
8 begin
9   assign(input,'charms.in'); reset(input);
10   assign(output,'charms.out'); rewrite(output);
11   readln(m,n,x);
12   for i:=1 to n do
13   begin
14     readln(len,place);
15     if place<=x then
16       writeln(x-place+len)
17     else writeln(place-x+len);
18   end;
19   close(input); close(output);
20 end.
简单的纯模拟
**********************************************************************

Problem 12: Pathfinding [Sherry Wu, 2011]

Bessie is stranded on a deserted arctic island and wants to determine
all the paths she might take to return to her pasture. She has
tested her boat and knows she can travel from one island to another
island in 1 unit of time if a route with proper currents connects
the pair.
She has experimented to create a map of the ocean with valid
single-hop routes between each pair of the N (1 <= N <= 100) islands,
conveniently numbered 1..N. The routes are one-way (unidirectional),
owing to the way the currents push her boat in the ocean. It's
possible that a pair of islands is connected by two routes that use
different currents and thus provide a bidirectional connection. The
map takes care to avoid specifying that a route exists between an
island and itself.

Given her starting location M (1 <= M <= N) and a representation
of the map, help Bessie determine which islands are one 'hop' away,
two 'hops' away, and so on. If Bessie can take multiple different
paths to an island, consider only the path with the shortest distance.

By way of example, below are N=4 islands with connectivity as shown
(for this example, M=1):

       start--> 1-------->2
                |         |
                |         |
                V         V
                4<--------3

Bessie can visit island 1 in time 0 (since M=1), islands 2 and 4
at time 1, and island 3 at time 2.

The input for this task is a matrix C where the element at row r,
column c is named C_rc (0 <= C_rc <= 1) and, if it has the value
1, means "Currents enable Bessie to travel directly from island r
to island c in one time unit". Row C_r has N elements, respectively
C_r1..C_rN, each one of which is 0 or 1.

PROBLEM NAME: pathfind

INPUT FORMAT:

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Line i+1 contains N space-separated integers: C_r

SAMPLE INPUT (file pathfind.in):

4 1
0 1 0 1
0 0 1 0
0 0 0 1
0 0 0 0

OUTPUT FORMAT:

* Lines 1..???: Line i+1 contains the list of islands (in ascending
        numerical order) that Bessie can visit at time i.  Do not
        include any lines of output after all reachable islands have
        been listed.

SAMPLE OUTPUT (file pathfind.out):

1
2 4
3
{
PROG: pathfind
LANG: PASCAL
ID: lxyweb1
}
var
  dist,q,num:array[0..100000] of longint;
  i,j,k,m,n,x:longint;
  head,tail,maxnum:longint;
  b:Array[0..200,0..200] of boolean;
  visit:array[0..200] of boolean;
  ans:array[0..200,0..200] of longint;
begin
  assign(input,'pathfind.in'); reset(input);
  assign(output,'pathfind.out'); rewrite(output);
  readln(n,m);
  fillchar(b,sizeof(b),0);
  for i:=1 to n do
    for j:=1 to n do
    begin
      read(x);
      if x=1 then b[i,j]:=true;
    end;
  fillchar(visit,sizeof(visit),0);
  head:=1;
  tail:=1;
  q[1]:=m;
  dist[m]:=0;
  visit[m]:=true;
  while head<=tail do
  begin
    for i:=1 to n do
      if (not visit[i])and b[q[head],i] then
      begin
        inc(tail);
        q[tail]:=i;
        dist[i]:=dist[q[head]]+1;
        visit[i]:=true;
      end;
    inc(head);
  end;
  for i:=1 to n do
  if (i=m)or(dist[i]<>0) then  
  begin
    inc(num[dist[i]]);
    ans[dist[i],num[dist[i]]]:=i;
    if dist[i]>maxnum then
      maxnum:=dist[i];
  end;
  for i:=0 to maxnum do
  begin
    for j:=1 to num[i]-1 do
      write(ans[i,j],' ');
    if num[i]<>0 then
      writeln(ans[i,num[i]])
    else writeln;
  end;
  close(input); close(output);
end.
 
有向无权图的最短路,BFS即可,输出有点麻烦
**********************************************************************

Problem 13: A spiral walk [Traditional, 2011]

Oh how the cows love to walk in their square pasture with sides of
length N (1 <= N <= 750) and which is partitioned into N*N squares.
They enjoy the sights, the smells, and the general ambience of the
grass and trees.

Bessie has decided to take the cows on the longest possible walk
from the upper left corner to the center (or near the center when
N is even) of the pasture, passing through each and every square
along the way after starting.

She has decided to create the obvious clockwise spiral route (example
below) for this evening's stroll. Write a program to create a map
for her that shows the order of squares she should visit.

By way of example, for pastures of size N=3 and N=4, here are the
routes Bessie should use:

      1  2  3        1  2  3  4
      8  9  4       12 13 14  5
      7  6  5       11 16 15  6
                    10  9  8  7

PROBLEM NAME: spiral

INPUT FORMAT:

* Line 1: A single integer: N

SAMPLE INPUT (file spiral.in):

3

OUTPUT FORMAT:

* Lines 1..N: N space-separated integers

SAMPLE OUTPUT (file spiral.out):

1 2 3
8 9 4
7 6 5
{
PROG: spiral
LANG: PASCAL
ID: lxyweb1
}
 
var
  n,add,now,i,j,k,x,y:longint;
  a:array[0..1000,0..1000] of longint;
 
 
begin
  assign(input,'spiral.in'); reset(input);
  assign(output,'spiral.out'); rewrite(output);
  readln(n);
  now:=1;
  x:=1;
  y:=0;
  k:=n;
  add:=1;
  while now<=n*n do
  begin
    for i:=now to now+k-1 do
    begin
      y:=y+add;
      a[x,y]:=i;
    end;
    now:=now+k;
    dec(k);
    for i:=now to now+k-1 do
    begin
      x:=x+add;
      a[x,y]:=i;
    end;
    now:=now+k;
    add:=-add;
  end;
  for i:=1 to n do
  begin
    for j:=1 to n-1 do
      write(a[i,j],' ');
    writeln(a[i,n]);
  end;
  close(input); close(output);
end.
 
也是模拟,按规格填充一个矩阵

**********************************************************************
 
 总结:USACO的铜组很水,我太粗心了,导致第二题得了30分,失去了晋升sliver的一次机会。
 

posted on 2011-03-27 13:15  oa414  阅读(859)  评论(0编辑  收藏  举报

导航