The Beijing Institute of Technology re-examination machine --2020

PS: 2020 is the summer camp questions

1, Title: to give you a size of m * n matrix, each point there are three values ​​0, 1; 0 representative of the obstacle, 1 for white, represents the ink droplet. Each second ink droplet can diffuse up and down thereto, around the stained white, then white is dyed may continue to spread around, and so on. Q. After a few seconds, all of the matrix are stained white.
If the output time of diffusion;
If not, then the output FALSE. 
Input: values ​​for each point and the size of m n matrix 
Output: diffusion time or FALSE 
E.g:
3 3
0 1 0
1 2 1
0 1 0
Output: 1

3 3
0 1 0
1 2 1
0 1 1
Output: 2

2 3
1 0 0
0 0 2
Output: False
 
#include <iostream>
#include <queue>
using namespace std;
int main() {
    int m, n;
    while (cin >> m >> n) {
        int arr[m][n];
        int temp = 0;
        queue<int> qx, qy;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                cin >> arr[i][j];
                if(arr[i][j] == 2) {
                    qx.push(i);
                    qy.push(j);
                }
                if(arr[i][j] == 1) temp++;
            }
        }
        int sec = 0;
        while(!qx.empty() && temp) {
            int size = qx.size();
            int dir[5] = {1, 0, -1, 0, 1};
            while(size--) {
                int x = qx.front();
                int y = qy.front();
                qx.pop();
                qy.pop();
                for(int i = 0; i < 4; i++) {
                    int dx = x + dir[i];
                    int dy = y + dir[i + 1];
                    if(dx < 0 || dx >= m || dy < 0 || dy >= n || arr[dx][dy] != 1) continue;
                    else {
                        arr[dx][dy] = 2;
                        temp--;
                        qx.push(dx);
                        qy.push(dy);
                    }
                }
            }
            sec++;
        }
        if (temp) cout << "False" << endl;
        else cout << sec << endl;
    }
    return 0;
}

 

2, three input string, the string can ask the third composition is formed by the first repeated two strings. If so, the frequency of use of each of the first two strings output; if not, the output FALSE. 
Input: three strings 
Output: The first two times the respective strings or FALSE 
Input: aa bb bbaaaabbaa 
Output: 32 
Import: ab ba abbaaabaab 
Output: FALSE 
(Note that special case: aa aab aabaa it recursively)
#include <iostream>
#include <queue>
using namespace std;

queue<string> st;

bool sub(string s1, string s, int n) {
    return s.substr(n, s1.length()) == s1;
}

bool issub(string s1, string s2, string s, int p, int rem) {
    if(p == s.length() && rem == 0) return true;
    if(sub(s1, s, p) && sub(s2, s, p)) st.push(s.substr(p, rem));
    if(sub(s1, s, p)) {
        rem -= s1.length();
        p += s1.length();
        issub(s1, s2, s, p, rem);
    }
    if(sub(s2, st.front(), p)) {
        st.pop();
        rem -= s2.length();
        p += s2.length();
        issub(s1, s2, s, p, rem);
    }
    return true;
}

int main() {
    string s1, s2, s;
    while(cin >> s1 >> s2 >> s) {
        int l = s.length();
        cout << issub(s1, s2, s, 0, l);
    }
    return 0;
}

 

PS: temporarily update is over, a comprehensive interview is a big head, all pay close attention to it, try not to Shousheng, see more and more practice Come on! ! ! ! Fighting! ! ! (Do not forget the front is not perfect !!)

Guess you like

Origin www.cnblogs.com/ache/p/12630070.html