Once there was a king named Akbar. He had a son named Shahjahan. For an unforgivable reason the king wanted him to leave the kingdom. Since he loved his son he decided his son would be banished in a new place. The prince became sad, but he followed his father's will. In the way he found that the place was a combination of land and water. Since he didn't know how to swim, he was only able to move on the land. He didn't know how many places might be his destination. So, he asked your help.
For simplicity, you can consider the place as a rectangular grid consisting of some cells. A cell can be a land or can contain water. Each time the prince can move to a new cell from his current position if they share a side.
Now write a program to find the number of cells (unit land) he could reach including the cell he was living.
Input
Input starts with an integer T (≤ 500), denoting the number of test cases.
Each case starts with a line containing two positive integers W and H; W and H are the numbers of cells in the x and ydirections, respectively. W and H are not more than 20.
There will be H more lines in the data set, each of which includes W characters. Each character represents the status of a cell as follows.
1) '.' - land
2) '#' - water
3) '@' - initial position of prince (appears exactly once in a dataset)
Output
For each case, print the case number and the number of cells he can reach from the initial position (including it).
Sample Input
4
6 9
....#.
.....#
......
......
......
......
......
#@...#
.#..#.
11 9
.#.........
.#.#######.
.#.#.....#.
.#.#.###.#.
.#.#..@#.#.
.#.#####.#.
.#.......#.
.#########.
...........
11 6
..#..#..#..
..#..#..#..
..#..#..###
..#..#..#@.
..#..#..#..
..#..#..#..
7 7
..#.#..
..#.#..
###.###
...@...
###.###
..#.#..
..#.#..
Sample Output
Case 1: 45
Case 2: 59
Case 3: 6
Case 4: 13
#include <iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
struct st
{
int x,y;
};
int n,m;
int x1,y1;
char mp[32][32];
int vis[32][32];
int dir[4][2]= {{1,0},{-1,0},{0,1},{0,-1}};
int dfs(int a,int b)
{
st s,p;
s.x=a;
s.y=b;
queue<st>q;
q.push(s);
int ans=1;
vis[s.x][s.y]=1;
while(!q.empty())
{
p=q.front();
q.pop();
for(int i=0; i<4; i++)
{
s.x=p.x+dir[i][0];
s.y=p.y+dir[i][1];
if(vis[s.x][s.y]||mp[s.x][s.y]=='#'||s.x<0||s.x>=n||s.y<0||s.y>=m)continue;
if(vis[s.x][s.y]==0&&mp[s.x][s.y]=='.')
{
ans=ans+1;
vis[s.x][s.y]=1;
q.push(s);
}
}
}
return ans;
}
int main()
{
int t;
int f=0;
scanf("%d",&t);
while(t--)
{
memset(vis,0,sizeof(vis));
scanf("%d%d",&m,&n);
for(int i=0; i<n; i++)
{
getchar();
for(int j=0; j<m; j++)
{
scanf("%c",&mp[i][j]);
if(mp[i][j]=='@')
{
x1=i;
y1=j;
}
}
}
f++;
int step=dfs(x1,y1);
printf("Case %d: %d\n",f,step);
}
return 0;
}