hdu 1240 BFS

简单的BFS啦。。。。。

AC代码如下:

#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;

typedef struct{
	int x, y, z;
	int step;
}Node;

int moves[6][3] = { 0, 0, 1,
					0, 0, -1,
					0, 1, 0,
					0, -1, 0,
					1, 0, 0,
					-1, 0, 0 };
int N;
bool mark[10][10][10];
int map[10][10][10];
Node start, endd;

int BFS(){
	queue<Node> q;
	start.step = 0;
	q.push( start );
	mark[start.z][start.y][start.x] = true;

	while( !q.empty() ){
		Node n = q.front();
		q.pop();
		
		if( n.x == endd.x && n.y == endd.y && n.z == endd.z ){
			return n.step;
		}

		for( int i = 0; i < 6; i++ ){
			Node temp = n;
			temp.x += moves[i][0];
			temp.y += moves[i][1];
			temp.z += moves[i][2];
			temp.step += 1;
			if( mark[temp.z][temp.y][temp.x] == true ){
				continue;
			}
			if( map[temp.z][temp.y][temp.x] == 1 ){
				continue;
			}
			if( temp.x < 0 || temp.x >= N || temp.y < 0 || temp.y >= N || temp.z < 0 || temp.z >= N ){
				continue;
			}
			mark[temp.z][temp.y][temp.x] = true;
			q.push( temp );
		}
	}
	return -1;
}

int main(){
	char s[20];
	while( scanf( "%s%d", s, &N ) != EOF ){
		for( int i = 0; i < N; i++ ){
			for( int j = 0; j < N; j++ ){
				scanf( "%s", s );
				for( int k = 0; k < N ; k++ ){
					if( s[k] == 'O' ){
						map[i][j][k] = 0;
					}else{
						map[i][j][k] = 1;
					}
				}
			}
		}
		cin >> start.x >> start.y >> start.z;
		cin >> endd.x >> endd.y >> endd.z;
		scanf( "%s", s );
		memset( mark, false, sizeof( mark ) );
		int ans = BFS();
		if( ans >= 0 ){
			cout << N << " " << ans << endl; 
		}else{
			cout << "NO ROUTE" << endl;
		}
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值