Team Game # # Solution Summary ZOJ 3803 YY ' s Minions (Dfs search + simulation)

Source: Internet
Author: User

YY ' s Minions Time limit: 2 Seconds Memory Limit: 65536 KB

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

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

  • If This minion was awake, and the number of its neighbors who was awake is less than 2, this minion would feel lonely and tu RN to asleep.
  • If This minion was awake, and the number of its neighbors who was awake is more than 3, this minion would turn to asleep for It'll feel too crowded.
  • If This minion was awake, and the number of its neighbors who was awake is exactly 2 or 3, this minion would keep being Awak E and feel very happy.
  • If This minion was asleep, and the number of its neighbors who was awake is exactly 3, this minion would 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'll get bored and leave this silly game. We use the ' X ' s to describe them. We Suppose a minion would leave after T minutes. It'll leave at the end of the Tth minute. Its status is considered during the change at the beginning of the Tth minute, and should was ignored after. Of course, one minion would 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's weak and lazy! Cute girl to solve this problem:)


    There is multiple test cases.

    The first line contains the number of test cases Q . 1<= Q <=100.
    For each case, there is several lines:
    The first line contains four integers,,,. means the number of N M F K K leaving messages. 1<= N , M<=50, 1<= F <=1000, 1<= K <=n*m.
    Next N lines is the matrix which shows the initial status of each minion. Each line contains M chars. We guarantee this ' X ' wouldn ' t appear in initial status matrix.
    And next K lines is the leaving messages. Each line contains three integers Ti ,,, Xi they mean the minion who's located in Yi ( Xi , Yi ) would leave Tithe game at the end of the th minutes. 1<= Ti <= F , 1<= Xi <= N , 1<= Yi <= M .


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

    Sample Input
    23 3 2 11011100011 2 25 5 6 310111010000000001100100002 3 32 4 15 1 5
    Sample Output
    For Case 1:t=0, the game starts101110001---------------at the beginning of T=1, a change took place100101010-------------- -at the end of T=1 (the Minion in (2,2) left) 1001x1010---------------at the beginning of t=2, a change took place0101x0010 ---------------at the end of t=2 (nothing changed for no Minion left at t=2) 0101x0010

  • Link: click here~~

    Test instructions: There is a n*m pet in a matrix, each pet has a state, 1 for waking, 0 for sleeping, ' X ' for what is going to leave, if the pet (awake) around the number of Awake >3 | | <2 It will fall asleep, if the pet (asleep) around the number of awake ==3 will wake up, every minute will have a change of state! Some of these pets will leave within a given time. Solve the status of all pets after a given T minute
    "problem-solving ideas": DFS search Eight directions, and then simulate, pay attention to the pre-processing dir array, see the topic requirements, animal sleep state and the number of animals around the relationship. After doing it, the search section is easy to implement, but if there are multiple pets to leave, and at different times, and each time period, will affect the state of their pets around! This part is going to be a little more careful, since more than one time period, we have all the time period from small to large arrange it again! , process in turn, synchronize DFS Search, update status! , the specific look at the code


    #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};//here array Open [8] [4],wa once more!]    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&GT;=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++)//Note Dynamic update!            {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;}

    Team Game # # Solution Summary ZOJ 3803 YY ' s Minions (Dfs search + simulation)

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.