UVA12113-Overlapping Squares(二进制枚举)

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 five lines and each line contains nine characters. If the horizontal border of a filled square is visible it is denoted with ‘ ’ (ASCII value 95) sign and if vertical border of a filled 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 }

 

转载于:https://www.cnblogs.com/npugen/p/9574511.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值