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

BFS一次AC o(∩_∩)o 附代码

Posted by lsz at 2011-05-05 16:31:20 on Problem 1111 and last updated at 2011-05-05 16:33:41
#include <iostream>
#include <queue>
using namespace std;

int main()
{
	const int D = 8;
	const int D_half = D / 2;
	const int dy[D] = {-1, 1, 0, 0, -1, 1, -1, 1};
	const int dx[D] = {0, 0, -1, 1, -1, -1, 1, 1};
	const int N = 32;
	char grid[N][N];
	char visited[N][N];
	int row, column, mouse_row, mouse_column;

	while (cin>>row>>column>>mouse_row>>mouse_column && row != 0)
	{
		memset(grid, 0, sizeof(char) * N * N);
		memset(visited, 0, sizeof(char) * N * N);
		for (int i=1; i<=row; i++)
		{
			cin>>(&grid[i][1]);
		}

		if (grid[mouse_row][mouse_column] != 'X')
		{
			cout<<0<<endl;
			continue;
		}

		queue<pair<int,int>> q;
		q.push(pair<int,int>(mouse_row,mouse_column));
		visited[mouse_row][mouse_column] = 1;
		int perimeter = 0;

		while (!q.empty())
		{
			pair<int,int> p = q.front();
			q.pop();
			for (int i=0; i<D; i++)
			{
				int y = p.first + dy[i];
				int x = p.second + dx[i];
				if (y >= 1 && y <= row && x >= 1 && x <= column && grid[y][x] == 'X')
				{
					if (visited[y][x] == 0)
					{
						q.push(pair<int,int>(y, x));
						visited[y][x] = 1;
					}
				} 
				else
				{
					if (i < D_half)
					{
						perimeter++;
					}
				}
			}
		}

		cout<<perimeter<<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