제출 #1344526

#제출 시각아이디문제언어결과실행 시간메모리
1344526usuyusNavigation 2 (JOI21_navigation2)C++20
75 / 100
216 ms900 KiB
#include "Anna.h"

#include <bits/stdc++.h>
using namespace std;

// 0: anchor
// [1..5): far
// [5..14): near

// position in the corresponding 3x3 block
int idx_in_block(int i, int j) {
	i %= 3, j %= 3;
	return i * 3 + j;
}

// code the max(|di|, |dj|) > 1 case into [1..5)
int make_far_flag(int di, int dj) {
	// in the order of Bruno's actions
	if (dj >  1) return 1; // right
	if (dj < -1) return 2; // left
	if (di >  1) return 3; // down
	if (di < -1) return 4; // up
	return -1; // shouldn't happen
}

// code the max(|di|, |dj|) <= 1 case into [5..14)
int make_near_flag(int di, int dj) {
	di++, dj++;
	return 5 + di * 3 + dj;
}

// whoops, thought values were 0-indexed...
void MySetFlag(int i, int j, int val) {
	SetFlag(i, j, val + 1);
}

void Anna(int N, int K, vector<int> R, vector<int> C) {
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			int idx = idx_in_block(i, j);

			// anchor
			if (idx == 0) {
				MySetFlag(i, j, 0);
				continue;
			}

			// unused
			if (idx > K) {
				MySetFlag(i, j, 1); // must not be anchor
				continue;
			}

			idx--; // now in [0..K)
			int di = R[idx] - i, dj = C[idx] - j;

			if (max(abs(di), abs(dj)) > 1) {
				MySetFlag(i, j, make_far_flag(di, dj));
			} else {
				MySetFlag(i, j, make_near_flag(di, dj));
			}
		}
	}
}
#include "Bruno.h"
#include <bits/stdc++.h>
using namespace std;

// find the index of candidate in neighbours list given the anchor
int cand_idx(int anchor, int cand) {
	cand += 1; // skip the anchor
	int ai = anchor / 3, aj = anchor % 3;

	int i = (ai + (cand / 3)) % 3; // i goes down every 3 indices
	int j = (aj + cand) % 3; // j always wraps around
	return i * 3 + j;
}

// find the offset of the destination from our current location if it's near
// idx is where the flag is
pair<int, int> near_offset(int idx, int flag) {
	// centre -> flag pos
	int i = idx / 3 - 1, j = idx % 3 - 1;

	// flag pos -> dest
	flag -= 5;
	int fi = flag / 3 - 1, fj = flag % 3 - 1;

	return {i + fi, j + fj};
}

// decide in which direction to go given the destination offset
int dir_of_offset(int di, int dj) {
	if (dj > 0) return 0; // right
	if (dj < 0) return 1; // left
	if (di > 0) return 2; // down
	if (di < 0) return 3; // up
	return 4; // stay (since di = dj = 0)
}

vector<int> Bruno(int K, vector<int> value) {
	// whoops, thought values were 0-indexed...
	for (int &val : value) val--;

	int anchor, ai, aj;
	for (int idx = 0; idx < 9; idx++) {
		if (value[idx] == 0) {
			anchor = idx;
			break;
		}
	}	

	vector<int> res(K, 0);

	for (int cand = 0; cand < K; cand++) {
		int idx = cand_idx(anchor, cand);

		if (value[idx] < 5) { // far
			// Anna told me where to go here
			// dirs are 0-indexed though
			res[cand] = value[idx] - 1;
		} else { // near
			// i know exactly where it is 
			auto [di, dj] = near_offset(idx, value[idx]);
			res[cand] = dir_of_offset(di, dj);
		}
	}

	return res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...