UVA 12113 Overlapping Squares

题意:

   总共有6个2*2的正方形,判断是否能够成所给的形状。

思路:

  一个正方形总共有9种摆放方式,对于整个地图来说摆放方式总共有2的9次方种摆放方式。然后将地图用9*5的数组表示,正方形的位置用其8个边的下标和4个中空的下标表示。

代码:

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int bian[9][8]={{1,3,9,13,18,19,21,22},{3,5,11,15,20,21,23,24},{5,7,13,17,22,23,25,26},{10,12,18,22,27,28,30,31},{12,14,20,24,29,30,32,33},{14,16,22,26,31,32,34,35},{19,21,27,31,36,37,39,40},{21,23,29,33,38,39,41,42},{23,25,31,35,40,41,43,44}};//2*2正方形的9种摆放方式的边坐标int nei[9][4]={10,11,12,20,12,13,14,22,14,15,16,24,19,20,21,29,21,22,23,31,23,24,25,33,28,29,30,38,30,31,32,40,32,33,34,42};//2*2正方形的9种摆放方式的中空坐标
char str[100];int map[45],cot[10],map1[45];int read()//读入数据{    int h=0,cnt = 0, kk = 0;    for(int i=0;i<5;i++)    {        if(gets(str)==NULL)            return -1;        if(str[0]==‘0‘)            return -1;        for(int j=0;j<9;j++)        {            map[kk++]=str[j] ==32?0:1;            if(str[j] != 32)                cnt++;        }    }    return cnt;}int count(int s)//计算总共使用了几个正方形{    return s==0?0:count(s/2)+(s&1);}void stick(int p,int &cnt)//构建将第p个正方形贴上去后的地图{    int i,j;    for(i=0;i<8;i++)    {        if(map1[bian[p][i]]==0)//第p个正方形边的位置如果没有边的话,边的数量+1            cnt++;        map1[bian[p][i]]=1;    }    for(i=0;i<4;i++)    {        if(map1[nei[p][i]]==1)//第p个正方形中空的位置如果有边的话,边的数量-1
            cnt--;        map1[nei[p][i]]=0;    }}int main(){    int flag,cnt;    int i,j,k;    int h=1;    while((flag=read())&&flag!=-1)    {        //cout<<flag<<endl;        int flag1=0;        for(i=0;i<(1<<9);i++)        {            int n=count(i);            if(n>6||8*n<flag)                continue;            k=0;            for(j=0;j<9;j++)                if((i>>j)&1)                    cot[k++]=j;            //cout<<k<<endl;            do            {                //cout<<1<<endl;                memset(map1,0,sizeof(map1));                cnt=0;                //cout<<n<<endl;                for(j=0;j<n;j++)                {                    int p=cot[j];//cout<<1<<endl;                    stick(p,cnt);                }
                if(cnt==flag)                {                    int flag2=1;                    for(int l=0;l<45;l++)                    {                        if(map[l]!=map1[l])                        {                            flag2=0;break;
                        }
                    }if(flag2)                            flag1=1;
                }                if(flag1)                    break;            }            while(next_permutation(cot,cot+k));        }        printf("Case %d: %s\n",h++, flag1? "Yes" : "No");    }    return 0;}
时间: 2024-08-07 00:13:57

UVA 12113 Overlapping Squares的相关文章

UVA - 12113 Overlapping Squares(dfs+回溯)

题目: 给定一个4*4的棋盘和棋盘上所呈现出来的纸张边缘,问用不超过6张2*2的纸能否摆出这样的形状. 思路: dfs纸的张数,每一张中枚举这张纸左上角这个点的位置,暴力解题就可以了. 这个题的覆盖太恶心了,很容易搞混~~~(因为搞混一直TLE+WA----) 代码: #include <bits/stdc++.h> #define inf 0x3f3f3f3f #define MAX 1000000000 #define mod 1000000007 #define FRE() freope

【习题 7-6 UVA - 12113】Overlapping Squares

[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 先预处理出来一个正方形. 然后每次枚举新加的正方形左上角的坐标就可以. 注意覆盖的规则,控制一下就可以. 然后暴力判断是否相同. 暴力回溯即可(只用回溯一个正方形区域) [代码] /* 1.Shoud it use long long ? 2.Have you ever test several sample(at least therr) yourself? 3.Can you promise that the solutio

【UVA】201 Squares(模拟)

题目 题目 ? ? 分析 记录一下再预处理一下. ? ? 代码 #include <bits/stdc++.h> int main() { int t=0,s,n; while(scanf("%d",&s)==1) { if(t!=0) printf("\n**********************************\n\n"); int ans[11],H[21][21],V[21][21]; memset(V,0,sizeof(V))

UVA-12113 Overlapping Squares (回溯+暴力)

题目大意:问能不能用不超过6张2x2的方纸在4x4的方格中摆出给定的图形? 题目分析:暴力枚举出P(9,6)种(最坏情况)方案即可. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<algorithm> using namespace std; int vis[10]; char mp[8][15],p1[8][15]; bool judge() { for(i

ACM-ICPC Dhaka Regional 2012 题解

B: Uva: 12582 - Wedding of Sultan 给定一个字符串(仅由大写字母构成)一个字母表示一个地点,经过这个点或离开这个点都输出这个地点的字母) 问: 每个地点经过的次数(维护一个栈就可以了,注意进入起点和离开起点都不算入起点的次数) #include<cstdio> #include<cstring> #include<stack> #include<iostream> #include<algorithm> using

USACO2013January(乱做)

由于不清楚来源,题目乱放: Problem 1: Mirrors [Brian Dean and Travis Hance, 2013] Farmer John's cows have been causing too much trouble around the farm, andFJ therefore wants to keep a more watchful eye on them.  By installing Nreflective fences (1 <= N <= 200)

Detecting diabetic retinopathy in eye images

Detecting diabetic retinopathy in eye images The past almost four months I have been competing in a Kaggle competition about diabetic retinopathy grading based on high-resolution eye images. In this post I try to reconstruct my progression through th

补题列表

上海网络赛: HDU 5468 Puzzled Elena HDU 5469 Antonidas HDU 5473 There was a kingdom 合肥网络赛: HDU 5487 Difference of Languages HDU 5486 Difference of Clustering HDU 5489 Removed Interval HDU 5492 Find a path HDU 5493 Queue 弱校联萌Day1: B. Carries D. Vertex Cover

uva 201 - Squares(自己的方法,同学有更好一点点的方法)

#include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<algorithm> using namespace std; int h[20][20]; int v[20][20]; int size_[20]; int n,m; int flag; void judge(int x,int y)//这是关键部分的代码,用来判读那是否能构成sq