典型迷宫。
#include<cstdio> #include <cstring> using namespace std; int m, n,cnt, count; char A[101][101]; int direction[4][2]={{1,0},{0,1},{0,-1},{-1,0}}; void dfs(int i, int j) { if(i < 0|| j < 0|| i >= m|| j >= n || (A[i][j] != '.' && A[i][j] != '@')) return; else { A[i][j] = '#'; cnt++; int di = i, dj = j; for(int q = 0; q < 4; ++q) dfs(di+ direction[q][0], dj+ direction[q][1]); } } int main() { while(~scanf("%d%d", &n, &m) && (m+n)) { cnt = 0; memset(A, 0, sizeof(A)); for(int i = 0; i < m; ++i) scanf("%s", A[i]); for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) if(A[i][j] == '@') { dfs(i, j); break; } printf("%d\n", cnt); } }