Submission #4065161


Source Code Expand

#include<stack>
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

const int dy[] = {0,1,0,-1};
const int dx[] = {1,0,-1,0};

int N, M, K;
int si, sj;
char F[811][811];
int W[811][811];
int D[811][811];

bool in(int y, int x) {
    return 0 <= y && y < N && 0 <= x && x < M;
}

void MAIN() {
    scanf("%d%d%d", &N, &M, &K);
    REP (i, N) scanf("%s", F[i]);

    REP (i, N) REP (j, M) if (F[i][j] == 'S') {
	si = i;
	sj = j;
    }
    memset(W, 0x3f, sizeof W);
    const int INF = W[0][0];

    stack<pair<int, int> > cur, nxt;
    W[si][sj] = 0;
    cur.emplace(si, sj);
    while (1) {
	if (cur.empty()) swap(cur, nxt);
	if (cur.empty()) break;

	int y = cur.top().first;
	int x = cur.top().second;
	cur.pop();
	REP (d, 4) {
	    int yy = y + dy[d];
	    int xx = x + dx[d];
	    if (in(yy, xx) && W[yy][xx] == INF) {
		if (F[yy][xx] == '.') {
		    W[yy][xx] = W[y][x];
		    cur.emplace(yy, xx);
		} else {
		    W[yy][xx] = W[y][x] + 1;
		    nxt.emplace(yy, xx);
		}
	    }
	}
    }

    memset(D, 0x3f, sizeof D);
    D[si][sj] = 0;
    cur.emplace(si, sj);
    stack<pair<int, int> > nnxt;

    int ans = 1;
    while (1) {
	bool ok = false;
	REP (i, K) {
	    if (cur.empty()) break;
	    while (!cur.empty()) {
		int y = cur.top().first;
		int x = cur.top().second;
		cur.pop();
		nnxt.emplace(y, x);
		REP (d, 4) {
		    int yy = y + dy[d];
		    int xx = x + dx[d];
		    if (in(yy, xx) && D[yy][xx] == INF && W[yy][xx] <= (ans - 1) * K) {
			D[yy][xx] = D[y][x] + 1;
			nxt.emplace(yy, xx);
			if (yy == 0 || yy == N-1 || xx == 0 || xx == M-1) ok = true;
		    }
		}
	    }
	    swap(cur, nxt);
	}

	while (!cur.empty()) cur.pop();
	swap(cur, nnxt);

	if (ok) {
	    break;
	} else {
	    ans++;
	}
    }

    printf("%d\n", ans);
}


int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}

Submission Info

Submission Time
Task C - Closed Rooms
User natsugiri
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2639 Byte
Status TLE
Exec Time 2107 ms
Memory 11264 KB

Compile Error

./Main.cpp: In function ‘void MAIN()’:
./Main.cpp:38:32: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d%d", &N, &M, &K);
                                ^
./Main.cpp:39:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     REP (i, N) scanf("%s", F[i]);
                                 ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 700
Status
AC × 3
AC × 21
TLE × 25
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt TLE 2103 ms 6144 KB
in10.txt TLE 2103 ms 6016 KB
in11.txt TLE 2103 ms 6016 KB
in12.txt TLE 2103 ms 6144 KB
in13.txt TLE 2103 ms 6144 KB
in14.txt TLE 2103 ms 6144 KB
in15.txt TLE 2103 ms 6144 KB
in16.txt TLE 2103 ms 6144 KB
in17.txt TLE 2103 ms 6144 KB
in18.txt TLE 2107 ms 6144 KB
in19.txt TLE 2103 ms 6144 KB
in2.txt AC 42 ms 11008 KB
in20.txt AC 3 ms 6016 KB
in21.txt AC 3 ms 5376 KB
in22.txt AC 3 ms 5376 KB
in23.txt AC 24 ms 6016 KB
in24.txt TLE 2103 ms 6016 KB
in25.txt TLE 2103 ms 6016 KB
in26.txt TLE 2104 ms 7352 KB
in27.txt TLE 2104 ms 7416 KB
in28.txt AC 30 ms 7344 KB
in29.txt AC 38 ms 8264 KB
in3.txt TLE 2103 ms 6144 KB
in30.txt AC 34 ms 8368 KB
in31.txt TLE 2104 ms 7316 KB
in32.txt TLE 2104 ms 7352 KB
in33.txt AC 38 ms 11156 KB
in34.txt AC 38 ms 11180 KB
in35.txt TLE 2104 ms 8104 KB
in36.txt AC 44 ms 11264 KB
in37.txt TLE 2103 ms 6016 KB
in38.txt TLE 2103 ms 6016 KB
in39.txt TLE 2104 ms 8448 KB
in4.txt AC 45 ms 11264 KB
in40.txt TLE 2104 ms 8448 KB
in5.txt AC 42 ms 11264 KB
in6.txt TLE 2103 ms 6144 KB
in7.txt AC 24 ms 6144 KB
in8.txt AC 42 ms 11008 KB
in9.txt TLE 2103 ms 6144 KB
sample1.txt AC 3 ms 5376 KB
sample2.txt AC 3 ms 5376 KB
sample3.txt AC 3 ms 5376 KB