组队赛#1 解题总结 ZOJ 3803 YY's Minions (DFS搜索+模拟)

YY‘s Minions


Time Limit: 2 Seconds     
Memory Limit: 65536 KB



Despite YY‘s so much homework, she would like to take some time to play with her minions first.

YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. We use 0(the digit) to represent that it is asleep, and 1 for awake.Also, we define the minions who are around a minionclosest
in one of the eight directions its neighbors.And every minute every minion will change its status by the following specific rules:

  • If this minion is awake, and the number of its neighbors who are awake is less than 2, this minion will feel lonely and turn to asleep.
  • If this minion is awake, and the number of its neighbors who are awake is more than 3, this minion will turn to asleep for it will feel too crowded.
  • If this minion is awake, and the number of its neighbors who are awake is exactly 2 or 3, this minion will keep being awake and feel very happy.
  • If this minion is asleep, and the number of its neighbors who are awake is exactly 3, this minion will wake up because of the noise.Note that all changes take place at the same time at the beginning of a specific minute.

    Also, some minions will get bored and leave this silly game. We use ‘X‘s to describe them.We suppose that a minion would leave afterT minutes. It will leave at the end of the Tth minute. Its status is considered during the change at
    the beginning of the Tth minute, and should be ignored after that.Of course, one minion will not leave twice!

    YY is a girl full of curiosity and wants to know every minion‘s status after
    F
    minutes. But you know she is weak and lazy! Please help this cute girl to solve this problem :)

    Input

    There are multiple test cases.

    The first line contains the number of test cases Q. 1<=Q<=100.

    For each case, there are several lines:

    The first line contains four integers N, M, F,
    K
    . K means the number of leaving messages. 1<=N,
    M
    <=50, 1<=F<=1000, 1<=K<=N*M.

    Next N lines are the matrix which shows the initial status of each minion. Each line containsM chars. We guarantee that ‘X‘ wouldn‘t appear in initial status matrix.

    And next K lines are the leaving messages. Each line contains three integersTi,
    Xi, Yi, They mean the minion who is located in (Xi,Yi) will leave the game at the end of the
    Tith minutes. 1<=Ti<=F, 1<=Xi<=N, 1<=Yi<=M.

    Output

    For each case, output N lines as a matrix which shows the status of each minion afterF minutes.

    Sample Input

    2
    3 3 2 1
    101
    110
    001
    1 2 2
    5 5 6 3
    10111
    01000
    00000
    01100
    10000
    2 3 3
    2 4 1
    5 1 5
    

    Sample Output

    010
    1X0
    010
    0000X
    11000
    00X00
    X0000
    00000
    

    Hint

    For case 1:
    
    T=0, the game starts
    101
    110
    001
    ---------------
    at the beginning of T=1, a change took place
    100
    101
    010
    ---------------
    at the end of T=1 (the minion in (2,2) left)
    100
    1X1
    010
    ---------------
    at the beginning of T=2, a change took place
    010
    1X0
    010
    ---------------
    at the end of T=2 (nothing changed for no minion left at T=2)
    010
    1X0
    010
    
  • 链接:click here~~

    题意:一个矩阵中有 n*m个宠物,每一个宠物都有一个状态, 1代表醒着的,0代表睡着的, ’X‘代表的是将要离开的,如果这个宠物(醒着的)的周围醒着的个数>3 || <2它就会睡着, 如果这个宠物(睡着的)的周围醒着的个数==3就会醒来,  每一分钟都会有变换一个状态! 其中会有些宠物会在给定的时间内离开。求解给定t分钟后,所有宠物的状态

    【解题思路】:DFS 搜索八个方向,然后模拟一下,注意要注意预处理的dir数组,看清题目要求,动物睡眠状态跟周围的动物数目关系。做完之后感觉就是,搜索部分容易实现,但是如果有多个宠物将要离开,而且在不同的时间段,而且每个时间段,都会影响其周围的宠物的状态!这个部分就要仔细一点了,既然多个时间段,我们把所有时间段从小到大排列一遍!,依次处理,同步DFS搜索,更新状态!,具体看代码了

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    char mapp[1005][1005];
    char runq[1005][1005];
    int dir[8][2]= {0,-1,-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1};//这里数组开成[8][4],WA了一次!
    int N,M,F,K;
    int t,t1,x1,y1;
    int i,j,tot=1;
    struct node
    {
        int t,b,xx;
        bool operator <(const node &c) const
        {
            return t<c.t;
        }
    } runway[1005];
    bool judge(int x,int y)
    {
        if(x>0&&x<N&&y>0&&y<M) return true;
        else return false;
    }
    void dfs()
    {
        int awake=0,asleep;
        for(int i=0; i<N; i++)
            for(int j=0; j<M; j++)
            {
                int maxx=0;
                for(int p=0; p<8; p++)
                {
                    int dx=i+dir[p][0];
                    int dy=j+dir[p][1];
                      if(dx<0||dx>=N||dy<0||dy>=M) continue;
                     if(mapp[dx][dy]=='1') maxx++;
    //                if(judge(dx,dy))
    //                {
    //                    if(mapp[dx][dy]=='1') maxx++;
    //                }
                }
                runq[i][j]=mapp[i][j];
                if(mapp[i][j]=='1')
                {
                    if(maxx>3||maxx<2) runq[i][j]='0';
                    else runq[i][j]='1';
                }
                else if(mapp[i][j]=='0')
                {
                    if(maxx==3) runq[i][j]='1';
                }
            }
        for(int i=0; i<N; i++)
            for(int j=0; j<M; j++)
            {
                mapp[i][j]=runq[i][j];
            }
    }
    int main()
    {
        cin>>t;
        while(t--)
        {
            cin>>N>>M>>F>>K;
            for(i=0; i<N; i++)
                scanf("%s",mapp[i]);
            for(i=0; i<K; i++)
            {
                cin>>runway[i].t>>runway[i].b>>runway[i].xx;
                runway[i].b--;
                runway[i].xx--;
            }
            sort(runway,runway+K);
            for(i=1,j=0; i<=F; i++)    //注意动态更新!
            {
                dfs();
                while(j<K&&runway[j].t==i)
                {
                    mapp[runway[j].b][runway[j].xx]='X';
                    j++;
                }
            }
            for(i=0; i<N; i++)
                printf("%s\n",mapp[i]);
        }
        return 0;
    }
    

    组队赛#1 解题总结 ZOJ 3803 YY's Minions (DFS搜索+模拟)

    时间: 2024-12-13 06:42:14

    组队赛#1 解题总结 ZOJ 3803 YY's Minions (DFS搜索+模拟)的相关文章

    ZOJ 3804 YY&#39;s Minions(简单模拟)

    YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake or asleep. W

    ZOJ 3804 YY&#39;s Minions (简单模拟)

    1 /* 2 题意:一个矩阵中有 n*m个宠物,每一个宠物都有一个状态, 1醒着的,0睡着的 3 X离开的!如果这个宠物(醒着的)的周围醒着的个数>3 || <2它就会睡着, 4 如果这个宠物(睡着的)的周围醒着的个数==3就会醒来! 5 每一分钟都会有变换一个状态! 6 其中会有些宠物会在给定的时间内离开! 7 */ 8 #include<iostream> 9 #include<cstring> 10 #include<cstdio> 11 #inclu

    组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)

    Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is playing a game, and this time the game is all about the absolute value! Alice has N different positive integers, and each number is not greater than N.

    ZOJ Problem Set - 3804 YY&#39;s Minions

    学习:换一个角度考虑问题.YY's Minions Time Limit: 2 Seconds      Memory Limit: 65536 KB Despite YY's so much homework, she would like to take some time to play with her minions first. YY lines her minions up to an N*M matrix. Every minion has two statuses: awake

    ZOJ 1008 Gnome Tetravex (DFS + 剪枝)

    Gnome Tetravex 题目:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=8 题意:有N*N个方格,每个方格分为上下左右四个部分,每个部分填数字.现在要求重排方块,使得每两个有边相连的方块对应的数字相同. 思路:就是一个简单的搜索,我想了个剪枝,将上下左右四个方向上每个数字对应的是哪几个方块记录下来,但是这个剪枝并没有起很大的作用,还是T了.后来才发现,如果有很多个方块是相同的,会重复搜索,所以需要将相同的方块一起处

    ZOJ 3631 Watashi&#39;s BG DFS

    J - Watashi's BG Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Practice ZOJ 3631 Appoint description: Description Watashi is the couch of ZJU-ICPC Team and he is very kind hearted. In ZJU-ICPC summer trainin

    ZOJ 3644 Kitty&#39;s Game dfs,记忆化搜索,map映射 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的因数,所以不会形成环,只需从1直接走,走到n即可. 但是如果这样的话时空复杂度就都是nk,明显不满足题意,而这个时候我们可以想到,每个状态都必然是k的约数,(点数不是k的约数的节点不在路上,可以无视),而约数的个数也就k^0.5个,可以直接用map映射,这样时空复杂度都是n*k^0.5,可以解出答案

    zoj 2110 很好的dfs+奇偶剪枝

    //我刚开始竟然用bfs做,不断的wa,bfs是用来求最短路的而这道题是求固定时间的 //剪纸奇偶剪枝加dfs #include<stdio.h> #include<queue> #include<math.h> #include<string.h> using namespace std; #define N 10 char ma[N][N]; struct node { int x,y,step; }ss,tt; int dis[4][2]={1,0,-

    组队赛#2解题总结

    比赛链接:click here~~ A BNU ACM校队时间安排,模拟自能 手速题: #include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <algorithm> using namespace std; int main() { int t; scanf("%d", &t); while (t--)