pku acm 1915,这个代码错在哪里?谢谢
#include<iostream>#include<vector>#include<cstring>usingnamespacestd;intx0,y0,x1,y1,l...
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
int x0,y0,x1,y1,length,in,n;
struct point{
int x;
int y;
}temp;
vector<point> vec;
int fnd;
int dix[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-2};
int map[301][301];
void deal(int x,int y,int time)
{
int tempx,tempy;
if(x==x1&&y==y1) {fnd=1;return;}
for(int i=0;i<8;i++)
{
tempx=x+dix[i][0];tempy=y+dix[i][1];//cout<<tempx<<' '<<tempy<<endl;
if(tempx>=0&&tempx<length&&tempy>=0&&tempy<length&&map[tempx][tempy]==-1) {temp.x=tempx;temp.y=tempy;vec.push_back(temp);map[tempx][tempy]=time+1;}
}
}
int main()
{
cin>>n;
while(n--)
{
cin>>length>>x0>>y0>>x1>>y1;
vec.clear();
//cout<<length<<" "<<x0<<" "<<y0<<" "<<x1<<" "<<y1;
temp.x=x0;temp.y=y0;
//cout<<temp.x<<" "<<temp.y<<endl;
in=0;
fnd=0;
vec.push_back(temp);
memset(map,-1,sizeof(map));
map[x0][y0]=0;
while(in<vec.size()&&fnd!=1)
{
deal(vec[in].x,vec[in].y,map[vec[in].x][vec[in].y]);
in++;
}
cout<<map[x1][y1]<<endl;
//cout<<vec.size()<<endl;
}
return 0;
} 展开
#include <vector>
#include <cstring>
using namespace std;
int x0,y0,x1,y1,length,in,n;
struct point{
int x;
int y;
}temp;
vector<point> vec;
int fnd;
int dix[8][2]={1,2,1,-2,-1,2,-1,-2,2,1,2,-1,-2,1,-2,-2};
int map[301][301];
void deal(int x,int y,int time)
{
int tempx,tempy;
if(x==x1&&y==y1) {fnd=1;return;}
for(int i=0;i<8;i++)
{
tempx=x+dix[i][0];tempy=y+dix[i][1];//cout<<tempx<<' '<<tempy<<endl;
if(tempx>=0&&tempx<length&&tempy>=0&&tempy<length&&map[tempx][tempy]==-1) {temp.x=tempx;temp.y=tempy;vec.push_back(temp);map[tempx][tempy]=time+1;}
}
}
int main()
{
cin>>n;
while(n--)
{
cin>>length>>x0>>y0>>x1>>y1;
vec.clear();
//cout<<length<<" "<<x0<<" "<<y0<<" "<<x1<<" "<<y1;
temp.x=x0;temp.y=y0;
//cout<<temp.x<<" "<<temp.y<<endl;
in=0;
fnd=0;
vec.push_back(temp);
memset(map,-1,sizeof(map));
map[x0][y0]=0;
while(in<vec.size()&&fnd!=1)
{
deal(vec[in].x,vec[in].y,map[vec[in].x][vec[in].y]);
in++;
}
cout<<map[x1][y1]<<endl;
//cout<<vec.size()<<endl;
}
return 0;
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询