程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 10051 - Tower of Cubes

10051 - Tower of Cubes

編輯:C++入門知識

[cpp] 
描述:麻煩的題,需要記錄走過的路程,而且和最短路還有些車別 
#include <cstdio>  
struct cube 

    int val; 
    int sum; 
    int no; 
    int x; 
    int y; 
}; 
cube v[510][6]; 
char str[6][7]= {"front","back","left","right","top","bottom"}; 
int main() 

    //freopen("a.txt","r",stdin);  
    int n,p[6],flag[6][2],q[6],m[2],sum,t=0; 
    while(scanf("%d",&n)!=EOF) 
    { 
        if(!n) break; 
        if(t) puts(""); 
        for(int i=0; i<n; i++) 
            for(int j=0; j<6; j++) 
            { 
                scanf("%d",&v[i][j].val); 
                v[i][j].sum=1; 
                v[i][j].no=v[i][j].x=v[i][j].y=-1; 
            } 
        sum=0,m[0]=m[1]=-1; 
        for(int i=n-2; i>=0; i--) 
        { 
            for(int j=0; j<6; j++) 
            { 
                p[j]=0; 
                q[j]=flag[j][1]=flag[j][0]=-1; 
            } 
            for(int j=i+1; j<n; j++) 
                for(int k=0; k<6; k++) 
                    for(int l=0; l<6; l++) 
                        if(v[j][l].val==v[i][k].val) 
                        { 
                            int c; 
                            if(l%2==0) c=l+1; 
                            else c=l-1; 
                            if(p[k]>=v[j][c].sum) continue; 
                            q[k]=l; 
                            flag[k][0]=j; 
                            flag[k][1]=c; 
                            p[k]=v[j][c].sum; 
                        } 
            for(int j=0; j<6; j++) 
            { 
                v[i][j].sum+=p[j]; 
                v[i][j].x=flag[j][0]; 
                v[i][j].y=flag[j][1]; 
                v[i][j].no=q[j]; 
                if(v[i][j].sum>sum) 
                { 
                    sum=v[i][j].sum; 
                    m[0]=i; 
                    m[1]=j; 
                } 
            } 
        } 
        printf("Case #%d\n%d\n",++t,sum); 
        int c,a=m[0],b=m[1]; 
        if(m[1]%2==0) c=m[1]+1; 
        else c=m[1]-1; 
        printf("%d %s\n",m[0]+1,str[c]); 
        c=v[a][b].no; 
        m[0]=v[a][b].x; 
        m[1]=v[a][b].y; 
        while(1) 
        { 
            if(m[1]==-1||m[0]==-1) break; 
            printf("%d %s\n",m[0]+1,str[c]); 
            a=m[0],b=m[1]; 
            c=v[a][b].no; 
            m[0]=v[a][b].x; 
            m[1]=v[a][b].y; 
        } 
    } 
    return 0; 

描述:麻煩的題,需要記錄走過的路程,而且和最短路還有些車別
#include <cstdio>
struct cube
{
    int val;
    int sum;
    int no;
    int x;
    int y;
};
cube v[510][6];
char str[6][7]= {"front","back","left","right","top","bottom"};
int main()
{
    //freopen("a.txt","r",stdin);
    int n,p[6],flag[6][2],q[6],m[2],sum,t=0;
    while(scanf("%d",&n)!=EOF)
    {
        if(!n) break;
        if(t) puts("");
        for(int i=0; i<n; i++)
            for(int j=0; j<6; j++)
            {
                scanf("%d",&v[i][j].val);
                v[i][j].sum=1;
                v[i][j].no=v[i][j].x=v[i][j].y=-1;
            }
        sum=0,m[0]=m[1]=-1;
        for(int i=n-2; i>=0; i--)
        {
            for(int j=0; j<6; j++)
            {
                p[j]=0;
                q[j]=flag[j][1]=flag[j][0]=-1;
            }
            for(int j=i+1; j<n; j++)
                for(int k=0; k<6; k++)
                    for(int l=0; l<6; l++)
                        if(v[j][l].val==v[i][k].val)
                        {
                            int c;
                            if(l%2==0) c=l+1;
                            else c=l-1;
                            if(p[k]>=v[j][c].sum) continue;
                            q[k]=l;
                            flag[k][0]=j;
                            flag[k][1]=c;
                            p[k]=v[j][c].sum;
                        }
            for(int j=0; j<6; j++)
            {
                v[i][j].sum+=p[j];
                v[i][j].x=flag[j][0];
                v[i][j].y=flag[j][1];
                v[i][j].no=q[j];
                if(v[i][j].sum>sum)
                {
                    sum=v[i][j].sum;
                    m[0]=i;
                    m[1]=j;
                }
            }
        }
        printf("Case #%d\n%d\n",++t,sum);
        int c,a=m[0],b=m[1];
        if(m[1]%2==0) c=m[1]+1;
        else c=m[1]-1;
        printf("%d %s\n",m[0]+1,str[c]);
        c=v[a][b].no;
        m[0]=v[a][b].x;
        m[1]=v[a][b].y;
        while(1)
        {
            if(m[1]==-1||m[0]==-1) break;
            printf("%d %s\n",m[0]+1,str[c]);
            a=m[0],b=m[1];
            c=v[a][b].no;
            m[0]=v[a][b].x;
            m[1]=v[a][b].y;
        }
    }
    return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved