UVA12113-Overlapping Squares(二进制枚举)

本文针对UVA12113-OverlappingSquares问题提供了一种解决方案,通过二进制枚举所有可能的小正方形摆放方案,并使用next_permutation()处理不同的摆放顺序,最终确定是否能形成目标图案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

这是一段 Python 代码,旨在更新三维数据的指定部分(即“块”)。 1. def __update_volume(self, blocks, indexes, overlapping=0, block_size=64): 这是一个方法定义。它的名称是“__update_volume”,括号内的参数是“self”(表示类实例本身),以及三个其他参数(“blocks”,“indexes”和“overlapping”)和一个默认参数(“block_size”)。 2. for block, index in zip(blocks, indexes): 这是一个 for 循环,它会遍历两个序列(“blocks”和“indexes”)并根据顺序将它们的元素配对。每次迭代时,可以使用变量“block”和“index”引用它们中的一个值。 3. block = block[overlapping:-overlapping, overlapping:-overlapping, overlapping:-overlapping,] 这行代码将“block”变量中的数据裁剪到给定的“overlapping”值。同时使用索引和切片操作删除边界。注意这一行代码修改了变量“block”的值。 4. s = [slice(index[i], index[i] + block_size) for i in range(3)] 这段代码为数据的指定块生成一个新的切片对象(“s”)并将其存储在列表中。对于每个坐标轴,“index”变量中存储的“起始”位置被用作切片的起点,并且通过添加“block_size”来确定切片的终点。 5. self.__volume[s[0], s[1], s[2]] = block 这一行代码将“block”值分配给类实例自己的内部变量“__volume”中选定的切片位置。对于每个坐标轴,“s[i]”中包含的切片范围都被用作索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值