BNU-truchet tiling-Search

Source: Internet
Author: User

Question:

A pattern is provided for you to output the connected area of the pattern.

Solution:

This question is troublesome. It is a BFS or DFS search. It is troublesome to mark the direction. I decided not to try again as I watched Yan yufang finish.

#include <iostream>#include <cstdio>#include<queue>#include <cstring>#include<cmath>#include <algorithm>#include <iostream>const double PI=acos(-1)/4.0;const double eps=1e-8;using namespace std;int m,n,Q;int grid[101][101], map[444][444];int dir[4][2]={{0,0},{0,1},{1,1},{1,0}};int dir2[4][2]={{-1,0},{0,1},{1,0},{0,-1}};int vis[433][433];void init(){    for(int i=1;i<=m;i++)    {        for(int j=1;j<=n;j++)        {            if(grid[i][j]==1)            {                map[i*2][j*2]=0;                map[i*2-1][j*2]=2;                map[i*2][j*2-1]=-2;                map[i*2-1][j*2-1]=0;            }            else            {                map[i*2][j*2]=-1;                map[i*2-1][j*2]=0;                map[i*2][j*2-1]=0;                map[i*2-1][j*2-1]=1;            }        }    }}bool check(int x,int y){    if(x<=0||y<=0||x>2*m||y>2*n||vis[x][y])return false;    return true;}void dfs(int u,int v,int f,double &ans){    vis[u][v]=1;    if(map[u][v]!=0)    {        if(map[u][v]==1)        {            if(f==0)            {                ans+=1-PI;                if(check(u,v+1))                dfs(u,v+1,1,ans);            }            else if(f==1)            {                ans+=PI;                if(check(u-1,v))                dfs(u-1,v,0,ans);            }            else if(f==2)            {                ans+=PI;                if(check(u,v-1))                dfs(u,v-1,3,ans);            }            else if(f==3)            {                ans+=1-PI;                if(check(u+1,v))                dfs(u+1,v,2,ans);            }        }        else if(map[u][v]==-1)        {             if(f==0)            {                ans+=PI;                if(check(u,v+1))                dfs(u,v+1,1,ans);            }            else if(f==1)            {                ans+=1-PI;                if(check(u-1,v))                dfs(u-1,v,0,ans);            }            else if(f==2)            {                ans+=1-PI;                if(check(u,v-1))                dfs(u,v-1,3,ans);            }            else if(f==3)            {                ans+=PI;                if(check(u+1,v))                dfs(u+1,v,2,ans);            }        }         else if(map[u][v]==2)        {             if(f==0)            {                ans+=1-PI;                if(check(u,v-1))                dfs(u,v-1,3,ans);            }            else if(f==1)            {                ans+=1-PI;                if(check(u+1,v))                dfs(u+1,v,2,ans);            }            else if(f==2)            {                ans+=PI;                if(check(u,v+1))                dfs(u,v+1,1,ans);            }            else if(f==3)            {                ans+=PI;                if(check(u-1,v))                dfs(u-1,v,0,ans);            }        }        else if(map[u][v]==-2)        {             if(f==0)            {                ans+=PI;                if(check(u,v-1))                dfs(u,v-1,3,ans);            }            else if(f==1)            {                ans+=PI;                if(check(u+1,v))                dfs(u+1,v,2,ans);            }            else if(f==2)            {                ans+=1-PI;                if(check(u,v+1))                dfs(u,v+1,1,ans);            }            else if(f==3)            {                ans+=1-PI;                if(check(u-1,v))                dfs(u-1,v,0,ans);            }        }        return;    }    ans+=1.0;    int x,y;    for(int i=0;i<4;i++)    {        x=u+dir2[i][0];        y=v+dir2[i][1];        if(check(x,y))        {            dfs(x,y,i,ans);        }    }}void solve(int x,int y){    int u,v,flag=0;    double ans=0;    memset(vis,0,sizeof(vis));    for(int i=0;i<4;i++)    {        u=x+dir[i][0];        v=y+dir[i][1];        if(u<=0||u>2*m)continue;        if(v<=0||v>2*n)continue;        if(i%2==0&&abs(map[u][v])==2)flag=1;        if(i%2==1&&abs(map[u][v])==1)flag=1;        if(flag)break;        if(check(u,v))        {            dfs(u,v,i,ans);        }    }    if(flag)puts("0.0000");    else printf("%.4lf\n",ans+eps);}char str[140];int main(){    int T,a,b;    scanf("%d",&T);    for(int ca=1;ca<=T;ca++)    {        printf("Case %d:\n",ca);        scanf("%d%d\n",&m,&n);        for(int i=1;i<=m;i++)        {            scanf("%s",str+1);            for(int j=1;j<=n;j++)            {                grid[i][j]=str[j]-'0';            }        }        init();        scanf("%d",&Q);        while(Q--)        {            scanf("%d%d",&a,&b);            solve(a,b);        }    }    return 0;}

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.