fork(1) download
  1. import java.io.*;
  2. import java.math.BigInteger;
  3. import java.util.*;
  4.  
  5.  
  6. public class Main{
  7. public static final int mod = 1000003;
  8. public static int r,c,s,dx[] = {1,0,-1,0},dy[] = {0,1,0,-1};
  9. public static double a,b,ans;
  10. public static boolean ap[][];
  11. public static double visited[][][];
  12. public static void main(String[] args)throws IOException {
  13. // TODO Auto-generated method stub
  14. InputStream input = System.in;
  15.  
  16.  
  17.  
  18. Reader in = new Reader();
  19.  
  20. int t = Integer.parseInt(br.readLine());
  21.  
  22. for(int o=1; o<=t; o++)
  23. {
  24. StringTokenizer st = new StringTokenizer(br.readLine());
  25.  
  26. r = Integer.parseInt(st.nextToken());
  27. c = Integer.parseInt(st.nextToken());
  28. int rs = Integer.parseInt(st.nextToken());
  29. int cs = Integer.parseInt(st.nextToken());
  30. s = Integer.parseInt(st.nextToken());
  31.  
  32. st = new StringTokenizer(br.readLine());
  33.  
  34. a = Double.parseDouble(st.nextToken());
  35. b = Double.parseDouble(st.nextToken());
  36.  
  37. ap = new boolean[r][c];
  38.  
  39. for(int i=0; i<r; i++)
  40. {
  41. st = new StringTokenizer(br.readLine());
  42. for(int j=0; j<c; j++)
  43. if(st.nextToken().equals("A"))
  44. ap[i][j] = true;
  45. }
  46.  
  47. visited = new double[r][c][s+1];
  48.  
  49. ans = -1;
  50.  
  51. int tt[][] = new int[r][c];
  52. dfs(rs,cs,0,0,tt);
  53.  
  54.  
  55.  
  56.  
  57. System.out.println("Case #"+o+": "+ans);
  58. }
  59. }
  60.  
  61. static void dfs(int x,int y,double p, int steps,int t[][])
  62. {
  63. if(steps == s)
  64. {
  65. ans = Math.max(ans,p);
  66. return;
  67. }
  68.  
  69. for(int i=0; i<4; i++)
  70. {
  71. int xx = x+dx[i];
  72. int yy = y+dy[i];
  73.  
  74. if(xx<0 || xx >=r || yy<0 || yy>=c)
  75. continue;
  76. double pt = -1;
  77.  
  78. if(t[xx][yy] != 0)
  79. pt = p + (ap[xx][yy]?a:b)*Math.pow(ap[xx][yy]?(1-a):(1-b),t[xx][yy]);
  80. else
  81. pt = p + (ap[xx][yy]?a:b);
  82.  
  83. if(visited[xx][yy][steps+1] <= pt)
  84. {
  85. visited[xx][yy][steps+1] = pt;
  86. t[xx][yy]++;
  87.  
  88.  
  89. dfs(xx,yy,pt,steps+1,t);
  90. t[xx][yy]--;
  91. }
  92.  
  93.  
  94. }
  95.  
  96.  
  97.  
  98.  
  99. }
  100.  
  101.  
  102.  
  103.  
  104. static class Reader
  105. {
  106. final private int BUFFER_SIZE = 1 << 16;
  107. private DataInputStream din;
  108. private byte[] buffer;
  109. private int bufferPointer, bytesRead;
  110.  
  111. public Reader()
  112. {
  113. din = new DataInputStream(System.in);
  114. buffer = new byte[BUFFER_SIZE];
  115. bufferPointer = bytesRead = 0;
  116. }
  117.  
  118. public Reader(String file_name) throws IOException
  119. {
  120. din = new DataInputStream(new FileInputStream(file_name));
  121. buffer = new byte[BUFFER_SIZE];
  122. bufferPointer = bytesRead = 0;
  123. }
  124.  
  125. public String readLine() throws IOException
  126. {
  127. byte[] buf = new byte[64]; // line length
  128. int cnt = 0, c;
  129. while ((c = read()) != -1)
  130. {
  131. if (c == '\n')
  132. break;
  133. buf[cnt++] = (byte) c;
  134. }
  135. return new String(buf, 0, cnt);
  136. }
  137.  
  138. public int nextInt() throws IOException
  139. {
  140. int ret = 0;
  141. byte c = read();
  142. while (c <= ' ')
  143. c = read();
  144. boolean neg = (c == '-');
  145. if (neg)
  146. c = read();
  147. do
  148. {
  149. ret = ret * 10 + c - '0';
  150. } while ((c = read()) >= '0' && c <= '9');
  151.  
  152. if (neg)
  153. return -ret;
  154. return ret;
  155. }
  156.  
  157. public long nextLong() throws IOException
  158. {
  159. long ret = 0;
  160. byte c = read();
  161. while (c <= ' ')
  162. c = read();
  163. boolean neg = (c == '-');
  164. if (neg)
  165. c = read();
  166. do {
  167. ret = ret * 10 + c - '0';
  168. }
  169. while ((c = read()) >= '0' && c <= '9');
  170. if (neg)
  171. return -ret;
  172. return ret;
  173. }
  174.  
  175. public double nextDouble() throws IOException
  176. {
  177. double ret = 0, div = 1;
  178. byte c = read();
  179. while (c <= ' ')
  180. c = read();
  181. boolean neg = (c == '-');
  182. if (neg)
  183. c = read();
  184.  
  185. do {
  186. ret = ret * 10 + c - '0';
  187. }
  188. while ((c = read()) >= '0' && c <= '9');
  189.  
  190. if (c == '.')
  191. {
  192. while ((c = read()) >= '0' && c <= '9')
  193. {
  194. ret += (c - '0') / (div *= 10);
  195. }
  196. }
  197.  
  198. if (neg)
  199. return -ret;
  200. return ret;
  201. }
  202.  
  203. private void fillBuffer() throws IOException
  204. {
  205. bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
  206. if (bytesRead == -1)
  207. buffer[0] = -1;
  208. }
  209.  
  210. private byte read() throws IOException
  211. {
  212. if (bufferPointer == bytesRead)
  213. fillBuffer();
  214. return buffer[bufferPointer++];
  215. }
  216.  
  217. public void close() throws IOException
  218. {
  219. if (din == null)
  220. return;
  221. din.close();
  222. }
  223. }
  224.  
  225.  
  226. }
Runtime error #stdin #stdout #stderr 0.05s 711168KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NumberFormatException: null
	at java.lang.Integer.parseInt(Integer.java:542)
	at java.lang.Integer.parseInt(Integer.java:615)
	at Main.main(Main.java:21)