UVA12113-Overlapping Squares(二進位枚舉)

來源:互聯網
上載者:User

標籤:sizeof   接下來   nsis   c++   div   nbsp   namespace   void   The   

Problem UVA12113-Overlapping Squares

Accept:116  Submit:596Time Limit: 3000 mSec Problem Description

 

 

 

 Input

The input consists of several test cases. Each test case is contained in ?ve lines and each line contains nine characters. If the horizontal border of a ?lled square is visible it is denoted with ‘ ’ (ASCII value 95) sign and if vertical border of a ?lled square is visible then it is denoted with ‘|’ (ASCII value 124) character. The board contains no other character than ‘ ’, ‘|’ and of course ‘ ’ (ASCII Value 32). The border lines of the squares can only be along the grid lines. Each board lines end with a ‘#’ (Hash character) which denotes the end of line. This character is not a part of the grid or square. The last test case is followed by a single zero, which should not be processed.

 

 Output

For each test case, print the case number and ‘Yes’ or ‘No’, depending on whether it’s possible to form the target.

 

 Sample Input

 

 Sample Ouput

Case 1: Yes
Case 2: Yes
Case 3: No
Case 4: Yes

 

題解:感覺最近做的題都十分考驗代碼能力(然而我很水),想到一共只有九種擺放方案之後這個題的思維就基本上結束了,所有的挑選方案只有2^9,直接二進位枚舉,對於相同的挑選方案,不同的擺放順序也會帶來不同的覆蓋結果,解決方案就是next_permutation(),預先處理出來不同小正方形的覆蓋格子的標號,接下來暴力就好。

 

 1 #include <bits/stdc++.h> 2  3 using namespace std; 4  5 const int maxn = (1<<9); 6 const int N = 5,M = 9; 7 const int kind = 9; 8  9 int edge[9][8] = {{1,3,9,13,18,19,21,22}};10 int core[9][4] = {{10,11,12,20}};11 int bits[kind+1],target[N*M];12 13 int read(){14     char str[20];15     int cnt = 0,edges = 0;16     for(int i = 0;i < N;i++){17         gets(str);18         if(str[0] == ‘0‘) return -1;19         for(int j = 0;j < M;j++){20             if(str[j] == ‘ ‘) target[cnt++] = 0;21             else target[cnt++] = 1,edges++;22         }23     }24     return edges;25 }26 27 void init(){28     for(int i = 0;i < 3;i++){29         for(int j = 0;j < 3;j++){30             if(!i && !j) continue;31             int plus,minus;32             if(j == 0) plus = 9,minus = 3;33             else plus = 2,minus = 1;34             for(int k = 0;k < 8;k++){35                 edge[i*3+j][k] = edge[i*3+j-minus][k]+plus;36             }37             for(int k = 0;k < 4;k++){38                 core[i*3+j][k] = core[i*3+j-minus][k]+plus;39             }40         }41     }42 }43 44 int bitcount(int s){45     return s == 0 ? 0 : bitcount(s>>1)+(s&1);46 }47 48 void bitpos(int s){49     int cnt = 0;50     for(int i = 0;i < 9;i++){51         if(s&(1<<i)) bits[cnt++] = i;52     }53 }54 55 int iCase = 1;56 57 int main()58 {59 #ifdef GEH60     freopen("helloworld.01,inp","r",stdin);61 #endif62     init();63     int edge_cnt;64     while(edge_cnt=read()){65         if(edge_cnt == -1) break;66         int tmp[M*N];67         bool ok = false;68         for(int s = 0;s < maxn;s++){69             int n = bitcount(s);70             bitpos(s);71             if(n>6 || n*8<edge_cnt) continue;72             do{73                 memset(tmp,0,sizeof(tmp));74                 for(int i = 0;i < n;i++){75                     for(int j = 0;j < 8;j++){76                         tmp[edge[bits[i]][j]] = 1;77                     }78                     for(int j = 0;j < 4;j++){79                         tmp[core[bits[i]][j]] = 0;80                     }81                 }82 83                 if(memcmp(tmp,target,sizeof(target)) == 0){84                     ok = true;85                     break;86                 }87             }while(next_permutation(bits,bits+n));88             if(ok) break;89         }90         printf("Case %d: ",iCase++);91         if(ok) printf("Yes\n");92         else printf("No\n");93     }94     return 0;95 }

 

UVA12113-Overlapping Squares(二進位枚舉)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.