关于qsort排序时,结构体内元素产生偏离了?
问题主要是我声明了一个用来标记坐标的结构体变量,然后用qsort快排,快排是基于该节点与其父节点的距离长短。然而出错了。通过在程序中插入printf显示,qsort 对结构体的各个变量的访问出了问题。
正确的输出应该是《x->x,x->y》《x->father->x,x->father->y》|《y->x,y->y》《y->father->x,y->father->y》
而程序输出的,,,《?,疑似父节点的y》《x->x,x->y》|《?,疑似父节点的y》《y->x,y->y》
源代码如下,主要也就关心一下main和Comp就行。。
程序代码:#include <stdio.h>
#include<stdlib.h>
/*
评测结果 时间 结果 得分 题目 编译器 用时(ms) 内存(MB) 用户
2016-04-17 18:19 正在评测 0 07-图5 gcc 无 无 569985011
测试点结果 测试点 结果 得分/满分 用时(ms) 内存(MB) 要点提示
测试点1 答案正确 15/15 17 1 sample1 多条最短路,同一点有多路,最近点无路,多连通
测试点2 答案正确 3/3 2 1 sample 2 聚集型,均离岸远
测试点3 答案正确 2/2 1 1 分散型,均跳不到,有在角上
测试点4 答案正确 3/3 2 1 有一只在岸上,有一只在岛上,不能算在内
测试点5 答案错误 0/6 1 1 最大N,sample1的复杂版,可选路径8条,后面要更新前面的最短路
测试点6 答案正确 1/1 2 1 最小N,一步跳到岸
*/
typedef struct node *Node;
struct node {
int x,y;
Node father;//记录他的father,出去以后在处理一遍。
};
typedef struct queue {
Node Q[101];
int Left,Right,Pause,Cen;
}*Queue;
int IsReached(Node,const int );
int CanReached(Node,Node,const float );
Node BFS(Node*,Queue,const int Lenth,int );
void Print(Node head) {
if(head) {
Print(head->father );
if(head->x ==0&&head->y==0)return;
printf("%d %d\n",head->x,head->y);
}
}
Node SourceNode;
int Comp(const void*a,const void*b) {
Node x=(Node)a;
Node y=(Node)b;
printf("&x=%d,&y=%d,",x,y);
printf("<%d,%d><%d,%d>|<%d,%d><%d,%d>\n",x->x,x->y,x->father->x,x->father->y,y->x,y->y,y->father->x,y->father->y);
int lenthx=((x->x)-(x->father->x))*((x->x)-(x->father->x))+((x->y)-(x->father->y))*((x->y)-(x->father->y));
int lenthy=(y->x-y->father->x)*(y->x-y->father->x)+(y->y-y->father->y)*(y->y-y->father->y);
return lenthx-lenthy;
}
int main() {
int n;
int lenth;
Node L[101];
scanf("%d%d",&n,&lenth);
struct node Zero;
Zero.x =0;Zero.y=0;Zero.father =NULL;
printf("[%d,%d]",Zero.x ,Zero.y);
for(int i=0; i<n; i++) {
L[i]=(Node)malloc(sizeof(struct node));
scanf("%d%d",&L[i]->x,&L[i]->y);
L[i]->father=&Zero;
Node x=L[i];
printf("(%d)",(x->x-x->father->x)*(x->x-x->father->x)+(x->y-x->father->y)*(x->y-x->father->y));
}
printf("\n|下面进入qsort函数|\n");
qsort(L,n,sizeof(Node),Comp);
for(int i=0;i<n;i++){
printf("[%d,%d]",L[i]->x,L[i]->y);
}
//---------------------
if(lenth>=35) {//如果邦德会轻功~~~
printf("1");
return 0;
}
//-----------------初始化队列
struct queue Q;
Q.Q[0]=&Zero;
Q.Left =0;
Q.Right =1;
Q.Pause =0;
Q.Cen =1;
//---------------------------
Node head=BFS(L,&Q,lenth,n);
if(!head)printf("0");
else Print(head);
return 0;
}
Node BFS(Node*L,Queue Q,const int Lenth,int n) {
float IsFirstJump=7.5;//用来处理第一跳,本层遍历结束后就赋0.
struct node None= {500,500};
while(Q->Left <Q->Right) {
for(int i=0; i<n; i++) {
if(CanReached(Q->Q[Q->Left],L[i],Lenth+IsFirstJump)) {
Node temp=L[i];
L[i]=&None;
temp->father=Q->Q[Q->Left];
if(IsReached(temp,Lenth)) {
printf("%d\n",Q->Cen+1 );
return temp;
}
Q->Q[Q->Right++]=temp;
}
}
if(Q->Left++==Q->Pause ) {
qsort(&Q->Q[Q->Pause],Q->Right-Q->Pause,sizeof(Node),Comp);
IsFirstJump=0;
Q->Pause =Q->Right ;
++Q->Cen;
}
}
return NULL;
}
int IsReached(Node L,const int lenth) {
int flag=0;
if(L->x >=50-lenth)flag=1;
else if(L->x <=lenth-50)flag=1;
else if(L->y >=50-lenth)flag=1;
else if(L->y <=lenth-50)flag=1;
return flag;
}
int CanReached(Node L,Node K,const float lenth) {
int x,y;
x=(L->x > K->x) ?(L->x)-(K->x):(K->x)-(L->x);
y=(L->y > K->y) ?(L->y)-(K->y):(K->y)-(L->y);
return lenth*lenth>=x*x+y*y?1:0;
}[此贴子已经被作者于2016-4-18 14:37编辑过]



