/*
Problem link
Type: Graph
Algorithm: BFS/DFS
*/
import java.util.*;
import java.io.*;
public class Main
{
static char[][] a = new char[110][110];
static int[][] d = new int[110][110];
static int[] ans = new int[110*110];
static final int[] dx = {0,-1,-1,-1,0,1,1,1};
static final int[] dy = {-1,-1,0,1,1,1,0,-1};
static int n,m;
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int nTest;
nTest = in.nextInt();
in.nextLine();
in.nextLine();
for (int test=1; test<=nTest; test++)
{
if (test>1) out.println();
ReadFile(in,out);
}
out.close();
in.close();
}
static void ReadFile(Scanner in, PrintWriter out)
{
n = 0;
String line;
int cnt = 0;
while (in.hasNextLine())
{
line = in.nextLine();
if (line.isEmpty()) break;
if (line.charAt(0)=='L' || line.charAt(0)=='W')
{
m = line.length();
n++;
for (int i=0; i<m; i++) a[n][i+1] = line.charAt(i);
}
else
{
cnt++;
if (cnt==1) Solve();
int x = 0, y = 0;
for (int i=0; i<line.indexOf(' '); i++)
x = x*10 + (int)(line.charAt(i)) - 48;
for (int i=line.indexOf(' ')+1; i<line.length(); i++)
y = y*10 + (int)(line.charAt(i)) - 48;
out.println(ans[d[x][y]]);
}
}
}
static Boolean IsIn(Integer x, Integer y)
{
return (x>=1 && y>=1 && x<=n && y<=m);
}
static void DFS(Integer i, Integer j, Integer cnt)
{
d[i][j] = cnt;
for (int k=0; k<8; k++)
{
Integer ii = i+dx[k];
Integer jj = j+dy[k];
if (IsIn(ii,jj) && a[ii][jj]=='W' && d[ii][jj]!=cnt)
DFS(ii,jj,cnt);
}
}
static void Solve()
{
for (int i=0; i<110; i++) Arrays.fill(d[i], 0);
Arrays.fill(ans,0);
int cnt = 0;
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
if (a[i][j]=='W' && d[i][j]==0)
{
cnt++;
DFS(i,j,cnt);
}
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
if (d[i][j]!=0) ans[d[i][j]]++;
}
}
No comments:
Post a Comment