題目鏈接:https://codeforces.com/gym/101667/attachments
題意:現在讓你從原點開始走,每次給你轉向的方向你需要安排每一次轉向要走多遠並且讓整個路徑沒有交點。
解題心得:螺旋走位法,記錄上下左右能夠到達的最遠值,如果再次走該方向只需要比這個方向最遠值多走一步就行了,這樣不會有交點。
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+100;
struct Step {
int len, dir;
}step[maxn];
int n;
void init() {
scanf("%d", &n);
for(int i=1;i<=n;i++) {
scanf("%d%d",&step[i].len, &step[i].dir);
}
}
int main() {
// freopen("1.in.txt", "r", stdin);
init();
int MaxX, MinX, MaxY, MinY, nowx, nowy;
MaxX = MaxY = MinX = MinY = nowx = nowy = 0;
int states = 0;
vector <int> ans;
for(int i=1;i<=n;i++) {
int cnt = 0;
if(states == 0) {
cnt = MaxX - nowx + 1;
nowx += cnt;
MaxX = nowx;
} else if(states == 3) {
cnt = nowy - MinY + 1;
nowy -= cnt;
MinY = nowy;
} else if(states == 2) {
cnt = nowx - MinX + 1;
nowx -= cnt;
MinX = nowx;
} else {
cnt = MaxY - nowy + 1;
nowy += cnt;
MaxY = nowy;
}
ans.push_back(cnt);
states = (states + step[i].dir + 4) % 4;
}
for(auto x:ans) {
printf("%d\n", x);
}
return 0;
}