网易互娱 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 陕西
第三题dfs如何判断成环的图案?
点赞 回复 分享
发布于 2022-08-28 09:05 上海
暴力怎么写得完的
点赞 回复 分享
发布于 2022-08-27 22:25 江苏
我嫌太暴力了就没枚举,后面想不出来只能枚举了结果没写完,就差几秒没提交上去
点赞 回复 分享
发布于 2022-08-27 22:17 广东
膜拜大佬
点赞 回复 分享
发布于 2022-08-27 22:06 天津
mark, 第一题用了一个半小时,a了2题
点赞 回复 分享
发布于 2022-08-27 22:06 四川
我也是用dxdy0,1,2暴力枚举8种方向,debug头搞晕了没做出来
点赞 回复 分享
发布于 2022-08-27 22:05 四川
抄一波🧐
点赞 回复 分享
发布于 2022-08-27 22:05 北京
厉害了这位运算
点赞 回复 分享
发布于 2022-08-27 22:05 北京

相关推荐

1、自我介绍2、Agent项目是实习项目还是个人项目?有没有上线?3、拷打实习(10min)4、大模型微调,你的训练数据集是如何构建的?数据量有多大?5、在构建数据集的过程中,遇到了哪些挑战?花了多长时间?6、你之前的实习经历偏后端工程,你未来的职业规划更倾向于纯后端开发,还是希望从事与AI/大模型结合的工作?7、详细讲一下Golang中Channel的概念和作用,它是否是并发安全的?8、Channel和传统的锁(Mutex)在实现并发控制时有什么区别?各自的适用场景是什么?9、讲一下GMP模型10、当P的本地队列为空或者不为空时,它会怎么去调度G(协程)?11、Redis支持哪些数据结构12、为什么Redis的速度这么快13、如何实现一个类似淘宝搜索框的实时商品名称模糊搜索功能?14、实时输入联想与输入完成后点击搜索在技术实现上有什么本质区别?15、实时搜索通常使用什么网络协议(如WebSocket)?你了解或有使用过吗?讲一下16、请详细说明微信扫码登录的完整流程和背后发生的原理17、在微服务架构中,服务发现和负载均衡是如何实现的?18、服务注册中心(如Nacos,&nbsp;Consul)是如何工作的?服务实例如何注册和保活(如通过心跳机制)?19、讲一下Agent中的“长短期记忆”20、什么样的信息应该放在长期记忆,什么样的信息放在短期记忆?21、当对话轮数很多,上下文窗口不足时,有哪些处理策略?(如截断、压缩)22、如果要进行记忆压缩,通常有哪些方法?23、了解过Agent的设计范式吗?有哪些?24、你设计的Agent是怎么实现ReAct模式的?详细讲讲25、手撕:实现一个并发任务处理器:给定一个包含100个任务ID的列表,要求控制最大并发数为3,模拟并发调用某个外部接口(如打印ID)26、反问
三本咋了:很好的面筋
查看24道真题和解析
点赞 评论 收藏
分享
评论
7
11
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务