| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
深度优先遍历#include<iostream>
using namespace std;
const int Max_N = 20;
int W, H;
char maze[Max_N][Max_N];
int sx, sy,res;
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };
void dfs(int x,int y)
{
res++;
maze[x][y] = '#';
for (int i = 0; i < 4; i++)
{
int nx = x + dx[i];
int ny = y + dy[i];
if (0 <= nx&&nx < H&& 0 <= ny&&ny < W&&maze[nx][ny] == '.')
{
dfs(nx, ny);
}
}
}
int main()
{
while (cin >> W >> H && W != 0 &&H != 0)
{
for (int i = 0; i < H; i++)
{
for (int j = 0; j < W; j++)
{
cin >> maze[i][j];
if (maze[i][j] == '@')
{
sx = i;
sy = j;
}
}
}
res = 0;
dfs(sx, sy);
cout << res << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator