关注
// 代码分享
// 第一题 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
相关推荐
09-04 20:39
南京林业大学 机械工程师 
点赞 评论 收藏
分享
牛客热帖
更多
- 1... 面试最后的反问环节,能问些什么?(附特供问题)1.9W
- 2... BG一般,如何逆天改命拿下后端秋招SSP?1.3W
- 3... 从面试官的角度看待一场面试是怎么样的?7400
- 4... 害,找工作哪有不上当的!5285
- 5... 团、节、东孝子全部启动启动启动!(26届后端秋招总结)5236
- 6... 作为普通家庭出身的我,为什么非大厂不可?4954
- 7... 双非硕的十月份秋招总结4469
- 8... 感觉每个人都有自己的苦恼4089
- 9... 项目经历混乱?STAR法则手把手教你梳理(附真实案例分析过程)3447
- 10... 待了一年,一点没亏3405
正在热议
更多
# 实习在多还是在精 #
23675次浏览 188人参与
# 你的房租占工资的比例是多少? #
61102次浏览 742人参与
# 智慧芽求职进展汇总 #
450次浏览 5人参与
# 秋招踩过的“雷”,希望你别再踩 #
56539次浏览 819人参与
# 我的求职进度条 #
36214次浏览 574人参与
# 大厂VS公务员你怎么选 #
13184次浏览 215人参与
# 爱玛科技集团求职进展汇总 #
34495次浏览 231人参与
# 如果不考虑收入,你最想做什么工作? #
30894次浏览 180人参与
# 柠檬微趣工作体验 #
13064次浏览 72人参与
# 机械人的保底公司是哪一家? #
40450次浏览 133人参与
# 顺丰求职进展汇总 #
61803次浏览 306人参与
# 华为池子有多大 #
101974次浏览 731人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
135254次浏览 868人参与
# 如果再来一次,你还会学硬件吗 #
137678次浏览 1441人参与
# 如何用一句话描述你的职业 #
24758次浏览 172人参与
# 高学历就一定能找到好工作吗? #
55350次浏览 607人参与
# 如何排解工作中的焦虑 #
219418次浏览 2104人参与
# 实习下班不想学习,正常吗? #
13472次浏览 150人参与
# 反问环节如何提问 #
112154次浏览 2340人参与
# 你见过哪些工贼行为 #
11114次浏览 76人参与
# 工作中,努力重要还是选择重要? #
204241次浏览 2073人参与
# 校招谈薪一定要知道的事 #
9372次浏览 92人参与