結果

問題 No.971 いたずらっ子
ユーザー FF256grhyFF256grhy
提出日時 2020-01-17 22:05:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 806 ms / 2,000 ms
コード長 2,780 bytes
コンパイル時間 1,832 ms
コンパイル使用メモリ 176,664 KB
実行使用メモリ 26,596 KB
最終ジャッジ日時 2023-08-07 07:55:21
合計ジャッジ時間 8,958 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 800 ms
26,596 KB
testcase_01 AC 806 ms
26,556 KB
testcase_02 AC 584 ms
18,992 KB
testcase_03 AC 682 ms
26,240 KB
testcase_04 AC 643 ms
24,992 KB
testcase_05 AC 682 ms
26,340 KB
testcase_06 AC 498 ms
18,632 KB
testcase_07 AC 6 ms
4,380 KB
testcase_08 AC 150 ms
9,436 KB
testcase_09 AC 148 ms
9,252 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); };
auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); };
auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; };
LL gcd(LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)

// ---- ----

template<typename T> using IPQ = priority_queue<T, vector<T>, greater<T>>;
template<typename T> using DPQ = priority_queue<T, vector<T>,    less<T>>;

template<typename T> T MV(T v) { return v; }
template<typename T, typename... R> auto MV(T v, int s, R... r) { return vector<decltype(MV(v, r...))>(s, MV(v, r...)); }

int main() {
	int h, w;
	cin >> h >> w;
	vector<string> s(h);
	inc(i, h) { cin >> s[i]; }
	
	int INF = 1e9;
	IPQ<array<int, 3>> q;
	auto dist = MV(INF, h, w);
	
	auto add = [&](int d, int i, int j) {
		if(setmin(dist[i][j], d)) { q.push({ d, i, j }); }
	};
	
	add(0, 0, 0);
	until(q.empty()) {
		auto p = q.top(); q.pop();
		int d = p[0];
		int i = p[1];
		int j = p[2];
		if(dist[i][j] != d) { continue; }
		
		inc(k, 2) {
			int di[9] = { +1,  0, -1,  0, +1, +1, -1, -1,  0 };
			int dj[9] = {  0, +1,  0, -1, +1, -1, +1, -1,  0 };
			int ii = i + di[k];
			int jj = j + dj[k];
			if(! (inID(ii, 0, h) && inID(jj, 0, w))) { continue; }
			
			add(d + 1 + (s[ii][jj] == '.' ? 0 : ii + jj), ii, jj);
		}
	}
	
	cout << dist.back().back() << endl;
	
	return 0;
}
0