标签:dir iss 左右 组合 mes pre 北京 while for
PS: 2020的是夏令营试题
#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; }
#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: 暂时是更新完了,综合面试也是一个大头,都抓紧吧,尽量不手生,多看 多练 加油!!!! Fighting!!! (别忘了前面未完善的!!)
标签:dir iss 左右 组合 mes pre 北京 while for
原文地址:https://www.cnblogs.com/ache/p/12630070.html