⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usaco 2_4_2 overfencing 题解_leokan的blog.mht

📁 美国USACO题库源程序
💻 MHT
📖 第 1 页 / 共 5 页
字号:
</DIV></DIV>
<DIV class=3Dstage>
<DIV class=3Dstagepad>
<DIV style=3D"WIDTH: 100%">
<TABLE class=3Dmodth cellSpacing=3D0 cellPadding=3D0 width=3D"100%" =
border=3D0>
  <TBODY>
  <TR>
    <TD class=3Dmodtl width=3D7>&nbsp;</TD>
    <TD class=3Dmodtc noWrap>
      <DIV class=3Dmodhead><SPAN =
class=3Dmodtit>=B2=E9=BF=B4=CE=C4=D5=C2</SPAN></DIV></TD>
    <TD class=3Dmodtc noWrap align=3Dright>
      <DIV class=3Dmodopt><A class=3Dmodact=20
      href=3D"http://hi.baidu.com/leokan/creat/blog/"><IMG=20
      src=3D"http://img.baidu.com/hi/img/ico_postnew.gif" =
align=3DabsMiddle=20
      border=3D0>=D0=B4=D0=C2=CE=C4=D5=C2</A></DIV></TD>
    <TD class=3Dmodtr width=3D7>&nbsp;</TD></TR></TBODY></TABLE>
<DIV class=3Dmodbox id=3Dm_blog>
<DIV class=3Dtit>USACO 2.4.2 Overfencing =CC=E2=BD=E2</DIV>
<DIV class=3Ddate>2008=C4=EA01=D4=C231=C8=D5 =D0=C7=C6=DA=CB=C4 =
09:46</DIV>
<TABLE style=3D"TABLE-LAYOUT: fixed">
  <TBODY>
  <TR>
    <TD>
      <DIV class=3Dcnt>
      <CENTER><STRONG><FONT =
size=3D7>Overfencing</FONT></STRONG><BR>Kolstad and=20
      Schrijvers </CENTER>
      <P>Farmer John went crazy and created a huge maze of fences out in =
a=20
      field. Happily, he left out two fence segments on the edges, and =
thus=20
      created two "exits" for the maze. Even more happily, the maze he =
created=20
      by this overfencing experience is a `perfect' maze: you can find a =
way out=20
      of the maze from any point inside it.</P>
      <P>Given W (1 &lt;=3D W &lt;=3D 38), the width of the maze; H (1 =
&lt;=3D H &lt;=3D=20
      100), the height of the maze; 2*H+1 lines with width 2*W+1 =
characters that=20
      represent the maze in a format like that shown later - then =
calculate the=20
      number of steps required to exit the maze from the `worst' point =
in the=20
      maze (the point that is `farther' from either exit even when =
walking=20
      optimally to the closest exit). Of course, cows walk only parallel =
or=20
      perpendicular to the x-y axes; they do not walk on a diagonal. =
Each move=20
      to a new square counts as a single unit of distance (including the =
move=20
      "out" of the maze.</P>
      <P>Here's what one particular W=3D5, H=3D3 maze looks =
like:</P><PRE>+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     | =20
+-+ +-+-+-+</PRE>
      <P>Fenceposts appear only in odd numbered rows and and odd =
numbered=20
      columns (as in the example). The format should be obvious and self =

      explanatory. Each maze has exactly two blank walls on the outside =
for=20
      exiting.</P>
      <H3>PROGRAM NAME: maze1</H3>
      <H3>INPUT FORMAT</H3>
      <TABLE border=3D1>
        <TBODY>
        <TR>
          <TD>Line 1:</TD>
          <TD>W and H, space separated</TD></TR>
        <TR>
          <TD>Lines 2 through 2*H+2:</TD>
          <TD>2*W+1 characters that represent the =
maze</TD></TR></TBODY></TABLE>
      <H3>SAMPLE INPUT (file maze1.in)</H3><PRE>5 3
+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     | =20
+-+ +-+-+-+</PRE>
      <H3>OUTPUT FORMAT</H3>A single integer on a single output line. =
The=20
      integer specifies the minimal number of steps that guarantee a cow =
can=20
      exit the maze from any possible point inside the maze.=20
      <H3>SAMPLE OUTPUT (file maze1.out)</H3>
      <P>9</P>
      <P>Overfencing<BR><BR>=B4=A9=D4=BD=D5=A4=C0=B8<BR>Kolstad and =
Schrijvers <BR><BR>=D2=EB by=20
      =
lyl<BR><BR>=C5=A9=B7=F2John=D4=DA=CD=E2=C3=E6=B5=C4=CC=EF=D2=B0=C9=CF=B4=EE=
=BD=A8=C1=CB=D2=BB=B8=F6=BE=DE=B4=F3=B5=C4=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=D0=A3=BA&nbsp;&nbsp; =
=C3=BF=D0=D02*W+1=B8=F6=D7=D6=B7=FB=B1=ED=CA=BE=C3=D4=B9=AC</P>
      <H3>SAMPLE INPUT (file maze1.in)</H3><PRE>5 3
+-+-+-+-+-+
|         |
+-+ +-+ + +
|     | | |
+ +-+-+ + +
| |     | =20
+-+ +-+-+-+</PRE>
      <H3>OUTPUT FORMAT</H3>
      =
<P>=CA=E4=B3=F6=D2=BB=B8=F6=B5=A5=B6=C0=B5=C4=D5=FB=CA=FD=A3=AC=B1=ED=CA=BE=
=C4=DC=B1=A3=D6=A4=C5=A3=B4=D3=C3=D4=B9=AC=D6=D0=C8=CE=D2=E2=D2=BB=B5=E3=D7=
=DF=B3=F6=C3=D4=B9=AC=B5=C4=D7=EE=D0=A1=B2=BD=CA=FD=A1=A3</P>
      <H3>SAMPLE OUTPUT (file maze1.out)</H3>
      <P>9</P>
      <P></P>
      <HR>

      <P></P>
      <P><STRONG>USACO 2.4.2 Overfencing =
<BR>=CC=E1=BD=BB=B4=CE=CA=FD:2=B4=CE</STRONG></P>
      <P><STRONG>=D3=C3bfs=C0=B4flood=20
      =
fill,=BD=AB=C8=EB=BF=DA=BC=D3=C8=EB=B6=D3=CA=D7,=D3=F6=B5=BD=C4=DC=D7=DF=C3=
=BB=D7=DF=B9=FD=B5=C4=B5=E3=D4=F2=BC=D3=C8=EB=B6=D3=CE=B2.=CE=D2=B6=D4=B6=
=D3=C1=D0=B5=C4=B3=A4=B6=C8=B9=C0=BC=C6=B5=C4=B2=BB=B9=BB...=D2=D4=BA=F3=D2=
=AA=C7=E5=B3=FE=B6=D3=C1=D0=B5=C4=D7=EE=B4=F3=B3=A4=B6=C8=B2=C5=BF=AA,=C3=
=BB=D3=D0=B0=D1=CE=D5=BE=CD=BF=AA=B4=F3=B5=E3,=CF=DE=B5=C3=CC=AB=CB=C0=C8=
=DD=D2=D7=B4=ED=CE=F3...</STRONG></P>
      <P><STRONG>{<BR>TASK:maze1<BR>LANG:PASCAL<BR>}<BR>program=20
      maze1;<BR>const<BR>&nbsp;&nbsp;&nbsp; next:array[1..4,1..2] of=20
      =
integer=3D((-1,0),(0,1),(1,0),(0,-1));<BR>type<BR>&nbsp;&nbsp;&nbsp;=20
      xy=3Darray[1..2] of integer;<BR>var<BR>&nbsp;&nbsp;&nbsp;=20
      map:array[0..203,0..79] of boolean;<BR>&nbsp;&nbsp;&nbsp;=20
      visit:array[0..203,0..79] of boolean;<BR>&nbsp;&nbsp;&nbsp;=20
      queue:array[1..10000] of xy;<BR>&nbsp;&nbsp;&nbsp; =
count:array[1..10000]=20
      of integer;<BR>&nbsp;&nbsp;&nbsp; n,m,h,t:integer;<BR>procedure=20
      init;<BR>var<BR>&nbsp;&nbsp;&nbsp; =
i,j:integer;<BR>&nbsp;&nbsp;&nbsp;=20
      ch:char;<BR>begin<BR>&nbsp;&nbsp;&nbsp;=20
      assign(input,'maze1.in');reset(input);<BR>&nbsp;&nbsp;&nbsp;=20
      readln(m,n);<BR>&nbsp;&nbsp;&nbsp; n:=3Dn shl 1 =
+1;<BR>&nbsp;&nbsp;&nbsp;=20
      m:=3Dm shl 1 +1;<BR>&nbsp;&nbsp;&nbsp; =
h:=3D1;t:=3D0;<BR>&nbsp;&nbsp;&nbsp;=20
      fillchar(map,sizeof(map),true);<BR>&nbsp;&nbsp;&nbsp; for i:=3D1 =
to n=20
      do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
      for j:=3D1 to m=20
      =
do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp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:=3D0;for j:=3D0 to m+1 do=20
      map[i,j]:=3Dfalse;<BR>&nbsp;&nbsp;&nbsp; i:=3Dn+1;for j:=3D0 to =
m+1 do=20
      map[i,j]:=3Dfalse;<BR>&nbsp;&nbsp;&nbsp; j:=3D0;for i:=3D0 to n+1 =
do=20
      map[i,j]:=3Dfalse;<BR>&nbsp;&nbsp;&nbsp; j:=3Dm+1;for i:=3D0 to =
n+1 do=20
      map[i,j]:=3Dfalse;<BR>&nbsp;&nbsp;&nbsp;=20
      fillchar(visit,sizeof(visit),false);<BR>&nbsp;&nbsp;&nbsp;=20
      fillchar(count,sizeof(count),0);<BR>&nbsp;&nbsp;&nbsp;=20
      close(input);<BR>end;<BR>procedure =
bfs;<BR>var<BR>&nbsp;&nbsp;&nbsp;=20
      i:integer;<BR>&nbsp;&nbsp;&nbsp; temp:xy;<BR>&nbsp;&nbsp;&nbsp;=20
      bo:boolean;<BR>begin<BR>&nbsp;&nbsp;&nbsp; while t&gt;=3Dh=20
      do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;=20
      for i:=3D1 to 4=20
      =
do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;=20
      =
begin<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
temp:=3Dqueue[h];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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sp;=20
      =
queue[t]:=3Dtemp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
count[t]:=3Dcount[h]+1;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;=20
      =
end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;=20
      =
end;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;=20
      inc(h);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=20
      end;<BR>end;<BR>procedure work;<BR>var<BR>&nbsp;&nbsp;&nbsp;=20
      i,max,j:integer;<BR>begin<BR>&nbsp;&nbsp;&nbsp;=20
      assign(output,'maze1.out');rewrite(output);<BR>&nbsp;&nbsp;&nbsp;=20
      bfs;<BR>&nbsp;&nbsp;&nbsp; max:=3D0;<BR>&nbsp;&nbsp;&nbsp; for =
i:=3D1 to t=20
      do<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if =
count[i]&gt;max then=20
      max:=3Dcount[i];<BR>&nbsp;&nbsp;&nbsp; writeln((max+1)shr=20
      1);<BR>&nbsp;&nbsp;&nbsp;=20
      close(output);<BR>end;<BR>begin<BR>&nbsp;&nbsp;&nbsp;=20
      init;<BR>&nbsp;&nbsp;&nbsp; work;<BR>end.</STRONG></P><STRONG>
      <HR>
      </STRONG>
      <P><STRONG>USACO=B5=C4=B7=D6=CE=F6,=B4=F3=B8=C5=D2=B2=CA=C7flood =
fill</STRONG></P>
      <P><STRONG>We can solve this with a standard flood fill, using a =
queue to=20
      implement breadth first search. It is convenient to leave the maze =
in its=20
      ASCII format and just look at it as a bunch of characters, with =
non-space=20
      characters being walls. </STRONG></P><PRE><STRONG>#include =
&lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
#include &lt;assert.h&gt;

#define MAXWID 38
#define MAXHT 100

typedef struct Point Point;
struct Point {
 int r, c;
};

int wid, ht;
char maze[MAXHT*2+1][MAXWID*2+1+2]; /* extra +2 for "\n\0" */
int dist[MAXHT*2+1][MAXWID*2+1];

Point
Pt(int r, int c)
{
    Point p;

    p.r =3D r;
    p.c =3D c;
    return p;
}

typedef struct Queue Queue;
struct Queue {
    Point p;
    int d;
};

Queue floodq[MAXHT*MAXWID];
int bq, eq;

/* if no wall between point p and point np, add np to queue with =
distance d+1 */
void
addqueue(int d, Point p, Point np)
{
    if(maze[(p.r+np.r)/2][(p.c+np.c)/2] =3D=3D ' ' &amp;&amp; =
maze[np.r][np.c] =3D=3D ' ') {
 maze[np.r][np.c] =3D '*';
 floodq[eq].p =3D np;
 floodq[eq].d =3D d+1;
 eq++;
    }
}

/* if there is an exit at point exitp, plug it and record a start point
 * at startp */

void
lookexit(Point exitp, Point startp)
{
    if(maze[exitp.r][exitp.c] =3D=3D ' ') {
 addqueue(0, startp, startp);
 maze[exitp.r][exitp.c] =3D '#';
    }
}

void
main(void)
{
    FILE *fin, *fout;
    Point p;
    int i, r, c, m, d;

    fin =3D fopen("maze1.in", "r");
    fout =3D fopen("maze1.out", "w");
    assert(fin !=3D NULL &amp;&amp; fout !=3D NULL);

    fscanf(fin, "%d %d\n", &amp;wid, &amp;ht);
    wid =3D 2*wid+1;
    ht =3D 2*ht+1;

    for(i=3D0; i&lt;ht; i++)
 fgets(maze[i], sizeof(maze[i]), fin);

    /* find exits */
    for(i=3D1; i&lt;wid; i+=3D2) {
 lookexit(Pt(0, i), Pt(1, i));
 lookexit(Pt(ht-1, i), Pt(ht-2, i));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -