Thread: who can sent me code about 8 numbers

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    who can sent me code about 8 numbers

    1 3 2 1 2 3
    8 5 0 =>> 8 0 4
    6 7 4 7 6 5
    thanks!

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    I'm afraid you're going to have to be a lot more descriptive than that. What is it your are trying to do? With what? What have you done so far?
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    sorry!

    I have no Intelligence about C!

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    And we don't have psychic powers... We can't help if we don't know what you're trying to do.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    could you help!

    I try my best to figure out !But I fail!
    2 3 4
    1 8 0
    7 6 5

    2 0 8
    1 4 3
    7 6 5

    2 8 3
    1 4 5
    7 0 6

    2 8 3
    0 6 4
    1 7 5

    2 8 3
    1 6 0
    7 5 4

    8 3 0
    2 1 4
    7 6 5

    8 1 3
    2 0 4
    7 6 5

    2 8 3
    7 0 4
    6 1 5

    2 8 3
    7 1 4
    6 5 0

    1 2 3
    8 0 4
    7 6 5


    #include<iostream.h>
    #include<stdlib.h>
    #include<stdio.h>
    #define Nil 0
    #define LEN sizeof(struct Table)
    struct Table
    {
    int change[3][3];
    struct Table *next;
    };
    struct Table *HeadofOpen(Nil),*HeadofClose(Nil),*temp;
    struct Table *AddtoOpen(int source[3][3],struct Table *Head);
    struct Table *MovetoClose(struct Table *head1,struct Table *head2);
    int ExtendOpen(struct Table *p);
    int InTable(int matrix[3][3],struct Table *head);
    int Same(int matrix1[3][3],int matrix2[3][3]);
    void output(struct Table *head,FILE *pFile);
    void swap(int &a,int &b);
    static int target[3][3]={{1,2,3},{8,0,4},{7,6,5}}; //目标状态
    int main()
    {
    int source[3][3];
    int i,j,flag(0);
    char c;
    cout<<"请输入初始状态!"<<endl; //读取初始状态
    temp=(struct Table *)malloc(LEN);
    for(i=0;i<3;i++)
    {
    cout<<"请输入第"<<(i+1)<<"行,每输入一个数字后按空 格,输入三个数字后按回车结束:"<<endl;
    for(j=0;j<3;j++)
    cin>>source[i][j];
    }
    HeadofOpen=AddtoOpen(source,HeadofOpen); //初始状态进入OPEN表中
    if(Same(source,target)) flag=1; //如果输入状态已是目标状态,则置flag为1
    while((HeadofOpen!=Nil)&&(flag!=1))
    {
    HeadofClose=MovetoClose(HeadofOpen,HeadofClose); //将第1个结点从OPEN表移出并放入CLOSE表
    flag=ExtendOpen(temp); //扩展n,把它的后继结点放入OPEN表的末端,提供回到n的 指针
    }
    if(HeadofOpen==Nil)
    cout<<"该问题无法解决!"<<endl;
    else
    {
    FILE *pFile;

    /* Open for write */
    if( (pFile = fopen( "output.txt", "w" )) == NULL )
    {
    printf( "The file 'output.txt' was not opened\n" );
    return 0;
    }
    char s[100];
    sprintf( s,"CLOSE表的内容:\n");
    fwrite( s,strlen(s),1,pFile);
    cout<<"CLOSE表的内容:"<<endl;
    output(HeadofClose,pFile);
    sprintf( s,"OPEN表的内容:\n");
    fwrite( s,strlen(s),1,pFile);
    cout<<"OPEN表的内容:"<<endl;
    output(HeadofOpen,pFile);
    fclose(pFile);
    }
    cout<<"Press any key to terminate!";
    cin>>c;

    return 0;
    }


    //************************************************** **************
    struct Table *AddtoOpen(int matrix[3][3],struct Table *Head) //将结点放入OPEN表中
    {
    int i,j;
    struct Table *p0,*p1,*p2;
    p0=p1=p2=(struct Table *)malloc(LEN);
    if(Head==Nil) //如果表是空的,则直接放入
    {
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    (p1->change)[i][j]=matrix[i][j];
    }
    }
    Head=p1;
    (p1->next)=Nil;
    }
    else //如果表中已有内容,则插入到表的最后
    {
    p1=Head;
    while((p1->next)!=Nil)
    {
    p2=p1;
    p1=(p2->next);
    }
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    (p0->change)[i][j]=matrix[i][j];
    }
    }
    p1->next=p0;
    p0->next=Nil;
    };
    return(Head); //将头指针返回
    }
    //************************************************** **********


    //************************************************** **********
    struct Table *MovetoClose(struct Table *head1,struct Table *head2) //从OPEN表移入到CLOSE表中
    {
    int i,j;
    struct Table *p0,*p1,*p2;
    void output(struct Table *head1);
    p0=p1=p2=(struct Table *)malloc(LEN);
    if(head2==Nil)
    {
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    (p1->change)[i][j]=(head1->change)[i][j];
    }
    head2=p1;
    p1->next=Nil;
    }
    else
    {
    p1=head2;
    while((p1->next)!=Nil)
    {
    p2=p1;
    p1=p2->next;
    }
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    (p0->change)[i][j]=(head1->change)[i][j];
    }
    p1->next=p0;
    p0->next=Nil;
    };
    temp=HeadofOpen;
    HeadofOpen=temp->next;
    return(head2);
    }
    //************************************************** ************

    //************************************************** ************
    int ExtendOpen(struct Table *p) //扩展OPEN表
    {
    int temp1[3][3],temp2[3][3],temp3[3][3],temp4[3][3];
    int i,j,flag(0);
    int row(0),line(0);
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    temp1[i][j]=(p->change)[i][j];
    temp2[i][j]=(p->change)[i][j];
    temp3[i][j]=(p->change)[i][j];
    temp4[i][j]=(p->change)[i][j];
    if((p->change)[i][j]==0)
    {
    row=i;
    line=j;
    }
    }
    };
    if((line>0)&&(!flag))
    {
    swap(temp1[row][line],temp1[row][line-1]);
    if((!(InTable(temp1,HeadofOpen)))&&(!(InTable(temp 1,HeadofClose))))
    HeadofOpen=AddtoOpen((temp1),HeadofOpen);
    if(Same(temp1,target))
    flag=1;
    };
    if((row>0)&&(!flag))
    {
    swap(temp2[row][line],temp2[row-1][line]);
    if((!(InTable(temp2,HeadofOpen)))&&(!(InTable(temp 2,HeadofClose))))
    HeadofOpen=AddtoOpen(temp2,HeadofOpen);
    if(Same(temp2,target))
    flag=1;
    };
    if((line<2)&&(!flag))
    { swap(temp3[row][line],(temp3)[row][line+1]);
    if((!(InTable(temp3,HeadofOpen)))&&(!(InTable(temp 3,HeadofClose))))
    HeadofOpen=AddtoOpen(temp3,HeadofOpen);
    if(Same(temp3,target))
    flag=1;
    };
    if((row<2)&&(!flag))
    { swap((temp4)[row][line],(temp4)[row+1][line]);
    if((!(InTable(temp4,HeadofOpen)))&&(!(InTable(temp 4,HeadofClose))))
    HeadofOpen=AddtoOpen(temp4,HeadofOpen);
    if(Same(temp4,target))
    flag=1;
    };
    return (flag);
    }
    //************************************************** ****
    int InTable(int matrix[3][3],struct Table *head) //判断新产生的结点是否属于CLOSE或OPEN表
    {
    int i,j,flag1(0),flag2(0);
    struct Table *p1,*p2;
    p1=head;
    while((p1!=Nil)&&(!flag2))
    {
    flag1=0;
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    if(matrix[i][j]!=(p1->change)[i][j])
    flag1=1;
    }
    if(flag1==0) flag2=1; //后移一个检查
    p2=p1;
    p1=p2->next;
    }
    return(flag2);
    }
    //************************************************** ****
    int Same(int matrix1[3][3],int matrix2[3][3]) //判断是否相同的矩阵
    {
    int i,j,flag3(1);
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    if(matrix1[i][j]!=matrix2[i][j])
    flag3=0;
    }
    }
    return(flag3);
    }

    //************************************************** *
    void swap(int &a,int &b)
    {
    int t;
    t=a;a=b;b=t;
    }
    //************************************************** *
    void output(struct Table *head,FILE *pFile) //将表中内容依次输出
    {



    char s[100]="";
    int i,j;
    struct Table *p1,*p2;
    p1=head;
    while(p1!=Nil)
    {
    for(i=0;i<3;i++)
    {
    for(j=0;j<3;j++)
    {
    char stemp[100];
    sprintf(stemp,"%d ",(p1->change)[i][j]);
    strcat(s,stemp);
    cout<<(p1->change)[i][j]<<" ";
    }
    strcat(s,"\n");
    cout<<endl;
    }
    strcat(s,"\n");
    cout<<endl;
    fwrite(s,strlen(s),1,pFile);
    sprintf(s,"");
    p2=p1;
    p1=p2->next;
    }


    }

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Can you explain in just simple English what your program should do?

  7. #7
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Re: sorry!

    Originally posted by xacoolboy
    I have no Intelligence about C!
    I'm sure that's not the only thing you have "no intelligence about"

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    thanks

    2 8 3
    1 0 4
    7 6 5
    transform
    1 2 3
    8 0 4
    7 6 5
    by C programing!

    extent searching or depth searching

    thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. prime numbers code compile error
    By Tony654321 in forum C Programming
    Replies: 5
    Last Post: 10-10-2004, 10:13 AM
  4. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 12:51 PM