Codeforces Round #288 (Div. 2) A. Pasha and Pixels

A. Pasha and Pixels

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Pasha loves his phone and also putting his hair up... But the hair is now irrelevant.

Pasha has installed a new game to his phone. The goal of the game is following. There is a rectangular field consisting of n row with mpixels
in each row. Initially, all the pixels are colored white. In one move, Pasha can choose any pixel and color it black. In particular, he can choose the pixel that is already black, then after the boy‘s move the pixel does not change, that is, it remains black.
Pasha loses the game when a 2?×?2 square consisting of black pixels is formed.

Pasha has made a plan of k moves, according to which he will paint pixels. Each turn in his plan is represented as a pair of numbers iand j,
denoting respectively the row and the column of the pixel to be colored on the current move.

Determine whether Pasha loses if he acts in accordance with his plan, and if he does, on what move the 2?×?2 square consisting of black pixels is formed.

Input

The first line of the input contains three integers n,?m,?k (1?≤?n,?m?≤?1000, 1?≤?k?≤?105) —
the number of rows, the number of columns and the number of moves that Pasha is going to perform.

The next k lines contain Pasha‘s moves in the order he makes them. Each line contains two integers i and j (1?≤?i?≤?n, 1?≤?j?≤?m),
representing the row number and column number of the pixel that was painted during a move.

Output

If Pasha loses, print the number of the move when the 2?×?2 square consisting of black pixels is formed.

If Pasha doesn‘t lose, that is, no 2?×?2 square consisting of black pixels is formed during the given k moves,
print 0.

Sample test(s)

input

2 2 4
1 1
1 2
2 1
2 2

output

4

input

2 3 6
2 3
2 2
1 3
2 2
1 2
1 1

output

5

input

5 3 7
2 3
1 2
1 1
4 1
3 1
5 3
3 2

output

0

题意:给定n*m的空白方格,进行k次涂色,将(x,y)处的方格涂成黑色,判断第几次涂色能形成2*2的黑色方格,若不能涂成2*2的方格,输出0。

涂(x,y)时总共就四种情况,四个 if 就能解决,代码太丑。。。。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
typedef long long ll;
using namespace std;

int mp[maxn][maxn];
int n,m,k;

bool isok(int x,int y)
{
    if (x>=1&&x<=n&&y>=1&&y<=m&&mp[x][y])
        return true;
    return false;
}

int main()
{
    while (~scanf("%d%d%d",&n,&m,&k))
    {
        int x,y;
        int ans=0;
        int x1,y1,x2,y2,x3,y3;
        memset(mp,0,sizeof(mp));
        for (int i=1;i<=k;i++)
        {
            scanf("%d%d",&x,&y);
            if (mp[x][y])
                continue;
            x1=x-1,y1=y-1;
            x2=x-1,y2=y;
            x3=x,y3=y-1;
            if (isok(x1,y1)&&isok(x2,y2)&&isok(x3,y3))
            {
                ans=i;
                break;
            }
            x1=x-1,y1=y;
            x2=x-1,y2=y+1;
            x3=x,y3=y+1;
            if (isok(x1,y1)&&isok(x2,y2)&&isok(x3,y3))
            {
                ans=i;
                break;
            }
            x1=x,y1=y-1;
            x2=x+1,y2=y-1;
            x3=x+1,y3=y;
            if (isok(x1,y1)&&isok(x2,y2)&&isok(x3,y3))
            {
                ans=i;
                break;
            }
            x1=x,y1=y+1;
            x2=x+1,y2=y;
            x3=x+1,y3=y+1;
            if (isok(x1,y1)&&isok(x2,y2)&&isok(x3,y3))
            {
                ans=i;
                break;
            }
            mp[x][y]=1;
        }
        if (ans<k&&ans)
        {
            k=k-ans;
            while (k--)
                scanf("%d%d",&x,&y);
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
2 3 6
2 3
2 2
1 3
2 2
1 2
1 1
2 2 4
1 1
1 2
2 1
2 2
5 3 7
2 3
1 2
1 1
4 1
3 1
5 3
3 2
*/
时间: 2024-10-09 01:36:45

Codeforces Round #288 (Div. 2) A. Pasha and Pixels的相关文章

Codeforces Round #249 (Div. 2) B. Pasha Maximizes

看到题目的时候,以为类似插入排序,比较第i个元素和第i-1个元素, 如果第i个元素比第i-1个元素小,则不交换 如果第i个元素比第i-1个元素大,则交换第i个元素和第i-1个元素 继续比较第i-1个元素与前一个元素,直到前一个元素大为止 交换元素次大于等于k则停止 但对测试用例 1234 3 则出现问题,如果按照上述方法得到答案为3214, 但正确答案是4321,直接将第4个元素往前交换 故在上面基础上改进得 当扫描第i个元素时,则要取出[i,i+k+1)之间的最大元素,然后将最大元素往前交换,

Codeforces Round #288 (Div. 2)

A. Pasha and Pixels 题目大意:给出n*m的棋盘,初始为全白,每次在上面使一个格子变黑,问多少次的时候使得棋盘上出现2×2的黑格 思路:模拟 1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<queue> 5 #define maxn 900000 6 #define LL long long 7 using namespace std; 8 boo

Codeforces Round #288 (Div. 2) 待续

A. Pasha and Pixels ( 暴力 ) 题意:给一个n*m的矩阵染色(初始为全白),如果在k步之内染出一个2*2的矩阵,输出最小步数,否则输出0 分析:brute force #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int M[ 1010 ][ 1010 ]; int step, r, c, k

Codeforces Round #297 (Div. 2)B Pasha and String

B. Pasha and String time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Pasha got a very beautiful string s for his birthday, the string consists of lowercase Latin letters. The letters in the

Codeforces Round #288 (Div. 2)B(字符串)

题意:交换两个数,使得交换后的数是偶数且尽可能大: KEY:分情况,1末尾数为偶数,即找到比它小的偶数交换,假如没有比它小的偶数,不用交换.2末尾数是奇数,再分情况,<1>全都是奇数(这个可以在一开始就判断掉),即输出-1,<2>有一个偶数,无论如何谁大谁小都要交换,<3>全部偶数都比奇数大,从n - 2(n是字符串长度)开始找到一个偶数交换即可,<4>如果有一些偶数比末尾数大,有些比它小,即从0开始找到比奇数小的那个偶数交换即可.其实是分类讨论.(有更好的

Codeforces Round #288 (Div. 2) ABCDE

A题 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cmath> 6 #include <vector> 7 #include <map> 8 #include <queue> 9 10 using namespace std; 11 12 #define LL

Codeforces Round #311 (Div. 2) B. Pasha and Tea

[题目链接]click here~~ [题目大意]给你n个boy,n个girl ,然后W表示茶壶的最大容量,然后n个茶杯,每个都有不同的容量,要求boy的茶杯里的茶水是girl的两倍,且boy和boy容量一样,girl和girl 容量一样,问如何倒茶,最大化总的茶量 [解题思路]这道题本来很简单,第一次读题没读明白,以为每个茶杯的茶水都倒满了,然后一想不就是拿最大的计算吗.一交,直接WA,然后仔细读题,发现,每个茶杯的茶水可以选择的倒(坑~啊),那么既然boy和boy,girl和girl的茶水一

Codeforces Round #288 (Div. 2) A,B,C,D,E

A:一个一个点向图里面加,判断其所在的位置与其他的点是否可以构成小矩形就可以了. B:贪心,如果前面的偶数有比他小的就找到一个最靠前的交换,如果前面的偶数都比它小,就找一个最靠后的交换. C:贪心,把蜡烛尽可能的放在恶魔,来之前,这样可以充分利用蜡烛的时间,模拟一下,不停地向前方就可以了.如果蜡烛时间小于需要的数目,一定不可以. const int maxn = 2010; int vis[maxn]; int num[maxn]; int main() { int m, t, r; while

Codeforces Round #288 (Div. 2) B. Anton and currency you all know

B. Anton and currency you all know time limit per test 0.5 seconds memory limit per test 256 megabytes input standard input output standard output Berland, 2016. The exchange rate of currency you all know against the burle has increased so much that