1 题目编号:1016
2 题目内容:
6 9<br>....#.<br>.....#<br>......<br>......<br>......<br>......<br>......<br>#@...#<br>.#..#.<br>11 9<br>.#.........<br>.#.#######.<br>.#.#.....#.<br>.#.#.###.#.<br>.#.#..@#.#.<br>.#.#####.#.<br>.#.......#.<br>.#########.<br>...........<br>11 6<br>..#..#..#..<br>..#..#..#..<br>..#..#..###<br>..#..#..#@.<br>..#..#..#..<br>..#..#..#..<br>7 7<br>..#.#..<br>..#.#..<br>###.###<br>...@...<br>###.###<br>..#.#..<br>..#.#..<br>0 0<br>
45<br>59<br>6<br>13<br>
3 解题思路形成过程:一个人从@点出发,求他所能到达的'.'的数目,'#'不可走,@本身算1个点。比较经典的一个深搜题,首先遇到障碍或边界时退出,此时没
有通路,因此返回的道路条数是0;若可以继续走时,把走过的地方设置成障碍,防止再走一遍,这条路可以走完,然后从四个方向再继续走。
4 代码:
#include<iostream>
using namespace std;
char mapp[65][65];
int m, n, k = 0, c = 0, d = 0;
void dfs(int x, int y)
{
if (mapp[x][y] == '#' || x<0 || y<0 || x >= m || y >= n)
{
return;
}
mapp[x][y] = '#';
dfs(x + 1, y);
dfs(x - 1, y);
dfs(x, y + 1);
dfs(x, y - 1);
}
int main()
{
int i, j, k;
while (cin >> n >> m)
{
c = 0;
d = 0;
if (m == 0 && n == 0)
break;
for (i = 0; i<m; i++)
{
for (j = 0; j<n; j++)
{
cin >> mapp[i][j];
}
}
for (i = 0; i<m; i++)
for (j = 0; j<n; j++)
{
if (mapp[i][j] == '#')
c++;
}
k = 0;
for (i = 0; i<m; i++)
{
for (j = 0; j<n; j++)
{
if (mapp[i][j] == '@')
{
mapp[i][j] = '.';
dfs(i, j);
}
}
}
for (i = 0; i<m; i++)
for (j = 0; j<n; j++)
{
if (mapp[i][j] == '#')
d++;
}
cout << d - c << endl;
}
return 0;
}