Codeforces Round #292 (Div. 1) B. Drazil and Tiles (topological sort)

Source: Internet
Author: User

Title Address: Codeforces 292 B
Use the queue to maintain a point with a degree of 1, that is, a point that can be uniquely determined, and then each time you find V1,v2, and v2 to update the points connected to it, if the updated point is 1, join the queue. If there is a final "." , stating that no solution or solution is unique.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>using namespace STD;#pragma COMMENT (linker, "/stack:102400000,102400000")#define LL __int64#define PI ACOs ( -1.0)Const intMod=1e9+7;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;intjx[]={0,0,1,-1};intjy[]={1,-1,0,0};intd[5002][5002], N, m;Charmp[5002][5002];structnode{intx, y;}; queue<node>QBOOLTopo () {intI, J, K, X, Y, A, B, H; Node F1, F2; while(!q.empty ())                {F1=q.front (); Q.pop ();if(!d[f1.x][f1.y])Continue; d[f1.x][f1.y]=0; for(k=0;k<4; k++) {x=f1.x+jx[k]; Y=F1.Y+JY[K];if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]=='. ') {d[x][y]--;if(k==0) {mp[f1.x][f1.y]=' < '; mp[x][y]=' > '; }Else if(k==1) {mp[f1.x][f1.y]=' > '; mp[x][y]=' < '; }Else if(k==2) {mp[f1.x][f1.y]=' ^ '; mp[x][y]=' V '; }Else{mp[f1.x][f1.y]=' V '; mp[x][y]=' ^ '; } for(h=0;h<4; h++) {a=x+jx[h]; B=Y+JY[H];if(a>=0&&a<n&&b>=0&&b<m&&mp[a][b]=='. ') {d[a][b]--;if(d[a][b]==1) {f2.x=a;f2.y=b;                                                Q.push (F2);        }                                        }                                }                        }                } } for(i=0; i<n;i++) { for(j=0; j<m;j++) {if(mp[i][j]=='. ')return 0; }        }return 1;}intMain () {intI, J, X, Y, cnt, k, ans; while(scanf("%d%d", &n,&m)!=eof) { for(i=0; i<n;i++) {scanf('%s ', Mp[i]); } node F; for(i=0; i<n;i++) { for(j=0; j<m;j++) {if(mp[i][j]!='. ')Continue; Cnt=0; for(k=0;k<4; k++) {x=i+jx[k]; Y=J+JY[K];if(x>=0&&x<n&&y>=0&&y<m&&mp[x][y]=='. ') cnt++; } d[i][j]=cnt;if(cnt==1) {f.x=i;f.y=j;                                Q.push (f); }                        }                }if(Topo ()) { for(i=0; i<n;i++) {printf("%s\n", Mp[i]); }                }Else puts("Not unique"); }return 0;}

Codeforces Round #292 (Div. 1) B. Drazil and Tiles (topological sort)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.