• Codeforces Round #188 (Div. 1) B. Ants 暴力


    B. Ants

    Time Limit: 20 Sec

    Memory Limit: 256 MB

    题目连接

    http://codeforces.com/contest/317/problem/B

    Description

    It has been noted that if some ants are put in the junctions of the graphene integer lattice then they will act in the following fashion: every minute at each junction (x, y) containing at least four ants a group of four ants will be formed, and these four ants will scatter to the neighbouring junctions (x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1) — one ant in each direction. No other ant movements will happen. Ants never interfere with each other.

    Scientists have put a colony of n ants into the junction (0, 0) and now they wish to know how many ants will there be at some given junctions, when the movement of the ants stops.

    Input

    First input line contains integers n (0 ≤ n ≤ 30000) and t (1 ≤ t ≤ 50000), where n is the number of ants in the colony and t is the number of queries. Each of the next t lines contains coordinates of a query junction: integers xi, yi ( - 109 ≤ xi, yi ≤ 109). Queries may coincide.

    It is guaranteed that there will be a certain moment of time when no possible movements can happen (in other words, the process will eventually end).

    Output

    Print t integers, one per line — the number of ants at the corresponding junctions when the movement of the ants stops.

    Sample Input

    1 3
    0 1
    0 0
    0 -1

    Sample Output

    0
    1
    0

    HINT

    题意

    一个格子中的蚂蚁如果大于等于4个,这四个蚂蚁就会向四周扩散,扩散到(x+1,y),(x-1,y),(x,y+1),(x,y-1)这四个格子

    然后q次查询,每次查询(x,y)格子里面有多少个蚂蚁

    题解:

    蚂蚁最多为30000个,假设所有格子都是4只蚂蚁,那么也就最多30000/4=7500个格子

    然后我们直接就好啦~

    代码:

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    #define maxn 1000
    #define mod 10007
    #define eps 1e-9
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    int a[maxn][maxn];
    void solve(int x,int y)
    {
        if(a[x][y]<3)
            a[x][y]++;
        else
        {
            a[x][y]=0;
            solve(x+1,y);
            solve(x,y+1);
            solve(x,y-1);
            solve(x-1,y);
        }
    }
    int main()
    {
        int n=read(),q=read();
        for(int i=0;i<n;i++)
            solve(500,500);
        for(int i=0;i<q;i++)
        {
            int x=read(),y=read();
            if(x>100||x<-100||y>100||y<-100)
                cout<<"0"<<endl;
            else
                printf("%d
    ",a[x+500][y+500]);
        }
    }
  • 相关阅读:
    【JavaScript】实现队列Queue
    【Leetcode刷题篇】1.两数之和(JS)
    【48个原生JS网页小demo】1.信息切换
    【JavaScript】原生实现call bind apply
    【JavaScript】Interview(精简版)
    【JavaScript】4种常见的内存泄露
    【JavaScript】原型和原型链
    论自作音乐播放器涉及知识点总结
    Android横竖屏切换继续播放视频
    Android上传头像代码,相机,相册,裁剪
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4574857.html
Copyright © 2020-2023  润新知