답안 #445845

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
445845 2021-07-19T20:41:20 Z Bungmint Mecho (IOI09_mecho) C++17
54 / 100
192 ms 5060 KB
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
using pi = pair<int, int>;
using vpi = vector<pair<int, int>>;
using pl = pair<ll, ll>;
using vl = vector<ll>;
using vpl = vector<pl>;
using ld = long double;

#define all(v) (v).begin(), (v).end()
#define ar array
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define lb lower_bound

template <typename T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &p)
{
    return os << '(' << p.first << ", " << p.second << ')';
}
template <typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type>
ostream &operator<<(ostream &os, const T_container &v)
{
    os << '{';
    string sep;
    for (const T &x : v)
        os << sep << x, sep = ", ";
    return os << '}';
}
void dbg_out()
{
    cerr << endl;
}
template <typename Head, typename... Tail>
void dbg_out(Head H, Tail... T)
{
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

struct custom_hash
{
    static uint64_t splitmix64(uint64_t x)
    {
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const
    {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};

void setIO(string s)
{
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7; //998244353;
int n, s;
int dx[] = {0,0,-1, 1}, dy[] = {-1, 1, 0,0};
char grid[801][801];
bool vis[801][801];

bool good(int x, int y){
	return (x>=0&&x<n&&y>=0&&y<n);
}


void solve()
{
	cin >> n >> s;
	for (int i=0;i<n;++i) for (int j=0;j<n;++j)cin >> grid[i][j];
	vector<vi> distFromHives(n, vi(n, INF));
	queue<pi> q;
	int mX, mY, homeX, homeY;
	for (int i=0;i<n;++i) for (int j=0;j<n;++j){
		if (grid[i][j]=='M'){
			mX = i, mY = j;
		}else if (grid[i][j]=='D'){
			homeX = i, homeY = j;
		}else if (grid[i][j] == 'H'){
			q.push({i,j});
			distFromHives[i][j] = 0;
		}
	}
	while(sz(q)){
		auto [x,y] = q.front();
		q.pop();
		for (int i=0;i<4;++i){
			int nX = x + dx[i], nY = y + dy[i];
			if (good(nX, nY)&&grid[nX][nY]!='D'&&grid[nX][nY]!='T'&&distFromHives[nX][nY]>distFromHives[x][y]+1){
				distFromHives[nX][nY] = distFromHives[x][y] + 1;
				q.push({nX, nY});
			}
		}
	}
	int l = 0, r = distFromHives[mX][mY]-1, ans = -1;
	while(l<=r){
		int start = l + (r-l)/2, t = start;
		queue<pi> nxt;
		queue<pair<int,pi>> cur;
		memset(vis, 0, sizeof(vis));
		vis[mX][mY] = 1;
		cur.push({0,{mX, mY}});
		while(sz(cur)){
			while(sz(cur)){
				int d = cur.front().fi;
				auto [x,y] = cur.front().se;
				cur.pop();
				if (d==s){
					nxt.push({x,y});
					continue;
				}
				for (int i=0;i<4;++i){
					int nX = x + dx[i], nY = y + dy[i];
					if (good(nX, nY)&&!vis[nX][nY]&&grid[nX][nY]!='T'&&distFromHives[nX][nY]>t){
						vis[nX][nY] = 1;
						cur.push({d+1,{nX, nY}});
					}
				}
			}
			while(sz(nxt)) cur.push({0, nxt.front()}), nxt.pop();
			t++;
		}
		
		if (vis[homeX][homeY]) ans = start, l = start+1;
		else r = start-1;
	}
	
	cout << ans << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int testcase=1;
    // cin >> testcase;
    while (testcase--)
    {
        solve();
    }
}

Compilation message

mecho.cpp: In function 'void setIO(std::string)':
mecho.cpp:74:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   74 |     freopen((s + ".in").c_str(), "r", stdin);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp:75:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   75 |     freopen((s + ".out").c_str(), "w", stdout);
      |     ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
mecho.cpp: In function 'void solve()':
mecho.cpp:121:37: warning: 'mY' may be used uninitialized in this function [-Wmaybe-uninitialized]
  121 |  int l = 0, r = distFromHives[mX][mY]-1, ans = -1;
      |                                     ^
mecho.cpp:121:33: warning: 'mX' may be used uninitialized in this function [-Wmaybe-uninitialized]
  121 |  int l = 0, r = distFromHives[mX][mY]-1, ans = -1;
      |                                 ^
mecho.cpp:150:23: warning: 'homeY' may be used uninitialized in this function [-Wmaybe-uninitialized]
  150 |   if (vis[homeX][homeY]) ans = start, l = start+1;
      |       ~~~~~~~~~~~~~~~~^
mecho.cpp:150:23: warning: 'homeX' may be used uninitialized in this function [-Wmaybe-uninitialized]
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 844 KB Output isn't correct
2 Incorrect 1 ms 844 KB Output isn't correct
3 Incorrect 1 ms 844 KB Output isn't correct
4 Incorrect 1 ms 844 KB Output isn't correct
5 Correct 1 ms 844 KB Output is correct
6 Correct 1 ms 844 KB Output is correct
7 Correct 102 ms 4736 KB Output is correct
8 Incorrect 1 ms 844 KB Output isn't correct
9 Correct 1 ms 844 KB Output is correct
10 Correct 1 ms 972 KB Output is correct
11 Correct 1 ms 844 KB Output is correct
12 Correct 1 ms 972 KB Output is correct
13 Correct 1 ms 972 KB Output is correct
14 Correct 1 ms 972 KB Output is correct
15 Incorrect 1 ms 844 KB Output isn't correct
16 Correct 1 ms 972 KB Output is correct
17 Incorrect 1 ms 972 KB Output isn't correct
18 Correct 1 ms 968 KB Output is correct
19 Incorrect 1 ms 972 KB Output isn't correct
20 Correct 1 ms 972 KB Output is correct
21 Incorrect 1 ms 972 KB Output isn't correct
22 Correct 1 ms 968 KB Output is correct
23 Incorrect 1 ms 972 KB Output isn't correct
24 Correct 1 ms 972 KB Output is correct
25 Incorrect 1 ms 972 KB Output isn't correct
26 Correct 1 ms 972 KB Output is correct
27 Incorrect 1 ms 972 KB Output isn't correct
28 Correct 1 ms 972 KB Output is correct
29 Incorrect 1 ms 972 KB Output isn't correct
30 Correct 1 ms 972 KB Output is correct
31 Incorrect 1 ms 972 KB Output isn't correct
32 Correct 1 ms 972 KB Output is correct
33 Incorrect 7 ms 1740 KB Output isn't correct
34 Correct 7 ms 1740 KB Output is correct
35 Correct 24 ms 1740 KB Output is correct
36 Incorrect 9 ms 2004 KB Output isn't correct
37 Correct 8 ms 1960 KB Output is correct
38 Correct 32 ms 2004 KB Output is correct
39 Incorrect 11 ms 2252 KB Output isn't correct
40 Correct 10 ms 2196 KB Output is correct
41 Correct 40 ms 2252 KB Output is correct
42 Incorrect 13 ms 2476 KB Output isn't correct
43 Correct 13 ms 2520 KB Output is correct
44 Correct 50 ms 2536 KB Output is correct
45 Incorrect 15 ms 2764 KB Output isn't correct
46 Correct 14 ms 2772 KB Output is correct
47 Correct 60 ms 2892 KB Output is correct
48 Incorrect 17 ms 3148 KB Output isn't correct
49 Correct 17 ms 3108 KB Output is correct
50 Correct 71 ms 3104 KB Output is correct
51 Incorrect 20 ms 3556 KB Output isn't correct
52 Correct 20 ms 3532 KB Output is correct
53 Correct 88 ms 3548 KB Output is correct
54 Incorrect 23 ms 3900 KB Output isn't correct
55 Correct 22 ms 3812 KB Output is correct
56 Correct 99 ms 3824 KB Output is correct
57 Incorrect 27 ms 4300 KB Output isn't correct
58 Correct 26 ms 4320 KB Output is correct
59 Correct 115 ms 4300 KB Output is correct
60 Incorrect 31 ms 4640 KB Output isn't correct
61 Correct 30 ms 4624 KB Output is correct
62 Correct 160 ms 4620 KB Output is correct
63 Correct 127 ms 4804 KB Output is correct
64 Correct 174 ms 4620 KB Output is correct
65 Correct 192 ms 4620 KB Output is correct
66 Incorrect 154 ms 4856 KB Output isn't correct
67 Correct 142 ms 4676 KB Output is correct
68 Correct 52 ms 4640 KB Output is correct
69 Correct 47 ms 4664 KB Output is correct
70 Correct 43 ms 4644 KB Output is correct
71 Correct 37 ms 4756 KB Output is correct
72 Correct 41 ms 4636 KB Output is correct
73 Correct 51 ms 4896 KB Output is correct
74 Correct 70 ms 4880 KB Output is correct
75 Correct 70 ms 4888 KB Output is correct
76 Correct 76 ms 4924 KB Output is correct
77 Correct 77 ms 5004 KB Output is correct
78 Correct 89 ms 4980 KB Output is correct
79 Correct 66 ms 4932 KB Output is correct
80 Correct 77 ms 4932 KB Output is correct
81 Correct 80 ms 4860 KB Output is correct
82 Correct 74 ms 4884 KB Output is correct
83 Correct 104 ms 4940 KB Output is correct
84 Correct 83 ms 5060 KB Output is correct
85 Correct 82 ms 4836 KB Output is correct
86 Correct 94 ms 4884 KB Output is correct
87 Correct 83 ms 4936 KB Output is correct
88 Correct 101 ms 4884 KB Output is correct
89 Correct 96 ms 4792 KB Output is correct
90 Correct 102 ms 4888 KB Output is correct
91 Correct 99 ms 4780 KB Output is correct
92 Correct 99 ms 4812 KB Output is correct