import java.io.* ;
import java.math.BigInteger ;
import java.util.* ;
public class Main{
public static final int mod = 1000003 ;
public static int r,c,s,dx[ ] = { 1 ,0 ,- 1 ,0 } ,dy[ ] = { 0 ,1 ,0 ,- 1 } ;
public static double a,b,ans;
public static boolean ap[ ] [ ] ;
public static double visited[ ] [ ] [ ] ;
// TODO Auto-generated method stub
int t
= Integer .
parseInt ( br.
readLine ( ) ) ;
for ( int o= 1 ; o<= t; o++ )
{
r
= Integer .
parseInt ( st.
nextToken ( ) ) ; c
= Integer .
parseInt ( st.
nextToken ( ) ) ; int rs
= Integer .
parseInt ( st.
nextToken ( ) ) ; int cs
= Integer .
parseInt ( st.
nextToken ( ) ) ; s
= Integer .
parseInt ( st.
nextToken ( ) ) ;
a
= Double .
parseDouble ( st.
nextToken ( ) ) ; b
= Double .
parseDouble ( st.
nextToken ( ) ) ;
ap = new boolean [ r] [ c] ;
for ( int i= 0 ; i< r; i++ )
{
for ( int j= 0 ; j< c; j++ )
if ( st.nextToken ( ) .equals ( "A" ) )
ap[ i] [ j] = true ;
}
visited = new double [ r] [ c] [ s+ 1 ] ;
ans = - 1 ;
int tt[ ] [ ] = new int [ r] [ c] ;
dfs( rs,cs,0 ,0 ,tt) ;
System .
out .
println ( "Case #" + o
+ ": " + ans
) ; }
}
static void dfs( int x,int y,double p, int steps,int t[ ] [ ] )
{
if ( steps == s)
{
return ;
}
for ( int i= 0 ; i< 4 ; i++ )
{
int xx = x+ dx[ i] ;
int yy = y+ dy[ i] ;
if ( xx< 0 || xx >= r || yy< 0 || yy>= c)
continue ;
double pt = - 1 ;
if ( t[ xx] [ yy] != 0 )
pt
= p
+ ( ap
[ xx
] [ yy
] ? a
: b
) * Math .
pow ( ap
[ xx
] [ yy
] ? ( 1 - a
) : ( 1 - b
) ,t
[ xx
] [ yy
] ) ; else
pt = p + ( ap[ xx] [ yy] ? a: b) ;
if ( visited[ xx] [ yy] [ steps+ 1 ] <= pt)
{
visited[ xx] [ yy] [ steps+ 1 ] = pt;
t[ xx] [ yy] ++;
dfs( xx,yy,pt,steps+ 1 ,t) ;
t[ xx] [ yy] --;
}
}
}
{
final private int BUFFER_SIZE = 1 << 16 ;
private byte [ ] buffer;
private int bufferPointer, bytesRead;
{
buffer = new byte [ BUFFER_SIZE] ;
bufferPointer = bytesRead = 0 ;
}
{
buffer = new byte [ BUFFER_SIZE] ;
bufferPointer = bytesRead = 0 ;
}
{
byte [ ] buf = new byte [ 64 ] ; // line length
int cnt = 0 , c;
while ( ( c = read( ) ) != - 1 )
{
if ( c == '\n ' )
break ;
buf[ cnt++ ] = ( byte ) c;
}
return new String ( buf,
0 , cnt
) ; }
{
int ret = 0 ;
byte c = read( ) ;
while ( c <= ' ' )
c = read( ) ;
boolean neg = ( c == '-' ) ;
if ( neg)
c = read( ) ;
do
{
ret = ret * 10 + c - '0' ;
} while ( ( c = read( ) ) >= '0' && c <= '9' ) ;
if ( neg)
return - ret;
return ret;
}
{
long ret = 0 ;
byte c = read( ) ;
while ( c <= ' ' )
c = read( ) ;
boolean neg = ( c == '-' ) ;
if ( neg)
c = read( ) ;
do {
ret = ret * 10 + c - '0' ;
}
while ( ( c = read( ) ) >= '0' && c <= '9' ) ;
if ( neg)
return - ret;
return ret;
}
{
double ret = 0 , div = 1 ;
byte c = read( ) ;
while ( c <= ' ' )
c = read( ) ;
boolean neg = ( c == '-' ) ;
if ( neg)
c = read( ) ;
do {
ret = ret * 10 + c - '0' ;
}
while ( ( c = read( ) ) >= '0' && c <= '9' ) ;
if ( c == '.' )
{
while ( ( c = read( ) ) >= '0' && c <= '9' )
{
ret += ( c - '0' ) / ( div *= 10 ) ;
}
}
if ( neg)
return - ret;
return ret;
}
{
bytesRead = din.read ( buffer, bufferPointer = 0 , BUFFER_SIZE) ;
if ( bytesRead == - 1 )
buffer[ 0 ] = - 1 ;
}
{
if ( bufferPointer == bytesRead)
fillBuffer( ) ;
return buffer[ bufferPointer++ ] ;
}
{
if ( din == null )
return ;
din.close ( ) ;
}
}
}
aW1w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yBmaWxlX25hbWUpIHRocm93cyBJT0V4Y2VwdGlvbgoJICAgICAgICB7CgkgICAgICAgICAgICBkaW4gPSBuZXcgRGF0YUlucHV0U3RyZWFtKG5ldyBGaWxlSW5wdXRTdHJlYW0oZmlsZV9uYW1lKSk7CgkgICAgICAgICAgICBidWZmZXIgPSBuZXcgYnl0ZVtCVUZGRVJfU0laRV07CgkgICAgICAgICAgICBidWZmZXJQb2ludGVyID0gYnl0ZXNSZWFkID0gMDsKCSAgICAgICAgfQoJIAoJICAgICAgICBwdWJsaWMgU3RyaW5nIHJlYWRMaW5lKCkgdGhyb3dzIElPRXhjZXB0aW9uCgkgICAgICAgIHsKCSAgICAgICAgICAgIGJ5dGVbXSBidWYgPSBuZXcgYnl0ZVs2NF07IC8vIGxpbmUgbGVuZ3RoCgkgICAgICAgICAgICBpbnQgY250ID0gMCwgYzsKCSAgICAgICAgICAgIHdoaWxlICgoYyA9IHJlYWQoKSkgIT0gLTEpCgkgICAgICAgICAgICB7CgkgICAgICAgICAgICAgICAgaWYgKGMgPT0gJ1xuJykKCSAgICAgICAgICAgICAgICAgICAgYnJlYWs7CgkgICAgICAgICAgICAgICAgYnVmW2NudCsrXSA9IChieXRlKSBjOwoJICAgICAgICAgICAgfQoJICAgICAgICAgICAgcmV0dXJuIG5ldyBTdHJpbmcoYnVmLCAwLCBjbnQpOwoJICAgICAgICB9CgkgCgkgICAgICAgIHB1YmxpYyBpbnQgbmV4dEludCgpIHRocm93cyBJT0V4Y2VwdGlvbgoJICAgICAgICB7CgkgICAgICAgICAgICBpbnQgcmV0ID0gMDsKCSAgICAgICAgICAgIGJ5dGUgYyA9IHJlYWQoKTsKCSAgICAgICAgICAgIHdoaWxlIChjIDw9ICcgJykKCSAgICAgICAgICAgICAgICBjID0gcmVhZCgpOwoJICAgICAgICAgICAgYm9vbGVhbiBuZWcgPSAoYyA9PSAnLScpOwoJICAgICAgICAgICAgaWYgKG5lZykKCSAgICAgICAgICAgICAgICBjID0gcmVhZCgpOwoJICAgICAgICAgICAgZG8KCSAgICAgICAgICAgIHsKCSAgICAgICAgICAgICAgICByZXQgPSByZXQgKiAxMCArIGMgLSAnMCc7CgkgICAgICAgICAgICB9ICB3aGlsZSAoKGMgPSByZWFkKCkpID49ICcwJyAmJiBjIDw9ICc5Jyk7CgkgCgkgICAgICAgICAgICBpZiAobmVnKQoJICAgICAgICAgICAgICAgIHJldHVybiAtcmV0OwoJICAgICAgICAgICAgcmV0dXJuIHJldDsKCSAgICAgICAgfQoJIAoJICAgICAgICBwdWJsaWMgbG9uZyBuZXh0TG9uZygpIHRocm93cyBJT0V4Y2VwdGlvbgoJICAgICAgICB7CgkgICAgICAgICAgICBsb25nIHJldCA9IDA7CgkgICAgICAgICAgICBieXRlIGMgPSByZWFkKCk7CgkgICAgICAgICAgICB3aGlsZSAoYyA8PSAnICcpCgkgICAgICAgICAgICAgICAgYyA9IHJlYWQoKTsKCSAgICAgICAgICAgIGJvb2xlYW4gbmVnID0gKGMgPT0gJy0nKTsKCSAgICAgICAgICAgIGlmIChuZWcpCgkgICAgICAgICAgICAgICAgYyA9IHJlYWQoKTsKCSAgICAgICAgICAgIGRvIHsKCSAgICAgICAgICAgICAgICByZXQgPSByZXQgKiAxMCArIGMgLSAnMCc7CgkgICAgICAgICAgICB9CgkgICAgICAgICAgICB3aGlsZSAoKGMgPSByZWFkKCkpID49ICcwJyAmJiBjIDw9ICc5Jyk7CgkgICAgICAgICAgICBpZiAobmVnKQoJICAgICAgICAgICAgICAgIHJldHVybiAtcmV0OwoJICAgICAgICAgICAgcmV0dXJuIHJldDsKCSAgICAgICAgfQoJIAoJICAgICAgICBwdWJsaWMgZG91YmxlIG5leHREb3VibGUoKSB0aHJvd3MgSU9FeGNlcHRpb24KCSAgICAgICAgewoJICAgICAgICAgICAgZG91YmxlIHJldCA9IDAsIGRpdiA9IDE7CgkgICAgICAgICAgICBieXRlIGMgPSByZWFkKCk7CgkgICAgICAgICAgICB3aGlsZSAoYyA8PSAnICcpCgkgICAgICAgICAgICAgICAgYyA9IHJlYWQoKTsKCSAgICAgICAgICAgIGJvb2xlYW4gbmVnID0gKGMgPT0gJy0nKTsKCSAgICAgICAgICAgIGlmIChuZWcpCgkgICAgICAgICAgICAgICAgYyA9IHJlYWQoKTsKCSAKCSAgICAgICAgICAgIGRvIHsKCSAgICAgICAgICAgICAgICByZXQgPSByZXQgKiAxMCArIGMgLSAnMCc7CgkgICAgICAgICAgICB9CgkgICAgICAgICAgICB3aGlsZSAoKGMgPSByZWFkKCkpID49ICcwJyAmJiBjIDw9ICc5Jyk7CgkgCgkgICAgICAgICAgICBpZiAoYyA9PSAnLicpCgkgICAgICAgICAgICB7CgkgICAgICAgICAgICAgICAgd2hpbGUgKChjID0gcmVhZCgpKSA+PSAnMCcgJiYgYyA8PSAnOScpCgkgICAgICAgICAgICAgICAgewoJICAgICAgICAgICAgICAgICAgICByZXQgKz0gKGMgLSAnMCcpIC8gKGRpdiAqPSAxMCk7CgkgICAgICAgICAgICAgICAgfQoJICAgICAgICAgICAgfQoJIAoJICAgICAgICAgICAgaWYgKG5lZykKCSAgICAgICAgICAgICAgICByZXR1cm4gLXJldDsKCSAgICAgICAgICAgIHJldHVybiByZXQ7CgkgICAgICAgIH0KCSAKCSAgICAgICAgcHJpdmF0ZSB2b2lkIGZpbGxCdWZmZXIoKSB0aHJvd3MgSU9FeGNlcHRpb24KCSAgICAgICAgewoJICAgICAgICAgICAgYnl0ZXNSZWFkID0gZGluLnJlYWQoYnVmZmVyLCBidWZmZXJQb2ludGVyID0gMCwgQlVGRkVSX1NJWkUpOwoJICAgICAgICAgICAgaWYgKGJ5dGVzUmVhZCA9PSAtMSkKCSAgICAgICAgICAgICAgICBidWZmZXJbMF0gPSAtMTsKCSAgICAgICAgfQoJIAoJICAgICAgICBwcml2YXRlIGJ5dGUgcmVhZCgpIHRocm93cyBJT0V4Y2VwdGlvbgoJICAgICAgICB7CgkgICAgICAgICAgICBpZiAoYnVmZmVyUG9pbnRlciA9PSBieXRlc1JlYWQpCgkgICAgICAgICAgICAgICAgZmlsbEJ1ZmZlcigpOwoJICAgICAgICAgICAgcmV0dXJuIGJ1ZmZlcltidWZmZXJQb2ludGVyKytdOwoJICAgICAgICB9CgkgCgkgICAgICAgIHB1YmxpYyB2b2lkIGNsb3NlKCkgdGhyb3dzIElPRXhjZXB0aW9uCgkgICAgICAgIHsKCSAgICAgICAgICAgIGlmIChkaW4gPT0gbnVsbCkKCSAgICAgICAgICAgICAgICByZXR1cm47CgkgICAgICAgICAgICBkaW4uY2xvc2UoKTsKCSAgICAgICAgfQoJICAgIH0JCgoKfQ==