hdu 1240 Asteroids! (广搜)

/*

最简单广搜多了一维而已,杭电这题代码数据实在是太弱了,估计就一组,各种错误代码都过了。有兴趣的可以去提交北大poj2225

*/

#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#define N 22
#define max 999999
#define min(a, b) (a>b?b:a) 
using namespace std;
int n, mark[N][N][N], dir[6][3] = {1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1};
int s_x, s_y, s_z, e_x, e_y, e_z;
char str[N][N][N];

struct node
{
	int x, y, z, step;
};

int judge(int x, int y, int z)
{
	if(x>=0 && x<n && y>=0 && y<n && z>=0 &&z<n && mark[x][y][z]==0 && str[x][y][z]=='O')
		return 1;
	else
		return 0;
}

int bfs()
{
	int i, x, y, z;
	node cur, next;
	queue<node>q;
	cur.x=s_x; cur.y=s_y; cur.z=s_z; cur.step=0;
	q.push(cur);
	mark[s_x][s_y][s_z] = 1;
	int ans = max;
	while(!q.empty())
	{
		cur = q.front();
		q.pop();
		if(cur.x==e_x && cur.y==e_y && cur.z==e_z) 
		{
			ans = min(ans, cur.step);
		}
		for( i=0; i < 6; i++ )
		{
			next.x = x = cur.x + dir[i][0];
			next.y = y = cur.y + dir[i][1];
			next.z = z = cur.z + dir[i][2];
			next.step = cur.step + 1;
			if(judge(x, y, z))
			{
				mark[x][y][z] = 1;
				q.push(next);
			}
		}
	}
	if(ans>=max) return -1;
	else return ans;
}

int main()
{
	char ch[22];
	int i, j, k;
	while(scanf("%s%d", ch, &n) != -1)
	{
		//getchar();
		memset(mark, 0, sizeof(mark));
		for( i=0; i < n; i++ )
			for(j=0; j < n; j++ )
			{
				getchar();
				for(k=0; k < n; k++ )
					scanf("%c", &str[k][j][i]);
			}

		scanf("%d%d%d", &s_x, &s_y, &s_z);
		scanf("%d%d%d", &e_x, &e_y, &e_z);
		scanf("%s", ch);
		
		int ans = bfs();
		if(ans==-1)
			printf("NO ROUTE\n");
		else
			printf("%d %d\n", n, ans);
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值