#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
class map
{
private:
bool **p,**vis;
int n,m;
inline bool isin(const int x1,const int y1,const int x2,const int y2,const int i,const int j)
{
return (((i>=((x1>x2)?x2:x1))&&(i<=((x1>x2)?x1:x2)))&&((j>=((y1>y2)?y2:y1))&&(j<=((y1>y2)?y1:y2))));
}
struct point
{
static int tar_x,tar_y;
int x,y,b_dis;
int f_dis() const
{
return abs(x-tar_x)+abs(y-tar_y);
}
bool operator<(const point b) const
{
return (b_dis+f_dis())>(b.b_dis+b.f_dis());
}
};
public:
map(const int N,const int M)
{
n=N;
m=M;
p=new bool*[N+1];
vis=new bool*[N+1];
for(int i=1;i<=N;i++)
{
p[i]=new bool[M+1];
vis[i]=new bool[M+1];
}
}
~map()
{
for(int i=1;i<=n;i++)
{
delete[] p[i];
delete[] vis[i];
}
delete[] p;
delete[] vis;
}
void set_obstruction(const int x,const int y)
{
p[x][y]=true;
return;
}
void set_obstruction(const int x1,const int y1,const int x2,const int y2)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(isin(x1,y1,x2,y2,i,j))
p[i][j]=true;
return;
}
int get_way(const int x1,const int y1,const int x2,const int y2)
{
priority_queue<point> q;
point::tar_x=x2;
point::tar_y=y2;
point start;
start.x=x1;
start.y=y1;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
vis[i][j]=false;
q.push(start);
while(true)
{
const point seach=q.top();
q.pop();
if(!vis[seach.x+1][seach.y])
{
point tmp;
tmp.x=seach.x+1;
tmp.y=seach.y;
tmp.b_dis=seach.b_dis+1;
if(!tmp.f_dis()) return tmp.b_dis;
q.push(tmp);
vis[seach.x+1][seach.y]=true;
}
if(!vis[seach.x-1][seach.y])
{
point tmp;
tmp.x=seach.x-1;
tmp.y=seach.y;
tmp.b_dis=seach.b_dis+1;
if(!tmp.f_dis()) return tmp.b_dis;
q.push(tmp);
vis[seach.x+1][seach.y]=true;
}
if(!vis[seach.x][seach.y+1])
{
point tmp;
tmp.x=seach.x;
tmp.y=seach.y+1;
tmp.b_dis=seach.b_dis+1;
if(!tmp.f_dis()) return tmp.b_dis;
q.push(tmp);
vis[seach.x+1][seach.y]=true;
}
if(!vis[seach.x][seach.y-1])
{
point tmp;
tmp.x=seach.x;
tmp.y=seach.y-1;
tmp.b_dis=seach.b_dis+1;
if(!tmp.f_dis()) return tmp.b_dis;
q.push(tmp);
vis[seach.x+1][seach.y]=true;
}
}
}
};
int main()
{
map m(10,10);
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
{
int a;
scanf("%d",&a);
if(a)
m.set_obstruction(i,j);
}
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%d",m.get_way(x1,y1,x2,y2));
return 0;
}
求帮调代码........
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- hubei_dxx 2021-06-18 13:54关注
error LNK2001: 无法解析的外部符号 "public: static int map::point::tar_x" (?tar_x@point@map@@2HA)
编译出错,原因是静态成员未声明,在类外进行静态成员变量声明
int map::point::tar_x,map::point::tar_y;编译通过,详细解释参考http://c.biancheng.net/view/2227.html
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 关于#硬件工程#的问题,请各位专家解答!
- ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
- ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
- ¥30 截图中的mathematics程序转换成matlab
- ¥15 动力学代码报错,维度不匹配
- ¥15 Power query添加列问题
- ¥50 Kubernetes&Fission&Eleasticsearch
- ¥15 報錯:Person is not mapped,如何解決?
- ¥15 c++头文件不能识别CDialog
- ¥15 Excel发现不可读取的内容