网易互娱 8.27 笔试题解

第三题真恶心,全枚举了一遍踩点完成

// 第1题
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int main() {
    int T;
    while (cin >> T) {
        int N, M;
        for (int t = 0; t < T; ++t) {
            cin >> N >> M;
            vector<vector<char>> v(N, vector<char>(N));
            for (int i = 0; i < N; ++i) {
                for (int j = 0; j < N; ++j) cin >> v[i][j];
            }

            if (M <= N) {
                int idx = (N - M) / 2;
                for (int i = idx; i < N - idx; ++i) {
                    for (int j = idx; j < N - idx; ++j) {
                        cout << v[i][j];
                    }
                    cout << endl;
                }
                if (t != T - 1) cout << endl;
            } else {
                float left = (float(M - N)) / 2 / N;
                int times = ceil(left) * 2 + 1;
                times *= N;
                vector<vector<char>> wall(times, vector<char>(times));
                for (int i = 0; i < times; ++i) {
                    for (int j = 0; j < times; ++j) {
                        wall[i][j] = v[i%N][j%N];
                    }
                }
                int center = ceil(times / 2);
                int width = ceil(M / 2);
                for (int i = center - width; i <= center + width; ++i) {
                    for (int j = center - width; j <= center + width; ++j) {
                        cout << wall[i][j];
                    }
                    cout << endl;
                }
                if (t != T - 1) cout << endl;
            }
        }
    }
    return 0;
}

// 第2题
#include <iostream>
#include <vector>
#include <set>
using namespace std;

bool is_intersect(const vector<int>& a, const vector<int>& b) {
    int xmin = max(a[0], b[0]);
    int xmax = min(a[2], b[2]);
    int ymin = max(a[1], b[1]);
    int ymax = min(a[3], b[3]);
    return (xmin < xmax && ymin < ymax);
}

int main() {
    int T;
    while (cin >> T) {
        int N;
        for (int t = 0; t < T; ++t) {
            cin >> N;
            vector<vector<int>> v;
            int x0, y0, x1, y1;
            for (int i = 0; i < N; ++i) {
                cin >> x0 >> y0 >> x1 >> y1;
                v.push_back({x0, y0, x1, y1});
            }
            if (N == 1) {
                cout << 0;
                if (t != T - 1) cout << endl;
                continue;
            }

            set<vector<int>> ss;
            for (int i = 0; i < N; ++i) {
                for (int j = i + 1; j < N; ++j) {
                    if (is_intersect(v[i], v[j])) {
                        ss.emplace(v[i]);
                        ss.emplace(v[j]);
                    }
                }
            }

            if (ss.empty()) {
                cout << 0;
                if (t != T - 1) cout << endl;
                continue;
            }

            int res = 0;
            vector<vector<int>> grid(1005, vector<int>(1005, 0));
            for (auto it = ss.begin(); it != ss.end(); ++it) {
                for (int i = (*it)[0]; i < (*it)[2]; ++i) {
                    for (int j = (*it)[1]; j < (*it)[3]; ++j) {
                        if (grid[i][j] == 0) {
                            ++res;
                        }
                        grid[i][j] = 1;
                    }
                }
            }
            cout << res;
            if (t != T - 1) cout << endl;
        }
    }
    return 0;
}

// 第3题
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;

// 构造 string,24位可以用int,用哈希表缓存
/*
   -  -
  |\/|/\|
   -  -
  |\/|\/|
   _  _
   前8位代表边框,后12位代表内部,还有8位是陡的斜线
*/

int connect(int x, int y, int nx, int ny) {
    if (x < nx) {
        swap(x, nx);
        swap(y, ny);
    }
    if (x == 0) {
        if (y == 0) {
            if (nx == 0 && ny == 1) return (1 << 1);
            else if (nx == 0 && ny == 2) return (1 << 1) | (1 << 2);
            else if (nx == 1 && ny == 0) return (1 << 8);
            else if (nx == 2 && ny == 0) return (1 << 7) | (1 << 8);
            else if (nx == 1 && ny == 1) return (1 << 9);
            else if (nx == 1 && ny == 2) return (1 << 17);
            else if (nx == 2 && ny == 1) return (1 << 24);
            else if (nx == 2 && ny == 2) return (1 << 9) | (1 << 13);
        } else if (y == 1) {
            if (nx == 0 && ny == 0) return (1 << 1);
            else if (nx == 0 && ny == 2) return (1 << 2);
            else if (nx == 1 && ny == 0) return (1 << 28);
            else if (nx == 1 && ny == 1) return (1 << 10);
            else if (nx == 1 && ny == 2) return (1 << 25);
            else if (nx == 2 && ny == 0) return (1 << 18);
            else if (nx == 2 && ny == 1) return (1 << 10) | (1 << 14);
            else if (nx == 2 && ny == 2) return (1 << 19);
        } else if (y == 2) {
            if (nx == 0 && ny == 0) return (1 << 1) | (1 << 2);
            else if (nx == 0 && ny == 1) return (1 << 2);
            else if (nx == 1 && ny == 0) return (1 << 20);
            else if (nx == 1 && ny == 1) return (1 << 11);
            else if (nx == 1 && ny == 2) return (1 << 3);
            else if (nx == 2 && ny == 0) return (1 << 11) | (1 << 15);
            else if (nx == 2 && ny == 1) return (1 << 21);
            else if (nx == 2 && ny == 2) return (1<<3) | (1<<4);
        }
    } else if (x == 1) {
        if (y == 0) {
            if (nx == 0 && ny == 0) return (1 << 8);
            else if (nx == 0 && ny == 1) return (1 << 28);
            else if (nx == 0 && ny == 2) return (1 << 20);
            else if (nx == 1 && ny == 1) return (1 << 16);
            else if (nx == 1 && ny == 2) return (1 << 12) | (1 << 16);
            else if (nx == 2 && ny == 0) return (1 << 7);
            else if (nx == 2 && ny == 1) return (1 << 27);
            else if (nx == 2 && ny == 2) return (1 << 23);
        } else if (y == 1) {
            if (nx == 0 && ny == 0) return (1 << 9);
            else if (nx == 0 && ny == 1) return (1 << 10);
            else if (nx == 0 && ny == 2) return (1 << 11);
            else if (nx == 1 && ny == 0) return (1 << 16);
            else if (nx == 1 && ny == 2) return (1 << 12);
            else if (nx == 2 && ny == 0) return (1 << 15);
            else if (nx == 2 && ny == 1) return (1 << 14);
            else if (nx == 2 && ny == 2) return (1 << 13);
        } else if (y == 2) {
            if (nx == 0 && ny == 0) return (1 << 17);
            else if (nx == 0 && ny == 1) return (1 << 25);
            else if (nx == 0 && ny == 2) return (1 << 3);
            else if (nx == 1 && ny == 0) return (1 << 16) | (1 << 12);
            else if (nx == 1 && ny == 1) return (1 << 12);
            else if (nx == 2 && ny == 0) return (1 << 22);
            else if (nx == 2 && ny == 1) return (1 << 26);
            else if (nx == 2 && ny == 2) return (1 << 4);
        }
    } else {
        if (y == 0) {
            if (nx == 0 && ny == 0) return (1 << 7) | (1 << 8);
            else if (nx == 0 && ny == 1) return (1 << 18);
            else if (nx == 0 && ny == 2) return (1 << 15) | (1 << 11);
            else if (nx == 1 && ny == 0) return (1 << 7);
            else if (nx == 1 && ny == 1) return (1 << 15);
            else if (nx == 1 && ny == 2) return (1 << 22);
            else if (nx == 2 && ny == 1) return (1 << 6);
            else if (nx == 2 && ny == 2) return (1 << 5) | (1 << 6);
        } else if (y == 1) {
            if (nx == 0 && ny == 0) return (1 << 24);
            else if (nx == 0 && ny == 1) return (1 << 10) | (1 << 14);
            else if (nx == 0 && ny == 2) return (1 << 21);
            else if (nx == 1 && ny == 0) return (1 << 27);
            else if (nx == 1 && ny == 1) return (1 << 14);
            else if (nx == 1 && ny == 2) return (1 << 26);
            else if (nx == 2 && ny == 0) return (1 << 6);
            else if (nx == 2 && ny == 2) return (1 << 5);
        } else if (y == 2) {
            if (nx == 0 && ny == 0) return (1 << 9) | (1 << 13);
            else if (nx == 0 && ny == 1) return (1 << 19);
            else if (nx == 0 && ny == 2) return (1 << 3) | (1 << 4);
            else if (nx == 1 && ny == 0) return (1 << 23);
            else if (nx == 1 && ny == 1) return (1 << 13);
            else if (nx == 1 && ny == 2) return (1 << 4);
            else if (nx == 2 && ny == 1) return (1 << 5);
            else if (nx == 2 && ny == 0) return (1 << 5) | (1 << 6);
        }
    }
    return 0;
}

void dfs(int x, int y, int cur, vector<vector<char>> &grid, unordered_set<int> &ss) {
    grid[x][y] = 'X';
    for (int i = 0; i < 3; ++i) {
        for (int j = 0; j < 3; ++j) {
            if (grid[i][j] == '.') {
                int val = connect(x, y, i, j);
                int nex = cur | val;
                ss.emplace(nex);
                dfs(i, j, nex, grid, ss);
            }
        }
    }
    grid[x][y] = '.';
}

int main() {
    int T;
    while (cin >> T) {
        for (int t = 0; t < T; ++t) {
            vector<vector<char>> v(3, vector<char>(3));
            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) cin >> v[i][j];
            }

            unordered_set<int> ss;

            for (int i = 0; i < 3; ++i) {
                for (int j = 0; j < 3; ++j) {
                    if (v[i][j] == '.') dfs(i, j, 0, v, ss);
                }
            }
            cout << ss.size();
            if (t != T - 1) cout << endl;
        }
    }
    return 0;
}


#网易互娱##网易互娱笔试##网易##笔试##网易笔试#
全部评论
第一题直接映射2个原点坐标,代码量极小                 int origin = (n - 1) / 2; int center = (m - 1) / 2;//i = center+x = origin+x => x = i - center, for (int j = 0; j < m; j++) { for (int k = 0; k < m; k++) { int dj = j - center; int dk = k - center; ans[j][k] = wall[(origin + dj + n * 100) % n][(origin + dk + n * 100) % n]; cout << ans[j][k]; } cout << endl; }
2
送花
回复
分享
发布于 2022-08-27 22:10 四川
t1只需要完成1/4内容就行了
1
送花
回复
分享
发布于 2022-08-27 22:42 陕西
滴滴
校招火热招聘中
官网直投
厉害了这位运算
点赞
送花
回复
分享
发布于 2022-08-27 22:05 北京
抄一波🧐
点赞
送花
回复
分享
发布于 2022-08-27 22:05 北京
我也是用dxdy0,1,2暴力枚举8种方向,debug头搞晕了没做出来
点赞
送花
回复
分享
发布于 2022-08-27 22:05 四川
mark, 第一题用了一个半小时,a了2题
点赞
送花
回复
分享
发布于 2022-08-27 22:06 四川
膜拜大佬
点赞
送花
回复
分享
发布于 2022-08-27 22:06 天津
我嫌太暴力了就没枚举,后面想不出来只能枚举了结果没写完,就差几秒没提交上去
点赞
送花
回复
分享
发布于 2022-08-27 22:17 广东
暴力怎么写得完的
点赞
送花
回复
分享
发布于 2022-08-27 22:25 江苏
第三题dfs如何判断成环的图案?
点赞
送花
回复
分享
发布于 2022-08-28 09:05 上海

相关推荐

7 11 评论
分享
牛客网
牛客企业服务