usaco Magic Squares

來源:互聯網
上載者:User

這道題目,以前寫過類似的。所以這次看了題目後,還是有頭緒的。

不過這道真的很麻煩啊!

用到了Hash + BFS

1、2、3、4、5、6、7、8不管將它們怎樣變換,最多也就8! = 40320 種情況,所以可以使用BFS演算法枚舉下,打表即可。這是總的思想。

但1……8是字串,不好打表,所以這時就用到了Hash演算法,我用的字串轉化演算法是從一位大牛那拷過來,挺好的。

其它不需要解釋了!

hehe....

原始碼好下:

/*ID: guo geerPROG: msquareLANG: C++*/#include<iostream>#include<fstream>#include<string>#include<cstring>using namespace std;string ans[40320];int jc[8] = {1, 1, 2, 6, 24, 120, 720, 5040};int Encode(int s[]){    int sum = 0;    for(int i=0; i<8; i++)    {    int count = 0;        for(int j=i; j<8; j++)        if(s[i] > s[j])                count ++;                                sum += jc[7-i]*count;        }    return sum;    }    void Decode(int s[], int n){     for(int i=0; i<8; i++)     {                           s[i] = n/jc[7-i];              n %= jc[7-i];              }              for(int i=7; i>=0; i--)              for(int j=i+1; j<8; j++)              if(s[i] <= s[j])              s[j] ++;     }void Exchange(int s[], int n){     int i,j,k;     int tmp[8];     switch(n)     {              case 0:                   for(i=0; i<8; i++)                   tmp[i] = s[7-i];                   memcpy(s, tmp, 8*sizeof(int));                   break;                   case 1:                        tmp[0] = s[3], tmp[1] = s[0], tmp[2] = s[1], tmp[3] = s[2];                        tmp[4] = s[5], tmp[5] = s[6], tmp[6] = s[7], tmp[7] = s[4];                        memcpy(s, tmp, 8*sizeof(int));                        break;                         case 2:                            tmp[0] = s[0], tmp[1] = s[6], tmp[2] = s[1], tmp[3] = s[3];                            tmp[4] = s[4], tmp[5] = s[2], tmp[6] = s[5], tmp[7] = s[7];                            memcpy(s, tmp, 8*sizeof(int));                            break;               }     }void BFS(){          int i,j,k;     for(i=0; i<40320; i++)     ans[i] = "";          int q[100000];     int r, r1, f;     r = r1 = f = 0;          int init[8] = {0, 1, 2, 3, 4, 5, 6, 7};     k = Encode(init);     q[0] = k;     int steps = 0;          while(true)     {                ++steps;                ++r;                while(f != r)                {                                                for(int x=0; x<3; x++)                        {                            Decode(init, q[f]);                            Exchange(init, x);                                                            k = Encode(init);                            if(ans[k].length() == 0 && k != q[0])                            {                                         q[++r1] = k;                                         ans[k] = ans[q[f]] + (char)('A' + x);                                           }                            }                            ++f;                                                                     }                        if(r == r1 + 1)                        return;                        r = r1;                }     }int main(){       ifstream fin("msquare.in");    ofstream fout("msquare.out");    BFS();    int i,j,k;    int data[8];    for(i=0; i<8; i++)    {             fin>>data[i];             data[i] -- ;             }             k = Encode(data); //cout<<ans[k]<<endl;            // cout<<k<<endl; fout<<ans[k].length()<<endl;            for(i=0; ans[k][i] != '\0'; i++)            {                     if(i%60 == 0 && i != 0)                     fout<<endl;                     fout<<ans[k][i];                     } fout<<endl;    //system("pause");    return 0;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.