Codeforces 508A Pasha and Pixels


/*
User ID: Tanmoy_Datta
Link : http://codeforces.com/contest/508/problem/A
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 120
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 100000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;
bool ara[1010][1010];
bool test=0,test2=0;
int ans=0;
int dirx1[]= {0,0,-1,-1};
int diry1[]= {0,-1,0,-1};

int dirx2[]= {0,0,-1,-1};
int diry2[]= {0,1,0,1};

int dirx3[]= {0,0,1,1};
int diry3[]= {0,1,0,1};

int dirx4[]= {0,0,1,1};
int diry4[]= {0,-1,0,-1};

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    ll n,m,k;

    ms(ara,0);
    cin>>n>>m>>k;
    for(int i=1; i<=k; i++)
    {
        int a,b;
        cin>>a>>b;
        ara[a][b]=true;
        if(i>=4 && !test2)
        {
            int cnt=0;
            {
                for(int j=0; j<4; j++)
                {
                    int c=a+dirx1[j];
                    int d=b+diry1[j];
                    if(c>0 && c<=n && d>0 && d<=m)
                    {
                        if(ara[c][d])
                            cnt++;
                    }
                }
                if(cnt!=4)
                {
                    cnt=0;
                    for(int j=0; j<4; j++)
                    {
                        int c=a+dirx2[j];
                        int d=b+diry2[j];
                        if(c>0 && c<=n && d>0 && d<=m)
                        {
                            if(ara[c][d])
                                cnt++;
                        }
                    }
                }

                if(cnt!=4)
                {
                    cnt=0;
                    for(int j=0; j<4; j++)
                    {
                        int c=a+dirx3[j];
                        int d=b+diry3[j];
                        if(c>0 && c<=n && d>0 && d<=m)
                        {
                            if(ara[c][d])
                                cnt++;
                        }
                    }
                }

                if(cnt!=4)
                {
                    cnt=0;
                    for(int j=0; j<4; j++)
                    {
                        int c=a+dirx4[j];
                        int d=b+diry4[j];
                        if(c>0 && c<=n && d>0 && d<=m)
                        {
                            if(ara[c][d])
                                cnt++;
                        }
                    }
                }
            }
            if(cnt>=4 && !test2)
            {
                test=1;
                ans=i;
                test2=1;
            }
        }
    }
    if(test)
    {
        cout<<ans<<endl;
        return 0;
    }
    cout<<0<<endl;
    return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s