結果

問題 No.5006 Hidden Maze
ユーザー square1001square1001
提出日時 2022-06-12 17:54:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 112 ms / 2,000 ms
コード長 4,895 bytes
コンパイル時間 994 ms
実行使用メモリ 22,868 KB
スコア 90,112
平均クエリ数 99.88
最終ジャッジ日時 2022-06-12 17:54:37
合計ジャッジ時間 8,368 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
21,952 KB
testcase_01 AC 27 ms
22,576 KB
testcase_02 AC 22 ms
22,228 KB
testcase_03 AC 19 ms
21,916 KB
testcase_04 AC 23 ms
21,904 KB
testcase_05 AC 26 ms
21,892 KB
testcase_06 AC 23 ms
21,940 KB
testcase_07 AC 26 ms
22,288 KB
testcase_08 AC 42 ms
21,904 KB
testcase_09 AC 34 ms
21,892 KB
testcase_10 AC 26 ms
21,780 KB
testcase_11 AC 22 ms
22,264 KB
testcase_12 AC 28 ms
22,240 KB
testcase_13 AC 22 ms
22,632 KB
testcase_14 AC 44 ms
21,904 KB
testcase_15 AC 22 ms
22,704 KB
testcase_16 AC 19 ms
21,792 KB
testcase_17 AC 23 ms
22,620 KB
testcase_18 AC 46 ms
21,940 KB
testcase_19 AC 30 ms
21,952 KB
testcase_20 AC 22 ms
21,880 KB
testcase_21 AC 24 ms
22,004 KB
testcase_22 AC 26 ms
21,768 KB
testcase_23 AC 24 ms
22,004 KB
testcase_24 AC 29 ms
22,468 KB
testcase_25 AC 23 ms
21,940 KB
testcase_26 AC 60 ms
21,904 KB
testcase_27 AC 30 ms
22,864 KB
testcase_28 AC 20 ms
22,620 KB
testcase_29 AC 65 ms
21,992 KB
testcase_30 AC 18 ms
21,880 KB
testcase_31 AC 20 ms
22,588 KB
testcase_32 AC 42 ms
22,576 KB
testcase_33 AC 30 ms
22,564 KB
testcase_34 AC 23 ms
21,916 KB
testcase_35 AC 22 ms
22,004 KB
testcase_36 AC 33 ms
21,904 KB
testcase_37 AC 38 ms
21,768 KB
testcase_38 AC 21 ms
21,940 KB
testcase_39 AC 21 ms
22,868 KB
testcase_40 AC 25 ms
21,892 KB
testcase_41 AC 22 ms
21,892 KB
testcase_42 AC 26 ms
21,880 KB
testcase_43 AC 54 ms
21,904 KB
testcase_44 AC 20 ms
22,444 KB
testcase_45 AC 54 ms
22,552 KB
testcase_46 AC 64 ms
22,240 KB
testcase_47 AC 35 ms
21,904 KB
testcase_48 AC 31 ms
21,892 KB
testcase_49 AC 87 ms
21,992 KB
testcase_50 AC 27 ms
22,728 KB
testcase_51 AC 37 ms
21,880 KB
testcase_52 AC 29 ms
21,904 KB
testcase_53 AC 37 ms
21,940 KB
testcase_54 AC 22 ms
21,780 KB
testcase_55 AC 24 ms
21,904 KB
testcase_56 AC 19 ms
22,624 KB
testcase_57 AC 24 ms
21,780 KB
testcase_58 AC 20 ms
22,600 KB
testcase_59 AC 24 ms
21,952 KB
testcase_60 AC 21 ms
22,612 KB
testcase_61 AC 20 ms
21,768 KB
testcase_62 AC 20 ms
22,728 KB
testcase_63 AC 20 ms
21,940 KB
testcase_64 AC 22 ms
21,952 KB
testcase_65 AC 41 ms
22,276 KB
testcase_66 AC 23 ms
21,992 KB
testcase_67 AC 26 ms
21,992 KB
testcase_68 AC 30 ms
22,396 KB
testcase_69 AC 24 ms
21,940 KB
testcase_70 AC 18 ms
22,240 KB
testcase_71 AC 25 ms
22,216 KB
testcase_72 AC 20 ms
22,564 KB
testcase_73 AC 27 ms
22,564 KB
testcase_74 AC 20 ms
21,792 KB
testcase_75 AC 25 ms
21,952 KB
testcase_76 AC 23 ms
21,892 KB
testcase_77 AC 38 ms
22,624 KB
testcase_78 AC 28 ms
21,892 KB
testcase_79 AC 95 ms
22,608 KB
testcase_80 AC 20 ms
21,768 KB
testcase_81 AC 22 ms
21,904 KB
testcase_82 AC 21 ms
21,904 KB
testcase_83 AC 22 ms
21,892 KB
testcase_84 AC 36 ms
21,940 KB
testcase_85 AC 28 ms
22,504 KB
testcase_86 AC 22 ms
21,928 KB
testcase_87 AC 37 ms
22,264 KB
testcase_88 AC 25 ms
21,940 KB
testcase_89 AC 112 ms
21,952 KB
testcase_90 AC 24 ms
21,892 KB
testcase_91 AC 21 ms
22,228 KB
testcase_92 AC 25 ms
21,904 KB
testcase_93 AC 22 ms
22,564 KB
testcase_94 AC 30 ms
22,228 KB
testcase_95 AC 21 ms
22,004 KB
testcase_96 AC 20 ms
22,276 KB
testcase_97 AC 28 ms
21,768 KB
testcase_98 AC 27 ms
21,904 KB
testcase_99 AC 32 ms
21,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/*
	yukicoder score contest (2022.6.12)
	author: square1001
	
	<< solution outline >>
	- repeat outputting the track with "the highest probability"
	  - probability estimation = bayesian
	  - assume that the probability to be walls are "independent"
	- highest probability track: search by dijkstra's algorithm
*/

#include <cmath>
#include <queue>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

uint64_t seed = 123456789ULL;
uint64_t xorshift64() {
	seed ^= seed << 13;
	seed ^= seed >> 7;
	seed ^= seed << 17;
	return seed;
}
int rand_int(int l, int r) {
	return l + int(xorshift64() % (r - l));
}
double randouble() {
	return double(xorshift64()) / double(uint64_t(-1));
}

class state {
public:
	int x, y; double prob;
	state() : x(-1), y(-1), prob(0.0) {};
	state(int x_, int y_, double prob_) : x(x_), y(y_), prob(prob_) {};
	bool operator<(const state& s) const {
		return prob < s.prob;
	}
};

int solve(int H, int W, double p) {
	// ---------- INITIAL CONFIGURATION ---------- //
	const vector<int> dx = { 1, 0, -1, 0 };
	const vector<int> dy = { 0, 1, 0, -1 };
	const string ds = "DRUL";
	const int T = 1000;
	const double wallp = 150.0 / 760.0; // (= ~19.7%)
	vector<vector<double> > wallx(H - 1, vector<double>(W, 0.0)); // wall between (x, y) and (x+1, y)
	vector<vector<double> > wally(H, vector<double>(W - 1, 0.0)); // wall between (x, y) and (x, y+1)
	
	// ---------- GAME LOOP ---------- //
	int iteration = 1;
	while (true) {
		// step #1. pre-process
		vector<vector<double> > maxprob(H, vector<double>(W, 0.0));
		maxprob[0][0] = 1.0;
		priority_queue<state> que;
		que.push(state(0, 0, 1.0));
		vector<vector<bool> > vis(H, vector<bool>(W, false));
		vector<vector<pair<int, int> > > precoord(H, vector<pair<int, int> >(W, make_pair(-1, -1)));
		while (!que.empty()) {
			state u = que.top();
			que.pop();
			if (vis[u.x][u.y]) {
				continue;
			}
			vis[u.x][u.y] = true;
			for (int i = 0; i < 4; i++) {
				int nx = u.x + dx[i], wx = (dx[i] == -1 ? u.x - 1 : u.x);
				int ny = u.y + dy[i], wy = (dy[i] == -1 ? u.y - 1 : u.y);
				if (0 <= nx && nx < H && 0 <= ny && ny < W) {
					int wallstate = (dx[i] != 0 ? wallx[wx][wy] : wally[wx][wy]);
					double nprob = u.prob * (1.0 - (wallstate == -1 ? 0.0 : wallp / (wallp + (1.0 - wallp) * pow(p, wallstate)))) * (1.0 - p * 2.7);
					nprob *= 1.0 - randouble() * 1.0e-7;
					if (maxprob[nx][ny] < nprob) {
						maxprob[nx][ny] = nprob;
						precoord[nx][ny] = make_pair(u.x, u.y);
						que.push(state(nx, ny, maxprob[nx][ny]));
					}
				}
			}
		}
		
		// step #2. calculate & output the track
		pair<int, int> cur = make_pair(H - 1, W - 1);
		string str;
		while (cur != make_pair(0, 0)) {
			pair<int, int> nxt = precoord[cur.first][cur.second];
			for (int i = 0; i < 4; i++) {
				if (nxt.first + dx[i] == cur.first && nxt.second + dy[i] == cur.second) {
					str += ds[i];
					break;
				}
			}
			cur = nxt;
		}
		reverse(str.begin(), str.end());
		cout << str << endl;
		cerr << "#" << iteration << ": prob = " << maxprob[H - 1][W - 1] << endl;
		
		// step #3. read input
		int res;
		cin >> res;
		if (res == -1) {
			return iteration;
		}
		
		// step #4. after-process
		double topwall = 0.0;
		int curx = 0, cury = 0, wallcnt = 0;
		for (int i = 0; i < int(str.size()); i++) {
			int dir = ds.find(str[i]);
			int nx = curx + dx[dir], wx = (dx[dir] == -1 ? curx - 1 : curx);
			int ny = cury + dy[dir], wy = (dy[dir] == -1 ? cury - 1 : cury);
			int subwall = -1;
			if (dx[dir] != 0 && wallx[wx][wy] != -1) {
				if (i <= res) {
					wallx[wx][wy] = (i != res ? -1 : wallx[wx][wy] + 1);
					subwall = wallx[wx][wy];
				}
				else {
					wallcnt += 1;
				}
			}
			if (dy[dir] != 0 && wally[wx][wy] != -1) {
				if (i <= res) {
					wally[wx][wy] = (i != res ? -1 : wally[wx][wy] + 1);
					subwall = wally[wx][wy];
				}
				else {
					wallcnt += 1;
				}
			}
			if (i == res) {
				topwall = (subwall == -1 ? 0.0 : wallp / (wallp + (1.0 - wallp) * pow(p, subwall)));
			}
			curx = nx;
			cury = ny;
		}
		curx = 0, cury = 0;
		for (int i = 0; i < int(str.size()); i++) {
			int dir = ds.find(str[i]);
			int nx = curx + dx[dir], wx = (dx[dir] == -1 ? curx - 1 : curx);
			int ny = cury + dy[dir], wy = (dy[dir] == -1 ? cury - 1 : cury);
			int roads = min(wx + wy, H + W - 1 - wx - wy);
			if (i > res) {
				if (dx[dir] != 0 && wallx[wx][wy] != -1) {
					wallx[wx][wy] += (1.0 - topwall) / wallcnt * roads / (H + W) * 1.5;
				}
				if (dy[dir] != 0 && wally[wx][wy] != -1) {
					wally[wx][wy] += (1.0 - topwall) / wallcnt * roads / (H + W) * 1.5;
				}
			}
			curx = nx;
			cury = ny;
		}
		iteration += 1;
	}
	return -1;
}

int main() {
	int H, W, PN;
	cin >> H >> W >> PN;
	int ops = solve(H, W, double(PN) / 100);
	cerr << "# of operations: " << ops << endl;
	cerr << "Score: " << (ops != -1 ? 1001 - ops : -1) << endl;
	return 0;
}
0