水题,dfs
#include#include #include #include #include using namespace std;const int MAXN = 22;int W, H;char str[MAXN][MAXN], vis[MAXN][MAXN];int dir[4][2] = {1, 0, 0, 1, -1, 0, 0, -1};bool inMap(int x, int y){ return x >= 0 && x < H && y >= 0 && y < W;}void dfs(int x, int y){ vis[x][y] = 1; for(int i = 0;i < 4;i ++){ int xx = dir[i][0] + x, yy = dir[i][1] + y; if(inMap(xx, yy) && !vis[xx][yy] && str[xx][yy] == '.') dfs(xx, yy); }}int main(){ int t,x,y,CASE(0); scanf("%d", &t); while(t--){ scanf("%d%d", &W, &H); for(int i = 0;i < H;i ++){ scanf("%s", str[i]); for(int j = 0;j < W;j ++) { if(str[i][j] == '@') x = i, y = j; } } memset(vis, 0, sizeof vis); int ans = 0; dfs(x, y); for(int i = 0;i < H;i ++) for(int j = 0;j < W;j ++) ans += vis[i][j]; printf("Case %d: %d\n", ++CASE, ans); } return 0;}