zoukankan      html  css  js  c++  java
  • 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
    ");
    92         else printf("No
    ");
    93     }
    94     return 0;
    95 }
  • 相关阅读:
    thinkphp3.2v
    ng-select 下拉的两种方式
    angular的时间指令 以及防止闪烁问题
    angularjs中的几种工具方法
    运用正则+replace+substring将一段英语的字母大写 angurlar运用自定义指令filter完成首字母大写
    angularjs bind与model配合双向绑定 表达式方法输出
    ajax跨域问题
    团队作业一
    校外实习报告(四)
    校外实习报告(三)
  • 原文地址:https://www.cnblogs.com/npugen/p/9574511.html
Copyright © 2011-2022 走看看