现在的位置: 首页 > 综合 > 正文

hdu 1240 三维BFS

2013年08月19日 ⁄ 综合 ⁄ 共 1209字 ⁄ 字号 评论关闭

1A  0MS 注意坐标方向

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

typedef struct
{
	int x;
	int y;
	int z;
	int step;
}point;

int N;
char map[11][11][11];
bool visited[11][11][11];
point begin,target;
string start;
string end;
int minStep;

int dirX[6] = {0,0,1,-1,0,0};
int dirY[6] = {0,0,0,0,1,-1};
int dirZ[6] = {1,-1,0,0,0,0};

bool BFS()
{
	queue<point> Q;
	point p;
	p.x = begin.x;
	p.y = begin.y;
	p.z = begin.z;
	p.step = 0;
	visited[p.z][p.x][p.y] = 1;

	if(map[p.z][p.x][p.y] == 'X')
		return false;
	Q.push(p);
	while(!Q.empty())
	{
		p = Q.front();
		Q.pop();
		
		if(p.x == target.x && p.y == target.y && p.z == target.z)
		{
			minStep = p.step;
			return true;
		}
		
		for(int i = 0; i < 6; i++)
		{
			point q;
			q.x = p.x + dirX[i];
			q.y = p.y + dirY[i];
			q.z = p.z + dirZ[i];
			if(q.x < 0 || q.x >= N || q.y < 0 || q.y >= N || q.z < 0 || q.z >= N)
				continue;
			if(map[q.z][q.x][q.y] == 'O' && visited[q.z][q.x][q.y] == false)
			{
				visited[q.z][q.x][q.y] = true; 
				q.step = p.step + 1;
				Q.push(q);
			}
		}
		

	}
	return false;
	
}

int main()
{	
	while(cin>>start>>N)
	{
		for(int z = 0; z < N; z++)
		{
			for(int x = 0; x < N; x++)
			{
				cin>>map[z][x];
				/*
				for(int y = 0; z < N; z++)
				{
					cin>>map[z][x][y];
					visited[z][x][y] = false;
				}*/
			}
		}
		memset(visited,false,sizeof(visited));
		cin>>begin.x>>begin.y>>begin.z;
		cin>>target.x>>target.y>>target.z;
		cin>>end;
		minStep = 32765;
		bool res = BFS();
		if(res)
		{
			cout<<N<<" "<<minStep<<endl;
		}
		else
		{
			cout<<"NO ROUTE"<<endl;
		}
	}
	return 0;
}
【上篇】
【下篇】

抱歉!评论已关闭.