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 */