|
5鱼币
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1438
求助一直答案错误 我找不到问题出在哪里
代码:
#include<iostream>
//#include "string.h" //cfree头文件
#include<string> //vc6.0 头文件
#include<queue>
using namespace std;
struct node
{
int x,y,z,steps; //x,y,z轴坐标、步数
};
int main()
{
string str;
int n;
char a[12][12][12]; //记录空间(多申请了一圈为了下面方便,在空间外围直接包上‘X’)
int f[6][3]={0,0,-1,0,0,1,-1,0,0,1,0,0,0,-1,0,0,1,0}; //
while(cin>>str>>n)
{
int i,j,k;
memset(a,'X',sizeof(a)); //初始化空间
for(k=1;k<=n;k++)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cin>>a[i][j][k]; //输入
}
}
}
int begin[3],end[3];
cin>>begin[0]>>begin[1]>>begin[2]; //输入起点坐标
cin>>end[0]>>end[1]>>end[2]; //输入终点坐标
cin>>str;
begin[0]++;begin[1]++;begin[2]++;end[0]++;end[1]++;end[2]++; //与设置空间同步
queue <node> mfc; //队列声明
node nodebegin;
nodebegin.x=begin[0];
nodebegin.y=begin[1];
nodebegin.z=begin[2];
nodebegin.steps=0;
a[begin[0]][begin[1]][begin[2]]='X';
mfc.push(nodebegin); //起点节点入栈
bool arr=false; //表示是否到达终点
int put=0; //最终结果
while(!mfc.empty())
{
node mc;
mc=mfc.front();
mfc.pop();
for(i=0;i<6;i++)
{
if(begin[0]==end[0]&&begin[1]==end[1]&&begin[2]==end[2]){arr=true;put=0;;break;} //起点就是终点时
int ti=f[i][0]+mc.x;
int tj=f[i][1]+mc.y;
int tz=f[i][2]+mc.z;
if(ti==end[0]&&tj==end[1]&&tz==end[2]&&a[end[0]][end[1]][end[2]]=='O'){arr=true;put=mc.steps+1;break;}//判断是否到达指定地点
if(a[ti][tj][tz]=='O')//判断是否是空间;
{
node mx;
mx.x=ti;
mx.y=tj;
mx.z=tz;
mx.steps=mc.steps+1;
mfc.push(mx);
a[ti][tj][tz]='X';
}
}
if(arr)break;
}
if(arr)cout<<n<<" "<<put<<endl;
else
cout<<"NO ROUTE"<<endl;
}
return 0;
}
|
最佳答案
查看完整内容
终于AC了,楼主起始坐标的输入顺序写错了
该为 cin>>begin[1]>>begin[0]>>begin[2]; //输入起点坐标
cin>>end[1]>>end[0]>>end[2]; //输入终点坐标
然后再加个memset的头文件 string.h 就可以了
|