Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓撲排序),

來源:互聯網
上載者:User

Codeforces Round #292 (Div. 1) B. Drazil and Tiles(拓撲排序),

題目地址:codeforces 292 B
用隊列維護度數為1的點,也就是可以唯一確定的點,然後每次找v1,v2,並用v2來更新與之相連的點,如果更新後的點度數為1,就排入佇列。若最後還有為”.”的,說明無解或解不唯一。
代碼如下:

#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 int mod=1e9+7;const int INF=0x3f3f3f3f;const double eqs=1e-9;int jx[]={0,0,1,-1};int jy[]={1,-1,0,0};int d[5002][5002], n, m;char mp[5002][5002];struct node{        int x, y;};queue<node>q;bool topo(){        int i, 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;}int main(){        int i, 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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.