Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我做过了好多次的数据检验 都没有错误,但一提交就说出错 为什么啊 有高手能帮帮我吗??代码较长 主要是想利用栈来实现,R和C的值是输入给定的,出错是因为和R、C不定有关吗,高手教教我吧!!谢谢了!!(刚学C不久,很多不懂!) #include<stdio.h> #include<stdlib.h> #define OVERFLOW -2 #define OK 1 #define ERROR 0 #define STACK_INIT_SIZE 30 #define STACKINCREMENT 10 typedef struct Number { int num; int r; int c; int mark; int dir; struct Number *next; }Number,*NumPtr; typedef struct { NumPtr front; NumPtr rear; }LinkNum; typedef struct { NumPtr base; NumPtr top; NumPtr now; int stacksize; }SqStack; int InitStack(SqStack &S) { S.base=(Number*)malloc(STACK_INIT_SIZE*sizeof(Number)); if(!S.base)exit(OVERFLOW); S.now=S.top=S.base; S.stacksize=STACK_INIT_SIZE; return OK; } int Push(SqStack &S,NumPtr &q) { if(S.top-S.base>=S.stacksize) { S.base=(NumPtr)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(Number)); if(!S.base) exit(OVERFLOW); S.top=S.base+S.stacksize; S.stacksize+=STACKINCREMENT; } S.top->r=q->r; S.top->c=q->c; S.top->dir=q->dir; S.top->mark=q->mark; S.top->num=q->num; q->mark=1; S.now=S.top; S.top++; return OK; } int Pop(SqStack &S) { if(S.top==S.base) return ERROR; if(S.now!=S.base) S.now--; S.top--; return OK; } int EmptyStack(SqStack S) { if(S.top==S.base&&S.base->dir==4) return OK; else return ERROR; } int PutStack(SqStack S) { NumPtr fp; if(S.top==S.base){printf("NO!\n");return ERROR;} fp=S.base; while(fp!=S.top) { printf("%3d %2d %2d %d %d\n",fp->num,fp->r,fp->c,fp->mark,fp->dir); fp++; } printf("\n"); return OK; } int InitNumber(LinkNum &N) { N.front=N.rear=(NumPtr)malloc(sizeof(Number)); if(!N.front)exit(OVERFLOW); N.front->next=NULL; return OK; } int DestroyNumber(LinkNum &N) { while(N.front) { N.rear=N.front->next; free(N.front); N.front=N.rear; } return OK; } int EnNumber(LinkNum &N,int e,int i,int j) { NumPtr p; p=(NumPtr)malloc(sizeof(Number)); if(!p)exit(OVERFLOW); p->num=e; p->r=i; p->c=j; p->mark=0; p->dir=0; p->next=NULL; N.rear->next=p; N.rear=p; return OK; } int DeNumber(LinkNum N) { NumPtr p; if(N.front==N.rear)return ERROR; p=N.front->next; while(p) { printf("%3d %2d %2d %d %d\n",p->num,p->r,p->c,p->mark,p->dir); p=p->next; } printf("\n"); return OK; } int LargerNumber(LinkNum N,int &hang,int &lie,int &z) { NumPtr p; int a=0,larger=-1; if(N.front==N.rear) return ERROR; p=N.front->next; while(p) { if(p->mark==0) { if(larger<p->num) { larger=p->num; hang=p->r; lie=p->c; } } else a++; p=p->next; } z=a; return OK; } int Work(int &max) { int i,j,m,n,e,add=0; int markmax,z=0,b=1; int hang,lie; LinkNum N; NumPtr q,nextside,f,father=NULL; SqStack S; InitNumber(N); f=N.front; InitStack(S); scanf("%d%d",&m,&n); for(i=1;i<=m;i++) for(j=1;j<=n;j++) { scanf("%d",&e); EnNumber(N,e,i,j); } while(z<m*n) { markmax=0; LargerNumber(N,hang,lie,z); for(add=0,q=f;add<(hang-1)*n+lie;add++) q=q->next; Push(S,q); markmax++; while(!EmptyStack(S)) { while(S.now->dir<4) { while(b) { switch(S.now->dir) { case 0:i=(S.now->r)-1;j=S.now->c;break; case 1:i=(S.now->r)+1;j=S.now->c;break; case 2:i=(S.now->r);j=(S.now->c)-1;break; case 3:i=(S.now->r);j=(S.now->c)+1;break; default:break; } if(i>m||i<1||j>n||j<1) { S.now->dir=S.now->dir+1; if(S.now->dir==4)goto loop; b=1; } else b=0; } b=1; for(add=0,nextside=f;add<(i-1)*n+j;add++) nextside=nextside->next; if(S.now->num>nextside->num) { S.now->dir=S.now->dir+1; Push(S,nextside); markmax++; } else (S.now->dir)++; } loop: if(max<markmax)max=markmax; Pop(S); markmax--; } } return OK; } void main() { int max=0; Work(max); printf("%d\n",max); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator