Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 23191 | Accepted: 12510 |
Description
Input
Output
Sample Input
6 9 ....#. .....# ...... ...... ...... ...... ...... #@...# .#..#. 11 9 .#......... .#.#######. .#.#.....#. .#.#.###.#. .#.#..@#.#. .#.#####.#. .#.......#. .#########. ........... 11 6 ..#..#..#.. ..#..#..#.. ..#..#..### ..#..#..#@. ..#..#..#.. ..#..#..#.. 7 7 ..#.#.. ..#.#.. ###.### ...@... ###.### ..#.#.. ..#.#.. 0 0
Sample Output
45 59 6 13
Source
#include<cstdio> #include<string> #include<iostream> #include<cstring> #include<algorithm> #include<queue> #include<stack> #include<cstdlib> using namespace std; char a[22][22]; int n, m, ans; void dfs ( int s1, int s2 ) { if ( s1 <= m - 1 && s2 <= n - 1 && a[s1][s2] == '.' && s1 >= 0 && s2 >= 0 ) { ans++; a[s1][s2] = '#'; } else return; dfs ( s1, s2 + 1 ); dfs ( s1, s2 - 1 ); dfs ( s1 + 1, s2 ); dfs ( s1 - 1, s2 ); } int main() { while ( cin >> n >> m && ( n + m ) ) { int s1, s2; for ( int i = 0; i < m; i++ ) for ( int j = 0; j < n; j++ ) { cin >> a[i][j]; if ( a[i][j] == '@' ) { s1 = i, s2 = j; a[i][j] = '.'; } } ans = 0; dfs ( s1, s2 ); printf ( "%d\n", ans ); } return 0; }
原文地址:http://blog.csdn.net/acm_baihuzi/article/details/41482597