码迷,mamicode.com
首页 > 移动开发 > 详细

UVA12113-Overlapping Squares(二进制枚举)

时间:2018-09-02 18:34:19      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:sizeof   接下来   nsis   c++   div   nbsp   namespace   void   The   

Problem UVA12113-Overlapping Squares

Accept:116  Submit:596

Time 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 GEH
60     freopen("helloworld.01,inp","r",stdin);
61 #endif
62     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(二进制枚举)

标签:sizeof   接下来   nsis   c++   div   nbsp   namespace   void   The   

原文地址:https://www.cnblogs.com/npugen/p/9574511.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!