Polish version    English version  
  History of OI -> VI OI 1998/1999 -> Problems


 News
 About Olympic
 History of OI
XVII OI 2009/2010
XVI OI 2008/2009
XV OI 2007/2008
XIV OI 2006/2007
XIII OI 2005/2006
XII OI 2004/2005
XI OI 2003/2004
X OI 2002/2003
IX OI 2001/2002
VIII OI 2000/2001
VII OI 1999/2000
VI OI 1998/1999
Stage III - results
Stage II - results
Stage I - results
Problems
Regulations
Organization
Hints
Schedule
Stats
V OI 1997/1998
IV OI 1996/1997
III OI 1995/1996
II OI 1994/1995
I OI 1993/1994
 OI books
 National team
 Olympic camps
 Photo gallery
 Links
 SIO
 MAIN
Niebieskie ksi.eczki
VI Olimpiada Informatyczna 1998/1999

Task: MAG
Author:
Store-keeper

III stage contest  

The floor of a store is a rectangle divided into n*m square fields. Two fields are adjacent, if they have a common side. A parcel lays on one of the fields. Each of the remaining fields is either empty, or occupied by a case, which is too heavy to be moved by a store-keeper. The store-keeper has to shift the parcel from the starting field P to the final field K. He can move on the empty fields, going from the field on which he stands to a chosen adjacent field. When the store-keeper stays on a field adjacent to the one with the parcel he may push the parcel so that if moves to the next field (i.e. the field on the other side of the parcel), assuming condition that there are no cases on this field.

Task

Write a program, which:

  • reads from the text file MAG.IN a store scheme, a starting position of the store-keeper and a final position of the parcel,
  • computes minimal number of the parcel shifts through borders of fields, which are necessary to put the parcel in the final position or decides that it is impossible to put the parcel there,
  • writes the result into the text file MAG.OUT.

Input

In the first line of the input file MAG.IN two positive integers separated by a single space, n,m<=100, are written. These are dimensions of the store. In each of the following n lines there appears one m-letter word made of letters S, M, P, K, w. A letter on i-th position in j-th word denotes a type of the field with coordinates (i,j) and its meaning is following: 

  • S - case,
  • M - starting position of the store-keeper,
  • P - starting position of the parcel,
  • K - final position of the parcel,
  • w - empty field. 

Each letter M, P and K appears in the text file MAG.IN exactly once.

Output

Your program should write in the output file MAG.IN: 

  • exactly one word NIE (means "no" in Polish), if the parcel cannot be put on the target field,
  • exactly one integer, equal to the minimal number of the parcel shifts through borders of the fields, necessary to put a parcel on a final position, if it is possible to put the parcel there.

Example

For the input file MAG.IN:

10 12
SSSSSSSSSSSS
SwwwwwwwSSSS
SwSSSSwwSSSS
SwSSSSwwSKSS
SwSSSSwwSwSS
SwwwwwPwwwww
SSSSSSSwSwSw
SSSSSSMwSwww
SSSSSSSSSSSS
SSSSSSSSSSSS
the correct answer is the output file MAG.OUT
7



Print friendly version