#29689: TLE找不到錯誤q


110703046@g.nccu.edu.tw (hys)

學校 : 不指定學校
編號 : 178973
來源 : [118.150.147.45]
最後登入時間 :
2022-03-22 00:58:45
c073. 00101 - The Blocks Problem -- UVa101 | From: [219.85.159.180] | 發表日期 : 2022-03-21 20:11

#include<stdio.h>

#include<string.h>

struct block{

        int n;

        struct block *next;

        int back;

};

typedef struct block block;

block my[26];

void onto(block* a)

{

        block *tmp;

        while(a->next!=NULL){

                tmp=a->next;

                a->next=NULL;

                a=tmp;

        }

}

block *findBefore(block *a)

{

        block *tmp;

        if(a->n==a->back){

                return a;

        } else{

                tmp=&my[a->back];

                while(tmp->next->n!=a->n){

                        tmp=tmp->next;

                }

                return tmp;

        }

}

int main(void)

{

        char cmd[10], p[10];

        int a, b;

        int k;

        while(scanf("%d", &k)!=EOF){

                for(int i=0; i<=k; i++){

                        my[i].n=i;

                        my[i].next=NULL;

                        my[i].back=i;

                }

                scanf("%s", &cmd);

                while(strcmp(cmd, "quit")!=0){

                        scanf("%d", &a);

                        scanf("%s", &p);

                        scanf("%d", &b);

                        if(my[a].back==my[b].back)continue;

                        if(strcmp(cmd, "move")==0&&strcmp(p, "onto")==0){

                                block *aptr= &my[a], *bptr= &my[b];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                onto(aptr);

                                onto(bptr);

                                my[b].next=&my[a];

                                while(aptr){

                                        aptr->back=bptr->back;

                                        aptr=aptr->next;

                                }

                        }

                        if(strcmp(cmd, "move")==0&&strcmp(p, "over")==0){

                                block *aptr=&my[a];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                onto(aptr);

                                block *bptr=&my[b];

                                while(bptr->next!=NULL){

                                        bptr=bptr->next;

                                }

                                bptr->next=&my[a];

                                while(aptr){

                                        aptr->back=bptr->back;

                                }

                        }

                        if(strcmp(cmd, "pile")==0&&strcmp(p, "onto")==0){

                                block *bptr=&my[b];

                                onto(bptr);

                                block *aptr=&my[a];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                bptr->next=&my[a];

                                while(aptr){

                                        aptr->back=bptr->back;

                                        aptr=aptr->next;

                                }

                        }

                        if(strcmp(cmd, "pile")==0&&strcmp(p, "over")==0){

                                block *aptr=&my[a], *bptr=&my[b];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                while(bptr->next!=NULL){

                                        bptr=bptr->next;

                                }

                                bptr->next=&my[a];

                                while(aptr){

                                        aptr->back=bptr->back;

                                        aptr=aptr->next;

                                }

                        }

                        scanf("%s", &cmd);

                }

                for(int i=0; i<k; i++){

                        printf("%d: ", i);

                        if(my[i].n=my[i].back){

                                block *p=&my[i];

                                while(p){

                                        printf("%d ", p->n);

                                        p=p->next;

                                }

                        }

                        printf("\n");

                }

        }

 

        return 0;

}

 

de了好久終於從Segmentation fault晉級成TLE了

我真的不行了 請大神幫忙 感謝

 
#29690: Re:TLE找不到錯誤q


cges30901 (cges30901)

學校 : 不指定學校
編號 : 30877
來源 : [101.136.203.77]
最後登入時間 :
2024-04-07 15:34:14
c073. 00101 - The Blocks Problem -- UVa101 | From: [39.8.233.87] | 發表日期 : 2022-03-21 20:31


 


                                while(aptr){

                                        aptr->back=bptr->back;

                                }


我沒仔細看程式碼,可是從debugger看來會卡在這裡

 
#29692: Re:TLE找不到錯誤q


110703046@g.nccu.edu.tw (hys)

學校 : 不指定學校
編號 : 178973
來源 : [118.150.147.45]
最後登入時間 :
2022-03-22 00:58:45
c073. 00101 - The Blocks Problem -- UVa101 | From: [118.150.147.45] | 發表日期 : 2022-03-22 00:49


 


                                while(aptr){

                                        aptr->back=bptr->back;

                                }


我沒仔細看程式碼,可是從debugger看來會卡在這裡


感謝

現在測資的第一個情況對了

但是第二個還是錯的

我設的back是想記錄他所在的linked list的第一個數字

第二個情況我試著印出來四格的back出現的是 1, 3, 3, 3

但我真的想不到為甚麼了QAQ

 
#29693: Re:TLE找不到錯誤q


110703046@g.nccu.edu.tw (hys)

學校 : 不指定學校
編號 : 178973
來源 : [118.150.147.45]
最後登入時間 :
2022-03-22 00:58:45
c073. 00101 - The Blocks Problem -- UVa101 | From: [118.150.147.45] | 發表日期 : 2022-03-22 01:01


 


                                while(aptr){

                                        aptr->back=bptr->back;

                                }


我沒仔細看程式碼,可是從debugger看來會卡在這裡


感謝

現在測資的第一個情況對了

但是第二個還是錯的

我設的back是想記錄他所在的linked list的第一個數字

第二個情況我試著印出來四格的back出現的是 1, 3, 3, 3

但我真的想不到為甚麼了QAQ

剛剛發現是在onto函數裡面沒有把back復原

在這裡過了,但是在別的oj上還是過不了

有人知道還有哪裡可以修改嗎!

#include<stdio.h>

#include<string.h>

struct block{

        int n;

        struct block *next;

        int back;

};

typedef struct block block;

block my[26];

void onto(block* a)

{

        block *tmp;

        while(a!=NULL){

                tmp=a->next;

                a->next=NULL;

                a->back=a->n;

                a=tmp;

        }

}

block *findBefore(block *a)

{

        block *tmp;

        if(a->n==a->back){

                return a;

        } else{

                tmp=&my[a->back];

                while(tmp->next->n!=a->n){

                        tmp=tmp->next;

                }

                return tmp;

        }

}

int main(void)

{

        char cmd[10], p[10];

        int a, b;

        int k;

        while(scanf("%d", &k)!=EOF){

                for(int i=0; i<=k; i++){

                        my[i].n=i;

                        my[i].next=NULL;

                        my[i].back=i;

                }

                scanf("%s", &cmd);

                while(strcmp(cmd, "quit")!=0){

                        scanf("%d", &a);

                        scanf("%s", &p);

                        scanf("%d", &b);

                        if(my[a].back==my[b].back)continue;

                        if(strcmp(cmd, "move")==0&&strcmp(p, "onto")==0){

                                block *aptr= &my[a], *bptr= &my[b];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                onto(aptr);

                                onto(bptr);

                                my[b].next=&my[a];

                                aptr->back=my[b].back;

                        }

                        if(strcmp(cmd, "move")==0&&strcmp(p, "over")==0){

                                block *aptr=&my[a];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                onto(aptr);

                                block *bptr=&my[b];

                                while(bptr->next!=NULL){

                                        bptr=bptr->next;

                                }

                                bptr->next=&my[a];

                                aptr->back=my[b].back;

                        }

                        if(strcmp(cmd, "pile")==0&&strcmp(p, "onto")==0){

                                block *bptr=&my[b];

                                onto(bptr);

                                block *aptr=&my[a];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                bptr->next=&my[a];

                                while(aptr){

                                        aptr->back=bptr->back;

                                        aptr=aptr->next;

                                }

                        }

                        if(strcmp(cmd, "pile")==0&&strcmp(p, "over")==0){

                                block *aptr=&my[a], *bptr=&my[b];

                                block *tmp=findBefore(aptr);

                                if(tmp->n!=my[a].n){

                                        tmp->next=NULL;

                                }

                                while(bptr->next!=NULL){

                                        bptr=bptr->next;

                                }

                                bptr->next=&my[a];

                                while(aptr){

                                        aptr->back=bptr->back;

                                        aptr=aptr->next;

                                }

                        }

                        scanf("%s", &cmd);

                }

                for(int i=0; i<k; i++){

                        printf("%d: ", i, my[i].back);

                        if(my[i].n==my[i].back){

                                block *p=&my[i];

                                while(p){

                                        printf("%d ", p->n);

                                        p=p->next;

                                }

                        }

                        printf("\n");

                }

        }

 

        return 0;

}

 
ZeroJudge Forum