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 fatfish at 2012-12-28 00:39:08 on Problem 3083
#include <stdio.h>
#include <string.h>

const int maxS = 46;
const int step[4][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
const int leftDs[4][4] = {{3, 0, 1, 2}, {0, 1, 2, 3}, {1, 2, 3, 0}, {2, 3, 0, 1}};
const int rightDs[4][4] = {{1, 0, 3, 2}, {2, 1, 0, 3}, {3, 2, 1, 0}, {0, 3, 2, 1}};
int w, h;
char maze[maxS][maxS];
struct Pox
{
	int x, y;
	int d; // 0 north, 1 east, 2 south, 3 west
	Pox() : x(-1), y(-1), d(0) {}
};
Pox src;

struct BfsPox
{
	int x, y, l;
	BfsPox() : x(-1), y(-1), l(0) {}
};
BfsPox bfsPoxs[maxS * maxS];
bool mark[maxS][maxS][4];
bool bfsMark[maxS][maxS];
int bfsRet;
int leftRet;
int rightRet;

void findSource(int index)
{
	if (src.x != -1)
	{
		return;
	}

	if (index == 0 || index == h - 1)
	{
		for (int j = 0; j < w; ++ j)
		{
			if (maze[index][j] == 'S')
			{
				src.x = index;
				src.y = j;
				src.d = (index == 0) ? 2 : 0;
				return;
			}
		}
	}

	if (maze[index][0] == 'S')
	{
		src.x = index;
		src.y = 0;
		src.d = 1;
		return;
	}

	if (maze[index][w - 1] == 'S')
	{
		src.x = index;
		src.y = w - 1;
		src.d = 3;
		return;
	}
}

void inputData()
{
	scanf("%d%d", &w, &h);
	for (int i = 0; i < h; ++ i)
	{
		scanf("%s", &maze[i]);
		findSource(i);
	}
}

bool dfsLeft(int x, int y, int d)
{
	++ leftRet;
	if (maze[x][y] == 'E')
	{
		return true;
	}

	for (int i = 0; i < 4; ++ i)
	{
		int newD = leftDs[d][i];
		int newX = x + step[newD][0];
		int newY = y + step[newD][1];

		if (newX < 0 || newX >= h || newY < 0 ||  newY >= w || maze[newX][newY] == '#' || mark[x][y][newD])
		{
			continue;
		}

		mark[x][y][newD] = true;
		if (dfsLeft(newX, newY, newD))
		{
			return true;
		}
	}
	return false;
}

bool dfsRight(int x, int y, int d)
{
	++ rightRet;
	if (maze[x][y] == 'E')
	{
		return true;
	}

	for (int i = 0; i < 4; ++ i)
	{
		int newD = rightDs[d][i];
		int newX = x + step[newD][0];
		int newY = y + step[newD][1];

		if (newX < 0 || newX >= h || newY < 0 ||  newY >= w || maze[newX][newY] == '#' || mark[x][y][newD])
		{
			continue;
		}

		mark[x][y][newD] = true;
		if (dfsRight(newX, newY, newD))
		{
			return true;
		}
	}
	return false;
}

void bfs()
{
	int len = 1;
	bfsPoxs[0].x = src.x;
	bfsPoxs[0].y = src.y;
	bfsPoxs[0].l = 1;
	bfsMark[src.x][src.y] = true;
	bfsRet = 1;
	for (int i = 0; i < len; ++ i)
	{
		for (int j = 0; j < 4; ++ j)
		{
			int newX = bfsPoxs[i].x + step[j][0];
			int newY = bfsPoxs[i].y + step[j][1];
			if (newX < 0 || newX >= h || newY < 0 ||  newY >= w || maze[newX][newY] == '#' || bfsMark[newX][newY])
			{
				continue;
			}

			if (maze[newX][newY] == 'E')
			{
				bfsRet = bfsPoxs[i].l + 1;
				return;
			}

			bfsMark[newX][newY] = true;
			bfsPoxs[len].x = newX;
			bfsPoxs[len].y = newY;
			bfsPoxs[len].l = bfsPoxs[i].l + 1;
			++ len;
		}
	}
}

void process()
{
	leftRet = 0;
	memset(mark, 0, sizeof(mark));
	dfsLeft(src.x, src.y, src.d);

	rightRet = 0;
	memset(mark, 0, sizeof(mark));
	dfsRight(src.x, src.y, src.d);

	bfsRet = 0;
	memset(bfsMark, 0, sizeof(bfsMark));
	bfs();
	printf("%d %d %d\n", leftRet, rightRet, bfsRet);

	src.x = -1;
	src.y = -1;
}

int main()
{
	int cases;
	scanf("%d", &cases);
	for (int i = 0; i < cases; ++ i)
	{
		inputData();
		process();
	}
	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