关注
// 代码分享
// 第一题 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
相关推荐
昨天 18:10
中国矿业大学 Java 牛客吹哨人:哨哥晚点统一更新到黑名单:能救一个是一个!26届毁意向毁约裁员黑名单https://www.nowcoder.com/discuss/1525833
点赞 评论 收藏
分享
牛客热帖
更多
正在热议
更多
# 入职跑路最快的一次经历 #
24442次浏览 172人参与
# 你会为了工作牺牲生活吗? #
40894次浏览 315人参与
# 乐堡互娱校招 #
17186次浏览 231人参与
# 国企秋招,你投了吗? #
9841次浏览 106人参与
# ___岗狗都不干,我干! #
11685次浏览 98人参与
# 拿到offer之后,可以做些什么 #
23680次浏览 171人参与
# 携程求职进展汇总 #
611938次浏览 4527人参与
# 硬件开发岗知多少 #
15758次浏览 123人参与
# 校招谈薪技巧 #
37169次浏览 515人参与
# 你在职场中沾染到的“坏”习惯 #
9903次浏览 100人参与
# 互联网回暖,腾讯要招5000人! #
19816次浏览 583人参与
# 海尔求职进展汇总 #
5666次浏览 31人参与
# 面试时间长是好事吗? #
52939次浏览 401人参与
# 材料人的华为红黑体验 #
32206次浏览 181人参与
# 通信硬件岗投递时间线 #
24093次浏览 88人参与
# 材料转码还有必要吗? #
27533次浏览 143人参与
# 找工作前vs找工作后的心路变化 #
20778次浏览 160人参与
# 如何看待应届生身份? #
167998次浏览 1920人参与
# 提名点击就挂的公司 #
47132次浏览 242人参与
# 入职第一天,你准备什么时候下班 #
79328次浏览 447人参与
# 长江存储求职进展汇总 #
55955次浏览 225人参与