{"trustable":true,"sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eYou are working for an amusement park as an operator of an \u003ci\u003eobakeyashiki\u003c/i\u003e, or a haunted house, in which guests walk through narrow and dark corridors. The house is proud of their lively ghosts, which are actually robots remotely controlled by the operator, hiding here and there in the corridors. One morning, you found that the ghosts are not in the positions where they are supposed to be. Ah, yesterday was Halloween. Believe or not, paranormal spirits have moved them around the corridors in the night. You have to move them into their right positions before guests come. Your manager is eager to know how long it takes to restore the ghosts.\u003c/p\u003e\u003cp\u003eIn this problem, you are asked to write a program that, given a floor map of a house, finds the smallest number of steps to move all ghosts to the positions where they are supposed to be.\u003c/p\u003e\u003cp\u003eA floor consists of a matrix of square cells. A cell is either a wall cell where ghosts cannot move into or a corridor cell where they can.\u003c/p\u003e\u003cp\u003eAt each step, you can move any number of ghosts simultaneously. Every ghost can either stay in the current cell, or move to one of the corridor cells in its 4-neighborhood (i.e. immediately left, right, up or down), if the ghosts satisfy the following conditions:\u003c/p\u003e\u003col\u003e\u003cli\u003e\u003cp\u003eNo more than one ghost occupies one position at the end of the step.\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003eNo pair of ghosts exchange their positions one another in the step.\u003c/p\u003e\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eFor example, suppose ghosts are located as shown in the following (partial) map, where a sharp sign (‘\u003ctt\u003e#\u003c/tt\u003e’) represents a wall cell and ‘\u003ctt\u003ea\u003c/tt\u003e’, ‘\u003ctt\u003eb\u003c/tt\u003e’, and ‘\u003ctt\u003ec\u003c/tt\u003e’ ghosts.\u003c/p\u003e\u003cdiv align\u003d\"center\"\u003e\u003cpre\u003e####\u003cbr\u003e ab#\u003cbr\u003e#c##\u003cbr\u003e####\u003c/pre\u003e\u003c/div\u003e\u003cp\u003eThe following four maps show the only possible positions of the ghosts after one step.\u003c/p\u003e\u003ctable align\u003d\"center\" border\u003d\"0\"\u003e\u003ctbody\u003e\u003ctr\u003e\u003ctd width\u003d\"16%\"\u003e\u003cpre\u003e####\u003cbr\u003e ab#\u003cbr\u003e#c##\u003cbr\u003e####\u003c/pre\u003e\u003c/td\u003e\u003ctd width\u003d\"12%\"\u003e\u003c/td\u003e\u003ctd width\u003d\"16%\"\u003e\u003cpre\u003e####\u003cbr\u003ea b#\u003cbr\u003e#c##\u003cbr\u003e####\u003c/pre\u003e\u003c/td\u003e\u003ctd width\u003d\"12%\"\u003e\u003c/td\u003e\u003ctd width\u003d\"16%\"\u003e\u003cpre\u003e####\u003cbr\u003eacb#\u003cbr\u003e# ##\u003cbr\u003e####\u003c/pre\u003e\u003c/td\u003e\u003ctd width\u003d\"12%\"\u003e\u003c/td\u003e\u003ctd width\u003d\"16%\"\u003e\u003cpre\u003e####\u003cbr\u003eab #\u003cbr\u003e#c##\u003cbr\u003e####\u003c/pre\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe input consists of at most 10 datasets, each of which represents a floor map of a house. The format of a dataset is as follows.\u003c/p\u003e\u003cdiv style\u003d\"padding-left: 2em;\"\u003e\u003ctable border\u003d\"0\"\u003e\u003ctbody\u003e\u003ctr\u003e\u003ctd\u003e\u003ci\u003ew\u003c/i\u003e\u003c/td\u003e\u003ctd\u003e\u003ci\u003eh\u003c/i\u003e\u003c/td\u003e\u003ctd\u003e\u003ci\u003en\u003c/i\u003e\u003c/td\u003e\u003ctd\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e11\u003c/sub\u003e\u003c/td\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e12\u003c/sub\u003e\u003c/td\u003e\u003ctd\u003e⋯\u003c/td\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e1\u003ci\u003ew\u003c/i\u003e\u003c/sub\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e21\u003c/sub\u003e\u003c/td\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e22\u003c/sub\u003e\u003c/td\u003e\u003ctd\u003e⋯\u003c/td\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e2\u003ci\u003ew\u003c/i\u003e\u003c/sub\u003e\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd align\u003d\"center\"\u003e⋮\u003c/td\u003e\u003ctd align\u003d\"center\"\u003e⋮\u003c/td\u003e\u003ctd\u003e⋱\u003c/td\u003e\u003ctd align\u003d\"center\"\u003e⋮\u003c/td\u003e\u003c/tr\u003e\u003ctr\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e\u003ci\u003eh\u003c/i\u003e1\u003c/sub\u003e\u003c/td\u003e\u003ctd\u003e\u003ci\u003ec\u003c/i\u003e\u003csub\u003e\u003ci\u003eh\u003c/i\u003e2\u003c/sub\u003e\u003c/td\u003e\u003ctd\u003e⋯\u003c/td\u003e\u003ctd\u003e\u003ci\u003ec\u003csub\u003ehw\u003c/sub\u003e\u003c/i\u003e\u003c/td\u003e\u003c/tr\u003e\u003c/tbody\u003e\u003c/table\u003e\u003c/div\u003e\u003cp\u003e\u003ci\u003ew\u003c/i\u003e, \u003ci\u003eh\u003c/i\u003e and \u003ci\u003en\u003c/i\u003e in the first line are integers, separated by a space. \u003ci\u003ew\u003c/i\u003e and \u003ci\u003eh\u003c/i\u003e are the floor width and height of the house, respectively. \u003ci\u003en\u003c/i\u003e is the number of ghosts. They satisfy the following constraints.\u003c/p\u003e\u003cp align\u003d\"center\"\u003e4 ≤ \u003ci\u003ew\u003c/i\u003e ≤ 16, 4 ≤ \u003ci\u003eh\u003c/i\u003e ≤ 16, 1 ≤ \u003ci\u003en\u003c/i\u003e ≤ 3\u003c/p\u003e\u003cp\u003eSubsequent \u003ci\u003eh\u003c/i\u003e lines of \u003ci\u003ew\u003c/i\u003e characters are the floor map. Each of \u003ci\u003ec\u003csub\u003eij\u003c/sub\u003e\u003c/i\u003e is either:\u003c/p\u003e\u003cul\u003e\u003cli\u003e\u003cp\u003ea ‘\u003ctt\u003e#\u003c/tt\u003e’ representing a wall cell,\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003ea lowercase letter representing a corridor cell which is the initial position of a ghost,\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003ean uppercase letter representing a corridor cell which is the position where the ghost corresponding to its lowercase letter is supposed to be, or\u003c/p\u003e\u003c/li\u003e\u003cli\u003e\u003cp\u003ea space representing a corridor cell that is none of the above.\u003c/p\u003e\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eIn each map, each of the first \u003ci\u003en\u003c/i\u003e letters from \u003ctt\u003ea\u003c/tt\u003e and the first \u003ci\u003en\u003c/i\u003e letters from \u003ctt\u003eA\u003c/tt\u003e appears once and only once. Outermost cells of a map are walls; i.e. all characters of the first and last lines are sharps; and the first and last characters on each line are also sharps. All corridor cells in a map are connected; i.e. given a corridor cell, you can reach any other corridor cell by following corridor cells in the 4-neighborhoods. Similarly, all wall cells are connected. Any 2 × 2 area on any map has at least one sharp. You can assume that every map has a sequence of moves of ghosts that restores all ghosts to the positions where they are supposed to be.\u003c/p\u003e\u003cp\u003eThe last dataset is followed by a line containing three zeros separated by a space.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003eFor each dataset in the input, one line containing the smallest number of steps to restore ghosts into the positions where they are supposed to be should be output. An output line should not contain extra characters such as spaces.\u003c/p\u003e"}},{"title":"Sample","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e5 5 2\r\n#####\r\n#A#B#\r\n# #\r\n#b#a#\r\n#####\r\n16 4 3\r\n################\r\n## ########## ##\r\n# ABCcba #\r\n################\r\n16 16 3\r\n################\r\n### ## # ##\r\n## # ## # c#\r\n# ## ########b#\r\n# ## # # # #\r\n# # ## # # ##\r\n## a# # # # #\r\n### ## #### ## #\r\n## # # # #\r\n# ##### # ## ##\r\n#### #B# # #\r\n## C# # ###\r\n# # # ####### #\r\n# ###### A## #\r\n# # ##\r\n################\r\n0 0 0\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e7\r\n36\r\n77\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}}]}