Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

深度优先遍历

Posted by 08153207 at 2017-07-13 21:30:20 on Problem 1979
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator