結果

問題 No.331 CodeRunnerでやれ
ユーザー antaanta
提出日時 2015-12-25 02:05:06
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 376 ms / 5,000 ms
コード長 3,678 bytes
コンパイル時間 1,065 ms
コンパイル使用メモリ 97,368 KB
実行使用メモリ 24,240 KB
平均クエリ数 390.59
最終ジャッジ日時 2023-09-23 23:00:32
合計ジャッジ時間 7,064 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
23,508 KB
testcase_01 AC 159 ms
23,232 KB
testcase_02 AC 189 ms
23,644 KB
testcase_03 AC 197 ms
23,872 KB
testcase_04 AC 179 ms
23,544 KB
testcase_05 AC 184 ms
24,120 KB
testcase_06 AC 212 ms
23,664 KB
testcase_07 AC 235 ms
24,012 KB
testcase_08 AC 235 ms
23,896 KB
testcase_09 AC 324 ms
23,476 KB
testcase_10 AC 242 ms
23,872 KB
testcase_11 AC 318 ms
24,240 KB
testcase_12 AC 274 ms
23,868 KB
testcase_13 AC 291 ms
23,268 KB
testcase_14 AC 311 ms
24,120 KB
testcase_15 AC 272 ms
23,656 KB
testcase_16 AC 376 ms
23,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

static const int dy[4] = {0, 1, 0, -1}, dx[4] = {1, 0, -1, 0};
int inity, initx, initd;
int curd, cury, curx;
vector<string> maze;
queue<int> myinq;
int queries;

void addtoq() {
	if(maze[cury][curx] == 'G') {
		myinq.push(-1);
	} else {
		int y = cury, x = curx, k = 0;
		while(1) {
			y += dy[curd], x += dx[curd];
			if(maze[y][x] == '#') {
				myinq.push(k);
				return;
			}
			if(maze[y][x] == 'G') {
				myinq.push(INF);
				return;
			}
			++ k;
		}
	}
}

void myout(const char *s) {
	if(*s == 'R') (++ curd) %= 4;
	else if(*s == 'L') (curd += 3) %= 4;
	else if(*s == 'F') cury += dy[curd], curx += dx[curd];
	else if(*s == 'B') cury -= dy[curd], curx -= dx[curd];
	puts(s); fflush(stdout);
#ifdef MY_LOCAL_RUN
	++ queries;
	if(maze[cury][curx] == '#') cerr << "err 1" << endl, abort();
	addtoq();
#endif
}

int myin() {
#ifdef MY_LOCAL_RUN
	if(myinq.empty()) {
		cerr << "err 4" << endl, abort();
		return 0;
	}
	int r = myinq.front();
	myinq.pop();
	return r;
#else
	int t = -1;
	scanf("%d", &t);
	return t;
#endif
}

set<pii> vis;
map<pii, bool> info;

int turn(int d, int e) {
	if(d <= e ? (e - d < d + 4 - e) : (e + 4 - d < d - e)) {
		while(d != e) myin(), myout("R"), (d += 1) %= 4;
	} else {
		while(d != e) myin(), myout("L"), (d += 3) %= 4;
	}
	return d;
}

bool dfs(int i, int j, int d0) {
	if(cury != i + inity || curx != j + initx || curd != (d0 + initd) % 4) {
		cerr << "err 2" << endl, abort();
	}
	if(vis.count(mp(i, j))) return false;
	vis.insert(mp(i, j));
	int nextds[4]; int k = 0;
	rep(di, 4) {
		int d = (d0 + di) % 4;
		int yy = i + dy[d], xx = j + dx[d];
		int t = myin();
		if(t == -1) return true;
		if(t > 100) {
			do {
				myout("F");
			} while(myin() != -1);
			return true;
		}
		for(int p = 0; p < t; ++ p)
			info[mp(i + dy[d] * p, j + dx[d] * p)] = false;
		info[mp(i + dy[d] * t, j + dx[d] * t)] = true;
		if(t > 0 && !vis.count(mp(yy, xx)))
			nextds[k ++] = d;
		myout("R");
	}
	int dd = d0;
	rep(kk, k) {
		int d = nextds[kk];
		dd = turn(dd, d);
		myin(), myout("F");
		if(dfs(i + dy[d], j + dx[d], d))
			return true;
		myin(), myout("B");
	}
	dd = turn(dd, d0);
	return false;
}

int main() {
	maze = {
		"########",
		"#..#.#.#",
		"#..#...#",
		"#..#.#.G",
		"#....#.#",
		"########",
	};
	inity = 1, initx = 1, initd = 0;
	cury = inity, curx = initx, curd = initd;
	queries = 0;
	addtoq();
	bool ok = dfs(0, 0, 0);
	if(!ok) {
		cerr << "err 3" << endl, abort();
	} else {
#ifdef MY_LOCAL_RUN
		cerr << "OK: " << queries << endl;
#endif
	}
	return 0;
}
0