Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

【My way】一个很水,很简单的做法

Posted by WilliamACM at 2012-12-29 17:24:35 on Problem 3338
每个方格都可以向上下左右四个方向走,但因为有矩形的原因,矩形边界的方格向某个方向扩展被禁止了,由此,把所有方格可以扩展的方向预处理,然后BFS,一块一块的找出可以练成一片的区域个数。
代码;

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int a[22][22][4];
bool v[22][22];
int w,h;
void do_h(int y,int x1,int x2)
{
    for(int i=x1;i<x2;i++)
    {
        a[y][i][2]=1;
        if(y)a[y-1][i][0]=1;
    }
}
void do_v(int x,int y1,int y2)
{
    for(int i=y1;i<y2;i++)
    {
        a[i][x][3]=1;
        if(x)a[i][x-1][1]=1;
    }
}
struct node
{
    int x,y;
};
queue<node> q;
int mx[4]={1,0,-1,0};
int my[4]={0,1,0,-1};
void bfs(int x,int y)
{
    v[x][y]=1;
    node t;t.x=x,t.y=y;
    q.push(t);
    while(!q.empty())
    {
        node now=q.front();q.pop();
        for(int i=0;i<4;i++)
        if(a[now.x][now.y][i]==0)
        {
            node next;
            next.x=now.x+mx[i];
            next.y=now.y+my[i];
            if(!v[next.x][next.y])
            {
                v[next.x][next.y]=1;
                q.push(next);
            }
        }
    }
}
void init()
{
    memset(a,0,sizeof(a));
    do_h(0,0,w);do_h(h,0,w);
    do_v(0,0,h);do_v(w,0,h);
    int m;
    scanf("%d",&m);

    while(m--)
    {
        int x1,y1,x2,y2;
        int X1,Y1,X2,Y2;
        scanf("%d%d%d%d",&X1,&Y1,&X2,&Y2);
        x1=min(X1,X2);x2=max(X1,X2);
        y1=min(Y1,Y2);y2=max(Y1,Y2);
        do_h(y1,x1,x2);do_h(y2,x1,x2);
        do_v(x1,y1,y2);do_v(x2,y1,y2);
    }
    int ans=0;
    memset(v,0,sizeof(v));
    for(int i=0;i<w;i++)
    for(int j=0;j<h;j++)
    if(!v[j][i])
    {
        ans++;
        bfs(j,i);
    }
    printf("%d\n",ans);
}
int main()
{
  //  freopen("in.txt","r",stdin);
    while(scanf("%d%d",&h,&w),w+h)
    {
        init();
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator