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 应该没问题 只是要稍微注意下几个问题!

Posted by ohsohot at 2010-08-10 00:36:31 on Problem 1101 and last updated at 2010-08-10 00:45:54
BFS的时候 是一整行或者一整列 而不是一个点一个点
例如:
x2Txxxxxx
x2xxxxx2x
22212222x
x2x1xxx2x
x2x1xxx2x
111S1111x
xxx1xxx2x
xxx1xxx2x
xxxxxxx2x
xxxxxxxxx

第一次 BFS 进队列的时候 是S的一个十字叉上的所有元素 标记1的部分

第二次 BFS 出队列元素是1  进队列的元素是元素1左右的元素(标记有方向 前后的元素不管)  进队列后的元素标记是2  

最后2的出列 找到T(答案是3)

#include<iostream>
#include <queue>
using namespace std;

int w, h;
char game[100][100];
int add[4][2]={{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
int visit[101][101];
int sx, sy, tx, ty;
struct NODE
{
	int x, y, ans, d;
};
bool check(int x, int y)
{
	if (x > h+1 || x < 0 || y > w+1 || y < 0 )
		return false;
	return true;
}

int bfs(int sx, int sy, int tx, int ty)
{
	queue<NODE> q;
	NODE node;
	int x, y;
	for (int i = 0; i < 4; i++)
	{
		x = sx+add[i][0];
		y = sy+add[i][1];

		while (check(x, y))
		{
			if (x == tx && y == ty)
				return 1;
			if (game[x][y] != 'X')
			{
				visit[x][y] = 1;
				node.x = x;
				node.y = y;
				node.ans = 1;
				node	.d = i;
				q.push(node);
			}
			else
				break;
			x = x+add[i][0];
			y = y+add[i][1];
		}
	}

	while (!q.empty())
	{
		int _x = q.front().x;
		int _y = q.front().y;
		int _d = q.front().d;
		int _ans = q.front().ans;
		q.pop();

		int d = (_d+1)%4;
		x = _x+add[d][0];
		y = _y+add[d][1];
		while (check(x, y))
		{
			if (x == tx && y == ty)
				return _ans+1;
			if (game[x][y] == 'X')
				break;
			if (visit[x][y] == 0)
			{
				visit[x][y] = 1;
				node.x = x;
				node.y = y;
				node.ans = _ans+1;
				node	.d = d;
				q.push(node);
			}
			x = x+add[d][0];
			y = y+add[d][1];
		}

		d = (_d-1+4)%4;
		x = _x+add[d][0];
		y = _y+add[d][1];
		while (check(x, y))
		{
			if (x == tx && y == ty)
				return _ans+1;
			if (game[x][y] == 'X')
				break;
			if (visit[x][y] == 0)
			{
				visit[x][y] = 1;
				node.x = x;
				node.y = y;
				node.ans = _ans+1;
				node	.d = d;
				q.push(node);
			}
			x = x+add[d][0];
			y = y+add[d][1];
		}
	}
	return -1;
}

int main()
{
	freopen("test.txt", "r", stdin);
	int t = 1;
	int k = 1;
	while (true)
	{
		memset(game, 0, sizeof	(game));
		k = 1;
		cin>>w>>h;
		if (w+h == 0)
			break;
		char c;
		gets(&c);
		for (int i = 1; i <= h; i++)
			gets(game[i]+1);
		cout<<"Board #"<<t++<<":"<<endl;
		while (true)
		{
			memset(visit, 0, sizeof(visit));
			cin>>sx>>sy>>tx>>ty;
			if (sx+sy+tx+ty == 0)
				break;
			swap(sx, sy);
			swap(tx, ty);
			int ans = bfs(sx, sy, tx, ty);
			if (ans == -1)
				cout<<"Pair "<<k++<<": impossible."<<endl;
			else
				cout<<"Pair "<<k++<<": "<<ans<<" segments."<<endl;
		}
		cout<<endl;
	}
}

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