/* ICPC Southeast USA Regional 2008 Problem: Teleport Author: Yiu Yu Ho Analysis: Clearly, if you ever walk some steps then teleport, it is not optimal by any means, because a teleportation is memoryless and renders your previous walking useless. Thus, if you have decided to walk, you will never teleport again. So, the only option is to teleport 0 or more times, until you arrive at some square, then walk from the square to exit. What will characterize the set of squares you want to start walking? It should be clear that there's really no difference between the square, except by the number of steps required (shortest path) to an exit. Thus, the correct strategy is clear: (1) Select k (2) Teleport until you arrive at a cell that requires at most k steps to exit the maze. (3) Walk to exit. The only parameter here is k - what is the optimal value of k such that the strategy produces optimal solution? Since the number of steps to leave is discrete, we can try all possible k from 0 to maxD, where maxD is the cell on the map with maximum number of steps to reach an exit. */ import java.io.*; import java.util.*; import java.text.*; public class teleport { public Scanner in=new Scanner(System.in); public PrintStream out=System.out; public int rn,cn; public char[][] M; public int[] count,sum; public int[][] D; public Pair me; public int maxD,total; public int oo=987654321; public DecimalFormat fmt=new DecimalFormat("0.000"); public void main() { int i,j,k; rn=in.nextInt(); while(rn>0) { cn=in.nextInt(); M=new char[rn][cn]; for(i=0;i0 } public void calD() { int i,j,k; ArrayList Q=new ArrayList(); for(i=0;iD[p.x][p.y]+1) { D[x][y]=D[p.x][p.y]+1; Q.add(new Pair(x,y)); } } } } public boolean valid(int x,int y) { return 0<=x && x