zoj1438-Asteroids!

这题完全是根据zoj1940改了一点输入输出...代码没多少变化.

#include<iostream>
#include<queue>
#include<string>
#include<memory.h>
using namespace std;
char maze[11][11][11];
const int pmove[4][2]={{0,1},{0,-1},{-1,0},{1,0}};//移动
int z;//多少组
int n;//多少行
int m;//多少列
int sx,sy,ex,ey,sz,ez;
int Asteroids();
struct Node
{
	friend bool operator < (Node a,Node b)
	{
		return a.step>b.step;
	}
	int x,y,z,step;
};
int main()
{
	int i,j;
	string str;
	while(cin>>str)
	{
		cin>>n;
		m=z=n;
		memset(maze,'X',sizeof(maze));
		if(z==0&&n==0&&m==0)
			break;
		for(int icase=0;icase<z;icase++)
			for(i=0;i<n;i++)
				for(j=0;j<m;j++)
					cin>>maze[icase][i][j];
		cin>>sx>>sy>>sz>>ex>>ey>>ez>>str;
		int tmp;
		tmp=Asteroids();
		if(tmp!=-1)
			cout<<z<<' '<<tmp<<endl;
		else
			cout<<"NO ROUTE"<<endl;
	}
	return 0;
}
int Asteroids()
{
	priority_queue<Node> que;
	Node now,temp;
	int v;
	now.x=sx;
	now.y=sy;
	now.z=sz;
	now.step=0;
	que.push(now);
	maze[sz][sy][sx]='X';
	while(!que.empty())
	{
		temp=que.top();
		que.pop();
		if(temp.x==ex&&temp.y==ey&&temp.z==ez)
				return temp.step;
		for(int i=0;i<z;i++)//穿越
			{
				if(temp.z-1>=0)
					if(maze[temp.z-1][temp.y][temp.x]!='X')
					{
						now=temp;
						now.z--;
						now.step++;
						que.push(now);
						maze[temp.z][temp.y][temp.x]='X';
						maze[temp.z-1][temp.y][temp.x]='X';
					}
				if(temp.z+1<z)
					if(maze[temp.z+1][temp.y][temp.x]!='X')
					{
						now=temp;
						now.z++;
						now.step++;
						que.push(now);
						maze[temp.z+1][temp.y][temp.x]='X';
					}
			}
		for(v=0;v<4;v++)// 正常移动
		{
			for(int i=0;i<z;i++)//穿越
			{
				if(temp.z-1>=0)
					if(maze[temp.z-1][temp.y][temp.x]=='.')
					{
						now=temp;
						now.z--;
						now.step++;
						que.push(now);
						maze[temp.z][temp.y][temp.x]='X';
						maze[temp.z-1][temp.y][temp.x]='X';
					}
				if(temp.z+1<z)
					if(maze[temp.z+1][temp.y][temp.x]=='.')
					{
						now=temp;
						now.z++;
						now.step++;
						que.push(now);
						maze[temp.z+1][temp.y][temp.x]='X';
					}
			}
			now.x=temp.x+pmove[v][0];
			now.y=temp.y+pmove[v][1];
			now.z=temp.z;
			now.step=temp.step+1;
			if(now.x==ex&&now.y==ey&&temp.z==ez)
				return now.step;
			if(now.x<0||now.y<0||now.x>m||now.y>n)
				continue;
			if(maze[temp.z][now.y][now.x]!='X')
			{
				que.push(now);
				maze[temp.z][now.y][now.x]='X';
			}
		}
	}
	return -1;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值