码迷,mamicode.com
首页 > 其他好文 > 详细

练习题目 3 Game on Paper

时间:2016-04-28 22:33:49      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:

 Game on Paper
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
 

Description

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.

Sample Input

Input
4 11 
1 1
1 2
1 3
2 2
2 3
1 4
2 4
3 4
3 2
3 3
4 1
Output
10
Input
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 1
Output
-1

简单题意:
   就是在N * N的中,一共可以添m个点,,问最少填几个点的时候可以形成3*3的正方形。。。
思路分析:
  可以慢慢填,填一个点判断一下,填一个点判断一下。。求解
# include <iostream>
# include <fstream>
# include <cstdio>
# include <cstring>
using namespace std;
const int MAX = 1001;
int a[MAX][MAX];
int m, n;
int dx[]={1,0,-1,0,1,1,-1,-1,0};
int dy[]={0,1,0,-1,1,-1,1,-1,0};

bool f(int x, int y)
{
    int tx, ty, sum = 0;
    for(int i = 0; i < 9; i++)
    {
        tx = x + dx[i];
        ty = y + dy[i];
        if(tx >= 0 && tx < m && ty >= 0 && ty < m)
        {
            if(a[tx][ty] == 9)
            return true;
            a[tx][ty]++;
            if(a[tx][ty] == 9)
            return true;
        }
    }
    return false;
}
int main()
{
   // fstream cin("aaa.txt");
    cin >> m >> n;

    memset(a, 0, sizeof(a));
    int x, y, flag = 0;
    for(int i = 0; i < n; i++)
    {
        cin >> x >> y;
        if(f(x, y))
        {
            cout << i + 1 << endl;
            flag = 1;
            break;
        }
    }
    //cout << flag << endl;
    if(!flag)
    cout << "-1" << endl;


return 0;
}

 


练习题目 3 Game on Paper

标签:

原文地址:http://www.cnblogs.com/lyf-acm/p/5444434.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!