题目衔接:http://codeforces.com/contest/203/problem/B
B. Game on Paper
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
One not particularly beautiful evening Valera got very bored. To amuse himself a little bit, he found the following game.
He took a checkered white square piece of paper, consisting of n × n cells. After that, he started to paint the white cells black one after the other. In total he painted m different cells on the piece of paper. Since Valera was keen on everything square, he wondered, how many moves (i.e. times the boy paints a square black) he should make till a black square with side 3 can be found on the piece of paper. But Valera does not know the answer to this question, so he asks you to help him.
Your task is to find the minimum number of moves, till the checkered piece of paper has at least one black square with side of 3. Otherwise determine that such move does not exist.
Input
The first line contains two integers n and m (1 ≤ n ≤ 1000, 1 ≤ m ≤ min(n·n, 105)) — the size of the squared piece of paper and the number of moves, correspondingly.
Then, m lines contain the description of the moves. The i-th line contains two integers xi, yi (1 ≤ xi, yi ≤ n) — the number of row and column of the square that gets painted on the i-th move.
All numbers on the lines are separated by single spaces. It is guaranteed that all moves are different. The moves are numbered starting from 1 in the order, in which they are given in the input. The columns of the squared piece of paper are numbered starting from 1, from the left to the right. The rows of the squared piece of paper are numbered starting from 1, from top to bottom.
Output
On a single line print the answer to the problem — the minimum number of the move after which the piece of paper has a black square with side 3. If no such move exists, print -1.
input
Copy
4 11 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 3 3 4 1output
Copy
10input
Copy
4 12 1 1 1 2 1 3 2 2 2 3 1 4 2 4 3 4 3 2 4 2 4 1 3 1output
-1
题目大意:给你n*n的网格,再给你m个点,问你需要多少个点才能达到3*3的网格
思路:先存进二维数组,并给每个坐标附上一个序号,再暴力查找到一个3*3的点并找到序号最大的那个,最后更新序号小的
代码:
/*
题目大意:给你n*n的网格,再给你m个点,问你需要多少个点才能达到3*3的网格
思路:先存进二维数组,并给每个坐标附上一个序号,再暴力查找到
一个3*3的并找到序号最大的那个,最后总的序号最大找最小
*/
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<cmath>
#include<string>
#include<cstdio>
#include <vector>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll unsigned long long
#define inf 0x3f3f3f
#define esp 1e-8
#define bug {printf("mmp\n");}
#define mm(a,b) memset(a,b,sizeof(a))
#define T() int test,q=1;scanf("%d",&test); while(test--)
#define Test() {freopen("F:\\test.in","r",stdin);freopen("F:\\test1.out","w",stdout);}
const int maxn=2e4+10;
const double pi=acos(-1.0);
const int N=1010;
const ll mod=1e9+7;
int a[N][N];
int book[N][N];
bool judege(int x,int y)
{
if(book[x-1][y-1]&&book[x-1][y]&&book[x-1][y+1]
&&book[x][y-1]&&book[x][y]&&book[x][y+1]
&&book[x+1][y-1]&&book[x+1][y]&&book[x+1][y+1])
return true;
return false;
}
int main()
{
mm(a,0);
mm(book,0);
int n,m,flag=0;
scanf("%d%d",&n,&m);
for(int i=0; i<m; i++)
{
int x,y;
cin>>x>>y;
a[x][y]=i+1;
book[x][y]=1;
}
int minn=inf,maxx=-inf;
for(int i=1; i<=n; i++)
{
for(int j=1; j<=n; j++)
{
if(judege(i,j)&&i-1>0&&j-1>0&&i+1<=n&&j+1<=n)///如果这个位置存在
{
flag=1;
///找到当前序号最大的
maxx=a[i][j];
maxx=max(maxx,a[i-1][j-1]);
maxx=max(maxx,a[i-1][j]);
maxx=max(maxx,a[i-1][j+1]);
maxx=max(maxx,a[i][j-1]);
maxx=max(maxx,a[i][j+1]);
maxx=max(maxx,a[i+1][j-1]);
maxx=max(maxx,a[i+1][j]);
maxx=max(maxx,a[i+1][j+1]);
minn=min(minn,maxx);
}
}
}
if(flag)
printf("%d\n",minn);
else
printf("-1\n");
return 0;
}