1644: [Usaco2007 oct]obstacle Course Obstacle Training Course

Source: Internet
Author: User

1644: [Usaco2007 oct]obstacle Course Obstacle Training session time Limit:5 Sec Memory limit:64 MB
submit:383 solved:196
[Submit] [Status] [Discuss] Description

Consider an n x N (1 <= n <= 100) square ranch with 1 squares. Some squares are not for cows to set foot on, they are labeled ' X '. For example:

. . B x.
. x x A.
.... x.
. X...
. . X. .

Bessie found herself exactly at point A, and she wanted to lick salt from the salt block at B. Slow and clumsy animals, such as cows, hate to turn. Still, of course they will turn when necessary. For a given ranch, please calculate the minimum number of turns from A to B. At the beginning, Bessie could make a face in either direction. Bessie knew that she must be able to arrive.

Input

Line 1th: An integer N row

2..N + 1: line i+1 has N characters ('. ', ' x ', ' A ', ' B '), indicating the state of each point.

Output

Line 1: An integer with a minimum number of turns.

Sample Input3
. XA
...
Bx.
Sample Output2
HINT Source

Silver

The problem: (Hansbug: Too happy, this question I amuse the ha haha wnjxyk: sweat = =)

In fact, although this problem is also a bfs water problem, but compared with the general maze, this question has a pit--for a point first to traverse to is not necessarily optimal, in fact, even if the current optimal also because of the problem in the subject because of the direction, so the follow-up is not necessarily optimal, and if the simple irrigation method, The true optimal value cannot be updated because it has been traversed.

For example, the 5th point in the data:

33
........ xa.....x.......x .....
... x...........x.........x ....
........... x.xx.....x.x .....
....... x.....x.........x .....
..... x................x.x .....
... x.. X....x.........xx .....
................ x......x ...
... x.....x....................x.
...... x..........................x.
X................................
.................................
..... x...x. X....................
................. ........
.............. x.........x ...
........... x...x....x...x ....
...... xx.......x. X..........
............ x----------
................... xx. xxx, .....
.. x...............x.x .......
. x....x...............x....x....x
.. X......xx.......x.x.......x ....
........... x...xx.....x.x. X
. x.............x........x...x.x.
... x.x............. .......
................. x......x
..... x.......x............xx...xx
..... x....x.....xx.x.....xx.x.
....... x...x........x.x ... Xxb
. x.x.x.............x...........x.
X.............x................x.
....... x.x.....x.....x.....x ...
..... x..............xx.....x.
......... xx......x.x.....x.

(2,10) position the optimal value is obviously 1, and no matter how the traversal is 1

But (3,10) because it involves the direction of the origin (2,10), it is possible to make a 2 optimal solution, but in fact the positive solution is 1

That's all I have to say.

1/**************************************************************2Problem:16443 User:hansbug4 language:pascal5 result:accepted6Time: +Ms7Memory:1728KB8****************************************************************/9  Ten ConstDd:Array[1..4,1..2] ofLongint= ((1,0),(-1,0),(0,1),(0,-1)); One var A I,j,k,l,m,n,x0,y0,x1,y1,f,r,x,y:longint; -B:Array[0..205,0..205] ofLongint; -A:Array[0..205,0..205] ofLongint; theD:Array[0..100000,0..2] ofLongint; - Ch:char; - functionmin (x,y:longint): Longint; -          begin +               ifX<y ThenMin:=xElsemin:=y; -          End; + functionMax (x,y:longint): Longint; A          begin at               ifX>y ThenMax:=xElsemax:=y; -          End; -   - begin - READLN (n); -Fillchar (b,sizeof (b),0); inFillchar (A,sizeof (a),0); -       fori:=0  ton+1  Do to          begin +b[0, i]:=1; -b[n+1, i]:=1; theB[i,0]:=1; *b[i,n+1]:=1; $          End;Panax Notoginseng       fori:=1  toN Do -          begin the                forj:=1  toN Do +                   begin A read (CH); the                         CaseUpCase (CH) of +                             '.': b[i,j]:=0; -                             'A':begin $x0:=i;y0:=J; $b[i,j]:=1; -                             End; -                             'B':begin thex1:=i;y1:=J; -b[i,j]:=0;Wuyi                             End; the                             'X': b[i,j]:=1; -                        End; Wu                   End; - Readln; About          End; $f:=1; r:=2;d [1,1]:=x0;d[1,2]:=y0; -a[x0,y0]:=1; a[x0,y0]:=1; a[x0,y0]:=1; a[x0,y0]:=1; -       whileF<r Do -            begin A                  fori:=1  to 4  Do +                     begin theX:=d[f,1];y:=d[f,2]; -                           whileTrue Do $                                begin theX:=x+dd[i,1]; theY:=y+dd[i,2]; the                                     ifb[x,y]=1  ThenBreak ; theL:=a[d[f,1],d[f,2]]+1; -                                     if(L>=a[x,y]) and(a[x,y]>0) Thencontinue; ina[x,y]:=l; the                                     if(X=X1) and(y=y1) Then the                                        begin AboutWriteln (a[x,y]-2); the Readln; the Halt; the                                        End; +D[r,1]:=x;d[r,2]:=y; - Inc (R); the                                End;Bayi   the                     End; the Inc (f); -            End; - End.

1644: [Usaco2007 oct]obstacle Course Obstacle Training Course

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.