hdu-6665 暴力模擬或離散化座標+bfs

 

題目鏈接

題意:給你兩個矩形的左下和右上座標,問這兩個矩形將平面分成了幾個區域。

思路:1.可能存在的狀態有2 3 4 5 6直接模擬座標狀態輸出對應值就行了,情況有點多,可能會出現考慮不完全。

 

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <cmath>
using namespace std;
int x[5], y[5];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
//        int mx=0, my=0, mix=1000000001,miy=1000000001;
        for(int i=1; i<=4; i++)
            scanf("%d%d",&x[i],&y[i]);
        if((max(x[2],x[4])-min(x[1], x[3]) >= x[2]-x[1]+x[4]-x[3]) || (max(y[2],y[4])-min(y[1], y[3]) >= y[2]-y[1]+y[4]-y[3]))
            puts("3");
        else if(x[1]==x[3] && x[2]==x[4] && y[1]==y[3] && y[2]==y[4]) puts("2");
        else if((x[3]<=x[1] && y[2]<=y[4] && x[2] <= x[4] && y[3]<=y[1]) || (x[3]>=x[1] && y[2]>=y[4] && x[2] >= x[4] && y[3]>=y[1]))
        {
            if((x[1]==x[3] && x[2] == x[4] && y[1]!=y[3] && y[2]!=y[4])||(y[1]==y[3] && y[2] == y[4] && x[1]!=x[3] && x[2]!=x[4]))
                puts("4");
            else puts("3");
        }
        else{
            if((y[1]<y[3] && y[2]>y[4] && x[3]<x[1] && x[4]>x[2]) ||(y[1]>y[3] && y[2]<y[4] && x[3]>x[1] && x[4]<x[2])) puts("6");
            else if((y[1]==y[3] && y[4]>y[2] && x[1]<x[3] && x[2]>x[4])||(y[1]==y[3] && y[4]<y[2] && x[1]>x[3] && x[2]<x[4]))
                puts("5");
            else if((y[2]==y[4] && y[1]>y[3] && x[1]<x[3] && x[2]>x[4])||(y[2]==y[4] && y[1]<y[3] && x[1]>x[3] && x[2]<x[4]))
                puts("5");
            else if((y[2]<y[4] && y[1]>y[3] && x[1]<x[3] && x[2]==x[4])||(y[2]>y[4] && y[1]<y[3] && x[1]>x[3] && x[2]==x[4]))
                puts("5");
            else if((y[2]>y[4] && y[1]<y[3] && x[1]==x[3] && x[2]<x[4])||(y[2]<y[4] && y[1]>y[3] && x[1]==x[3] && x[2]>x[4]))
                puts("5");
            else puts("4");
        }
    }
    return 0;
}

           2. 座標值會很大,所以將其離散到10*10以內,標記矩形邊,bfs10*10,輸出bfs次數(離散化座標後乘2是的矩形內存在0)

#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#define mems(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
struct node
{
    ll x,y;
} s[5];
int vis[14][14];
int go[4][2]={1,0,-1,0,0,1,0,-1};
vector<ll>X,Y;
void Hash(node &s)
{      //離散座標從1開始
    s.x=lower_bound(X.begin(),X.end(),s.x)-X.begin()+1;
    s.y=lower_bound(Y.begin(),Y.end(),s.y)-Y.begin()+1;
}
void bfs(int x,int y)
{

    queue<node>q;
    node now,to;
    now.x=x;
    now.y=y;
    vis[x][y]=1;
    q.push(now);
    while(!q.empty())
    {
        now=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            to.x=now.x+go[i][0];
            to.y=now.y+go[i][1];
            if(to.x<0||to.y<0||to.x>10||to.y>10||vis[to.x][to.y])
                continue;
            vis[to.x][to.y]=1;
            q.push(to);
        }
    }
}
int main()
{

    int t;
    scanf("%d",&t);
    while(t--)
    {
        mems(vis,0);
        X.clear();
        Y.clear();
        for(int i=1; i<=4; i++)
        {
            cin>>s[i].x>>s[i].y;
            X.push_back(s[i].x);
            Y.push_back(s[i].y);
        }
        sort(X.begin(),X.end());//排序
        sort(Y.begin(),Y.end());
        X.erase(unique(X.begin(),X.end()),X.end());//去重加刪除重複的
        Y.erase(unique(Y.begin(),Y.end()),Y.end());
        for(int i=1; i<=4; i++)
            Hash(s[i]);
        for(int i=1;i<=4;i++)//座標乘2
        {
            s[i].x*=2;
            s[i].y*=2;
        }

        for(int i=s[1].x; i<=s[2].x; i++)//矩形邊標記1
            vis[i][s[1].y]=vis[i][s[2].y]=1;
        for(int i=s[3].x; i<=s[4].x; i++)
            vis[i][s[3].y]=vis[i][s[4].y]=1;
        for(int i=s[1].y; i<=s[2].y; i++)
            vis[s[1].x][i]=vis[s[2].x][i]=1;
        for(int i=s[3].y; i<=s[4].y; i++)
            vis[s[3].x][i]=vis[s[4].x][i]=1;
        ll ans=0;
        for(int i=0; i<=10; i++)
            for(int j=0; j<=10; j++)
            {
                if(!vis[i][j])
                {
                    ans++;
                    bfs(i,j);
                }
            }
        printf("%lld\n",ans);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章