[THUPC2022 初赛] 画图 题解
思路:
先把水平线段按y坐标排序,y坐标相同时把可以合并的合并,再把竖直线段按x坐标排序,x坐标相同时把可以合并的合并。
然后依次处理每个字母即可。先判断横竖线段数量是否够,如果不够,直接返回no。依次处理每个字母,枚举所有未被选择的线段,判断能否组成这个字母并且和其他字母不相交,如果所有字母都能组成,则返回yes,否则返回no。
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int N=1e5+5; 5 struct SHPI{ 6 int l,r,y; 7 }sp[N],heng[N],T1,H4,U7,P10,P11,C14,C15; 8 struct SHZH{ 9 int d,u,x; 10 }sz[N],shu[N],T2,H3,H5,U6,U8,P9,P12,C13; 11 int n,cntsp,cntsz,cnth,cnts; 12 bool usedh[N],useds[N]; 13 int redd(){ 14 int xx=0,fl=1;char ch=getchar(); 15 while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); 16 if(ch=='-'){ch=getchar();fl=-1;} 17 for(;ch>='0'&&ch<='9';ch=getchar())xx=(xx<<3)+(xx<<1)+ch-'0'; 18 return xx*fl; 19 } 20 int maxx(int x,int y){ 21 return x>y?x:y; 22 } 23 int minn(int x,int y){ 24 return x<y?x:y; 25 } 26 bool cmpsp(SHPI p,SHPI q){ 27 if(p.y!=q.y)return p.y<q.y; 28 return p.l<q.l; 29 } 30 bool cmpsz(SHZH p,SHZH q){ 31 if(p.x!=q.x)return p.x<q.x; 32 return p.d<q.d; 33 } 34 bool xj(SHPI p,SHZH q){ //判断是否相交 35 if(p.l<=q.x&&q.x<=p.r&&q.d<=p.y&&p.y<=q.u)return 1; 36 return 0; 37 } 38 bool workt(){ //T 39 for(int i=1;i<=cnth;i++){ 40 for(int j=1;j<=cnts;j++){ 41 T1=heng[i];T2=shu[j];usedh[i]=1;useds[j]=1; 42 if(T2.d<T1.y&&T1.y==T2.u&&T1.l<T2.x&&T2.x<T1.r)return 1; 43 usedh[i]=0;useds[j]=0; 44 } 45 } 46 return 0; 47 } 48 bool workh(){ //H 49 for(int i=1;i<=cnts;i++)if(!useds[i]){ 50 H3=shu[i]; 51 if(xj(T1,H3))continue; 52 useds[i]=1; 53 for(int j=1;j<=cnth;j++)if(!usedh[j]){ 54 H4=heng[j]; 55 if(xj(H4,T2))continue; 56 usedh[j]=1; 57 for(int k=1;k<=cnts;k++)if(!useds[k]){ 58 H5=shu[k]; 59 if(xj(T1,H5))continue; 60 useds[k]=1; 61 if(H3.d==H5.d&&H5.d<H4.y&&H4.y<H3.u&&H3.u==H5.u&&H3.x==H4.l&&H4.l<H4.r&&H4.r==H5.x)return 1; 62 useds[k]=0; 63 } 64 usedh[j]=0; 65 } 66 useds[i]=0; 67 } 68 return 0; 69 } 70 bool worku(){ //U 71 for(int i=1;i<=cnts;i++)if(!useds[i]){ 72 U6=shu[i]; 73 if(xj(T1,U6))continue; 74 if(xj(H4,U6))continue; 75 useds[i]=1; 76 for(int j=1;j<=cnth;j++)if(!usedh[j]){ 77 U7=heng[j]; 78 if(xj(U7,T2))continue; 79 if(xj(U7,H3))continue; 80 if(xj(U7,H5))continue; 81 usedh[j]=1; 82 for(int k=1;k<=cnts;k++)if(!useds[k]){ 83 U8=shu[k]; 84 if(xj(T1,U8))continue; 85 if(xj(H4,U8))continue; 86 useds[k]=1; 87 if(U6.d==U8.d&&U8.d==U7.y&&U7.y<U6.u&&U6.u==U8.u&&U6.x==U7.l&&U7.l<U7.r&&U7.r==U8.x)return 1; 88 useds[k]=0; 89 } 90 usedh[j]=0; 91 } 92 useds[i]=0; 93 } 94 return 0; 95 } 96 bool workp(){ //P 97 for(int i=1;i<=cnts;i++)if(!useds[i]){ 98 P9=shu[i]; 99 if(xj(T1,P9))continue; 100 if(xj(H4,P9))continue; 101 if(xj(U7,P9))continue; 102 useds[i]=1; 103 for(int j=1;j<=cnth;j++)if(!usedh[j]){ 104 P10=heng[j]; 105 if(xj(P10,T2))continue; 106 if(xj(P10,H3))continue; 107 if(xj(P10,H5))continue; 108 if(xj(P10,U6))continue; 109 if(xj(P10,U8))continue; 110 usedh[j]=1; 111 for(int k=1;k<=cnth;k++)if(!usedh[k]){ 112 P11=heng[k]; 113 if(xj(P11,T2))continue; 114 if(xj(P11,H3))continue; 115 if(xj(P11,H5))continue; 116 if(xj(P11,U6))continue; 117 if(xj(P11,U8))continue; 118 usedh[k]=1; 119 for(int l=1;l<=cnts;l++)if(!useds[l]){ 120 P12=shu[l]; 121 if(xj(T1,P12))continue; 122 if(xj(H4,P12))continue; 123 if(xj(U7,P12))continue; 124 useds[l]=1; 125 if(P9.d<P11.y&&P11.y==P12.d&&P12.d<P9.u&&P9.u==P10.y&&P10.y==P12.u&&P9.x==P10.l&&P10.l==P11.l&&P11.l<P10.r&&P10.r==P11.r&&P11.r==P12.x)return 1; 126 useds[l]=0; 127 } 128 usedh[k]=0; 129 } 130 usedh[j]=0; 131 } 132 useds[i]=0; 133 } 134 return 0; 135 } 136 bool workc(){ //C 137 for(int i=1;i<=cnts;i++)if(!useds[i]){ 138 C13=shu[i]; 139 if(xj(T1,C13))continue; 140 if(xj(H4,C13))continue; 141 if(xj(U7,C13))continue; 142 if(xj(P10,C13))continue; 143 if(xj(P11,C13))continue; 144 useds[i]=1; 145 for(int j=1;j<=cnth;j++)if(!usedh[j]){ 146 C14=heng[j]; 147 if(xj(C14,T2))continue; 148 if(xj(C14,H3))continue; 149 if(xj(C14,H5))continue; 150 if(xj(C14,U6))continue; 151 if(xj(C14,U8))continue; 152 if(xj(C14,P9))continue; 153 if(xj(C14,P12))continue; 154 usedh[j]=1; 155 for(int k=1;k<=cnth;k++)if(!usedh[k]){ 156 C15=heng[k]; 157 if(xj(C15,T2))continue; 158 if(xj(C15,H3))continue; 159 if(xj(C15,H5))continue; 160 if(xj(C15,U6))continue; 161 if(xj(C15,U8))continue; 162 if(xj(C15,P9))continue; 163 if(xj(C15,P12))continue; 164 usedh[k]=1; 165 if(C13.d==C15.y&&C15.y<C13.u&&C13.u==C14.y&&C13.x==C14.l&&C14.l==C15.l&&C15.l<C14.r&&C14.r==C15.r)return 1; 166 usedh[k]=0; 167 } 168 usedh[j]=0; 169 } 170 useds[i]=0; 171 } 172 return 0; 173 } 174 bool work(){ 175 sort(sp+1,sp+cntsp+1,cmpsp); 176 sort(sz+1,sz+cntsz+1,cmpsz); 177 for(int i=1;i<=cntsp;i++){ 178 if(!cnth||sp[i].y!=heng[cnth].y){ 179 cnth++;heng[cnth]=sp[i];continue; 180 } 181 if(sp[i].l>heng[cnth].r){ 182 cnth++;heng[cnth]=sp[i];continue; 183 } 184 heng[cnth].r=maxx(heng[cnth].r,sp[i].r); 185 } 186 for(int i=1;i<=cntsz;i++){ 187 if(!cnts||sz[i].x!=shu[cnts].x){ 188 cnts++;shu[cnts]=sz[i];continue; 189 } 190 if(sz[i].d>shu[cnts].u){ 191 cnts++;shu[cnts]=sz[i];continue; 192 } 193 shu[cnts].u=maxx(shu[cnts].u,sz[i].u); 194 } 195 if(cnth!=7)return 0; 196 if(cnts!=8)return 0; 197 if(!workt())return 0; 198 if(!workh())return 0; 199 if(!worku())return 0; 200 if(!workp())return 0; 201 if(!workc())return 0; 202 return 1; 203 } 204 int main(){ 205 // freopen("1.in","r",stdin); 206 // freopen("1.out","w",stdout); 207 int opt; 208 n=redd(); 209 for(int i=1;i<=n;i++){ 210 opt=redd(); 211 if(!opt){ 212 cntsp++; 213 sp[cntsp].l=redd();sp[cntsp].r=redd();sp[cntsp].y=redd(); 214 } 215 else{ 216 cntsz++; 217 sz[cntsz].d=redd();sz[cntsz].u=redd();sz[cntsz].x=redd(); 218 } 219 } 220 if(work())printf("Yes\n"); 221 else printf("No\n"); 222 return 0; 223 }

浙公网安备 33010602011771号