网易互娱小球变色问题ac,代码如下


#include <iostream>
#include <vector>
using namespace std;

int dx[4]{-1, -1, 1, 1};
int dy[4]{-1, 1, 1, -1};
int N, M;

void dp(int& f, int i, int j, int w, int cnts)
{
if (cnts == 0) return;
int a = i, b = j;
if (w == 0) a += dx[0], b += dy[0];
if (w == 1) a += dx[1], b += dy[1];
if (w == 2) a += dx[2], b += dy[2];
if (w == 3) a += dx[3], b += dy[3];
cnts--;
if (a == 2 || b == 2 || a == N - 1 || b == M - 1)
{
if (a == 2)
{
if (b == 2) w = 2, f += 2;
if (b == M - 1) w = 3, f += 2;
if (w == 0) w = 3, f += 1;
if (w == 1) w = 2, f += 1;
}
else if (a == N - 1)
{
if (b == 2) w = 1, f += 2;
if (b == M - 1) w = 0, f += 2;
if (w == 2) w = 1, f += 1;
if (w == 3) w = 0, f += 1;
}
else
{
if (b == 2)
{
if (w == 0) w = 1, f += 1;
if (w == 3) w = 2, f += 1;
}
if (b == M - 1)
{
if (w == 2) w = 3, f += 1;
if (w == 1) w = 0, f += 1;
}
}
}
dp(f, a, b, w, cnts);
}

int main()
{
int cnt = 0;
cin >> cnt;
for (int i = 0; i < cnt; i ++ )
{
int rows, cols;
cin >> rows >> cols;
N = rows, M = cols;
int x, y, w, t;
cin >> x >> y >> w >> t;
int f = 0;
dp(f, x, y, w, t);
cout << f << endl;
}
return (0);
}

#网易互娱##笔试题目#
全部评论
要用dp吗,,,我用的模拟,怎么做都只通过20,😖😖😖
点赞 回复 分享
发布于 2019-04-20 12:34
// 100% for循环t次即可 #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; }
点赞 回复 分享
发布于 2019-04-20 12:46
大佬有没有受到面试通知?
点赞 回复 分享
发布于 2019-04-22 18:08
没有,
点赞 回复 分享
发布于 2019-04-22 18:25

相关推荐

02-16 10:35
已编辑
西安科技大学 后端
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
02-14 11:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务