POJ 1915

时间:2023-03-09 21:29:48
POJ 1915
#include<iostream>
#include<stdio.h>
#define MAXN 350
#include"queue"
using namespace std;
bool mark[MAXN][MAXN];
struct point
{
int x;
int y;
int step;
point()
{
step = ;
}
};
bool bfs(point p);
point b;
int num;
point e;
point tem;
queue<point> coll;
int main()
{
//freopen("acm.acm","r",stdin);
int test;
int ans;
cin>>test;
while(test --)
{
//memset(road,0,sizeof(road));
memset(mark,false,sizeof(mark));
cin>>num;
cin>>b.x>>b.y;
cin>>e.x>>e.y;
if(b.x == e.x && b.y == e.y)
{
cout<<<<endl;
continue;
}
coll.push(b);
mark[b.x][b.y] = true;
// cout<<"----------"<<endl;
while(!coll.empty() && !bfs(coll.front()) )
{
//cout<<"-================"<<endl;
// cout<<coll.front().x<<endl;
// cout<<coll.front().y<<endl;
//break;
coll.pop();
}
while(!coll.empty())
{
coll.pop();
}
}
} bool bfs(point p)
{
if(p.x - >= )
{
if(p.y - >= && !mark[p.x - ][p.y - ])
{
tem.x = p.x - ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.y + < num && !mark[p.x - ][p.y + ])
{
tem.x = p.x - ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
}
////////////////
if(p.x + < num)
{
if(p.y - >= && !mark[p.x + ][p.y - ])
{
tem.x = p.x + ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.y + < num && !mark[p.x + ][p.y + ])
{
tem.x = p.x + ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
} ///////////////////
if(p.y - >= )
{
if(p.x - >= && !mark[p.x - ][p.y - ])
{
tem.x = p.x - ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.x + < num && !mark[p.x + ][p.y - ])
{
tem.x = p.x + ;
tem.y = p.y - ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
}
//////////////////////
if(p.y + < num)
{
if(p.x - >= && !mark[p.x - ][p.y + ])
{
tem.x = p.x - ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
if(p.x + < num && !mark[p.x + ][p.y + ])
{
tem.x = p.x + ;
tem.y = p.y + ;
tem.step = p.step + ;
mark[tem.x][tem.y] = true;
if(tem.x == e.x && tem.y == e.y)
{
cout<<tem.step<<endl;
return true;
}
else
coll.push(tem);
}
}
return false;
}

关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。

POJ 1915

技术网站地址: vmfor.com