这题完全是根据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;
}