import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static int W, H; static char C[][]; static int DP[][]; static int mAns = Integer.MAX_VALUE; public static void main(String[] args) { readMap(); boolean find = false; for(int i=0; i q = new LinkedList(); Node start = new Node(startX, startY, 0, C); q.add(start); while(!q.isEmpty()) { Node node = q.poll(); if(DP[node.mY][node.mX] <= node.mStep) continue; DP[node.mY][node.mX] = node.mStep; if(mAns <= node.mStep) continue; if(node.mX == 0 || node.mX == W-1) continue; if(node.mY == 0 || node.mY == H-1) continue; if(node.mMap[node.mY][node.mX] == '+') { mAns = Math.min(mAns, node.mStep); continue; } node.mMap[node.mY][node.mX] = '.'; int nextStep = node.mStep+1; int nextX, nextY; char nextMap[][] = node.mMap; //上 nextX = node.mX; nextY = node.mY-1; if(node.mMap[nextY][nextX] != '.') { Node next = new Node(nextX, nextY, nextStep, nextMap); q.add(next); } //右 nextX = node.mX+1; nextY = node.mY; if(node.mMap[nextY][nextX] != '.') { Node next = new Node(nextX, nextY, nextStep, nextMap); q.add(next); } //下 nextX = node.mX; nextY = node.mY+1; if(node.mMap[nextY][nextX] != '.') { Node next = new Node(nextX, nextY, nextStep, nextMap); q.add(next); } //左 nextX = node.mX-1; nextY = node.mY; if(node.mMap[nextY][nextX] != '.') { Node next = new Node(nextX, nextY, nextStep, nextMap); q.add(next); } } } static void readMap() { Scanner sc = new Scanner(System.in); W = sc.nextInt(); H = sc.nextInt(); C = new char[H][W]; DP = new int[H][W]; for(int i=0; i