关注
// 代码分享
// 第一题 100%
#include <iostream>
using namespace std;
static int dirs[4][2] = { {-1, -1}, {-1, 1}, {1, 1}, {1, -1} };
int solve(int n, int m, int x, int y, int dx, int dy, int t) {
int res = 0;
for (int i = 0; i < t; ++i) {
x += dx;
y += dy;
if (x == 2 || x == n - 1) {
++res;
dx = -dx;
}
if (y == 2 || y == m - 1) {
++res;
dy = -dy;
}
}
return res;
}
int main() {
int Q = 0;
cin >> Q;
while (Q--) {
int N = 0, M = 0;
cin >> N >> M;
int X = 0, Y = 0, W = 0, T = 0;
cin >> X >> Y >> W >> T;
int res = solve(N, M, X, Y, dirs[W][0], dirs[W][1], T);
cout << res << endl;
}
return 0;
}
// 第二题 100%
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int solve(string str) {
int res = 0;
switch(str[0]){
case '#':
res = solve(str.substr(1));
break;
case '+':
res = solve(str.substr(2)) + 1;
break;
default:
int n = str.size();
bool flag = false;
for (int i = 0; i < n; ++i) {
if (str[i] == '(') {
if (i > 0 && str[i - 1] == ']') {
flag = true;
res -= 2;
}
else {
++res;
}
}
else if (str[i] == ')'){
if (flag) {
flag = false;
}
else {
++res;
}
}
else{
if (!flag) {
++res;
}
}
}
break;
}
return res;
}
int main() {
int T = 0;
cin >> T;
while (T--) {
int N = 0;
cin >> N;
getchar();
vector<string> content(N);
for (int i = 0; i < N; ++i) {
getline(cin, content[i]);
}
int res = 0;
for (int i = 0; i < N; ++i) {
cout << content[i] << endl;
res += solve(content[i]);
}
cout << res << endl;
}
return 0;
}
// 第三题 0% 超时 深搜
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cctype>
#include <climits>
using namespace std;
// 左 右 下 上
static int dirs[4][2] = { {0, -1}, {0, 1}, {1, 0}, {-1, 0} };
void dfs(vector<string> & matrix, vector<int> &weight, vector<vector<int>> &visited,
int x, int y, int tx, int ty, int di, int qian, int you, int path, int &res){
int m = matrix.size(), n = matrix[0].size();
if (x == tx && y == ty) {
path += weight[di];
res = min(path, res);
return;
}
visited[x][y] = 1;
for (int d = 0; d < 4; ++d) {
int newx = x + dirs[d][0];
int newy = y + dirs[d][1];
int newd = di, newq = qian, newyou = you;
if (newx >= 0 && newx < m && newy >= 0 && newy < n &&
!visited[newx][newy] && matrix[newx][newy] == '.') {
switch (d) {
case 0:
newd = 5 - you;
newyou = di;
break;
case 1:
newd = you;
newyou = 5 - di;
break;
case 2:
newd = qian;
newq = 5 - di;
break;
case 3:
newd = 5 - qian;
newq = di;
break;
}
dfs(matrix, weight, visited, newx, newy, tx, ty, newd, newq, newyou,
path + weight[di], res);
}
}
visited[x][y] = 0;
}
int main() {
int T = 0;
cin >> T;
while (T--) {
int M = 0, N = 0;
cin >> M >> N;
vector<string> matrix(M);
for (int i = 0; i < M; ++i) {
cin >> matrix[i];
}
int sx, sy, tx, ty;
cin >> sx >> sy >> tx >> ty;
vector<int> weight(6);
for (int i = 0; i < 6; ++i) {
cin >> weight[i];
}
if (matrix[sx][sy] == '#' || matrix[tx][ty] == '#') {
cout << -1 << endl;
}
else {
int res = 0x3f3f3f3f;
vector<vector<int>> visited(M, vector<int>(N, 0));
dfs(matrix, weight, visited, sx, sy, tx, ty, 4, 0, 2, 0, res);
cout << (res == 0x3f3f3f3f ? -1 : res) << endl;
}
}
return 0;
}
查看原帖
点赞 2
相关推荐
01-10 19:44
上海大学 Java
牛客57020934...:现在大厂就很迷,感觉要么就是算法+agent,要么就是开发+agent。。还是招和原来一样的岗位,只是都要求赋能agent了 点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 简历第一个项目做什么 #
4698次浏览 71人参与
# 租房找室友 #
58231次浏览 240人参与
# MiniMax求职进展汇总 #
1577次浏览 25人参与
# 实习的你做了哪些离谱的工作 #
5814次浏览 87人参与
# 工作压力大,你会干什么? #
7366次浏览 194人参与
# 参加哪些竞赛对找工作有帮助? #
5618次浏览 109人参与
# AI让你的思考变深了还是变浅了? #
2843次浏览 86人参与
# 找实习记录 #
15205次浏览 282人参与
# 如果不上班,你会去做什么 #
4023次浏览 184人参与
# 携程工作体验 #
20718次浏览 79人参与
# 邪修省钱套路 #
4783次浏览 168人参与
# 学历对求职的影响 #
587820次浏览 4004人参与
# 为了入行xx岗,我学了__ #
3278次浏览 55人参与
# 我的付费上班经历 #
8941次浏览 149人参与
# 一上班就想____,这正常吗? #
13970次浏览 143人参与
# 如果再来一次,你还会选择这个工作吗? #
780152次浏览 6264人参与
# 职场上哪些行为很加分? #
314358次浏览 3549人参与
# 如何KTV领导 #
83407次浏览 525人参与
# 产品实习,你更倾向大公司or小公司 #
193750次浏览 2074人参与
# 实习打杂,要跑路吗 #
56299次浏览 336人参与
# 你们公司哪个部门最累? #
37515次浏览 255人参与