Submission #258107

# Submission time Handle Problem Language Result Execution time Memory
258107 2020-08-05T11:18:06 Z sjimed Sandwich (JOI16_sandwich) C++14
100 / 100
5237 ms 10772 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(NULL)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define em emplace
#define all(v) (v).begin(), (v).end()
#define pre(a) cout<<fixed; cout.precision(a)

#define mp make_pair

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf = 1e9;
const ll INF = 1e18;

int n, m;
string s[444];
int chk[444][444];
int ans[444][444];

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

int t[2][2][2] = {
	1, 2,
	0, 3,
	2, 3,
	0, 1
};

int dir[2][4] = {
	1, 0, 0, 1,
	1, 1, 0, 0
};

int dfs(int x, int y, int d) {
	if(chk[x][y] == 1) return inf;
	if(chk[x][y] == 2) return 0;

	//cout << x << "!!! " << y << " " << d << endl;

	chk[x][y]=1;

	int ret = 2;

	for(int i=0; i<2; i++) {
		int nx = x + dx[t[s[x][y]][d][i]];
		int ny = y + dy[t[s[x][y]][d][i]];

		if(nx < 0 || nx >= n || ny < 0 || ny >= m) continue;

		ret += dfs(nx, ny, dir[s[nx][ny]][t[s[x][y]][d][i]]);

		ret = min(inf, ret);
	}

	chk[x][y]=2;

	return ret;
}

int main() {
	fast;

	cin >> n >> m;

	for(int i=0; i<n; i++) {
		cin >> s[i];

		for(int j=0; j<m; j++) {
			if(s[i][j] == 'N') s[i][j] = 0;
			else s[i][j] = 1;

			ans[i][j] = inf;
		}
	}

	for(int j=0; j<m; j++) {

		memset(chk, 0, sizeof(chk));

		int sum = 0;
		
		for(int i=0; i<n; i++) {
			sum += dfs(i, j, 0);

			sum = min(sum, inf);

			ans[i][j] = min(ans[i][j], sum);
		}

		memset(chk, 0, sizeof(chk));

		sum = 0;
		
		for(int i=n-1; i>=0; i--) {
			sum += dfs(i, j, 1);

			sum = min(sum, inf);

			ans[i][j] = min(ans[i][j], sum);
		}		
	}

	for(int i=0; i<n; i++) {
		for(int j=0; j<m; j++) {
			if(ans[i][j] >= inf) cout << "-1 ";
			else cout << ans[i][j] << " ";
		}
		cout << "\n";
	}
}

Compilation message

sandwich.cpp: In function 'int dfs(int, int, int)':
sandwich.cpp:52:28: warning: array subscript has type 'char' [-Wchar-subscripts]
   int nx = x + dx[t[s[x][y]][d][i]];
                            ^
sandwich.cpp:53:28: warning: array subscript has type 'char' [-Wchar-subscripts]
   int ny = y + dy[t[s[x][y]][d][i]];
                            ^
sandwich.cpp:57:35: warning: array subscript has type 'char' [-Wchar-subscripts]
   ret += dfs(nx, ny, dir[s[nx][ny]][t[s[x][y]][d][i]]);
                                   ^
sandwich.cpp:57:46: warning: array subscript has type 'char' [-Wchar-subscripts]
   ret += dfs(nx, ny, dir[s[nx][ny]][t[s[x][y]][d][i]]);
                                              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 3 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 8 ms 1280 KB Output is correct
8 Correct 8 ms 1280 KB Output is correct
9 Correct 6 ms 1280 KB Output is correct
10 Correct 11 ms 1280 KB Output is correct
11 Correct 8 ms 1408 KB Output is correct
12 Correct 5 ms 1152 KB Output is correct
13 Correct 7 ms 1280 KB Output is correct
14 Correct 8 ms 1280 KB Output is correct
15 Correct 6 ms 1280 KB Output is correct
16 Correct 5 ms 1280 KB Output is correct
17 Correct 5 ms 1280 KB Output is correct
18 Correct 11 ms 1280 KB Output is correct
19 Correct 6 ms 1280 KB Output is correct
20 Correct 6 ms 1280 KB Output is correct
21 Correct 6 ms 1280 KB Output is correct
22 Correct 5 ms 1280 KB Output is correct
23 Correct 9 ms 1280 KB Output is correct
24 Correct 9 ms 1280 KB Output is correct
25 Correct 9 ms 1280 KB Output is correct
26 Correct 8 ms 1280 KB Output is correct
27 Correct 9 ms 1280 KB Output is correct
28 Correct 9 ms 1280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 1152 KB Output is correct
2 Correct 3 ms 1152 KB Output is correct
3 Correct 1 ms 1152 KB Output is correct
4 Correct 1 ms 1152 KB Output is correct
5 Correct 1 ms 1152 KB Output is correct
6 Correct 12 ms 1408 KB Output is correct
7 Correct 8 ms 1280 KB Output is correct
8 Correct 8 ms 1280 KB Output is correct
9 Correct 6 ms 1280 KB Output is correct
10 Correct 11 ms 1280 KB Output is correct
11 Correct 8 ms 1408 KB Output is correct
12 Correct 5 ms 1152 KB Output is correct
13 Correct 7 ms 1280 KB Output is correct
14 Correct 8 ms 1280 KB Output is correct
15 Correct 6 ms 1280 KB Output is correct
16 Correct 5 ms 1280 KB Output is correct
17 Correct 5 ms 1280 KB Output is correct
18 Correct 11 ms 1280 KB Output is correct
19 Correct 6 ms 1280 KB Output is correct
20 Correct 6 ms 1280 KB Output is correct
21 Correct 6 ms 1280 KB Output is correct
22 Correct 5 ms 1280 KB Output is correct
23 Correct 9 ms 1280 KB Output is correct
24 Correct 9 ms 1280 KB Output is correct
25 Correct 9 ms 1280 KB Output is correct
26 Correct 8 ms 1280 KB Output is correct
27 Correct 9 ms 1280 KB Output is correct
28 Correct 9 ms 1280 KB Output is correct
29 Correct 23 ms 1152 KB Output is correct
30 Correct 1 ms 1792 KB Output is correct
31 Correct 5237 ms 10772 KB Output is correct
32 Correct 5090 ms 10296 KB Output is correct
33 Correct 358 ms 3960 KB Output is correct
34 Correct 1570 ms 2960 KB Output is correct
35 Correct 1436 ms 2552 KB Output is correct
36 Correct 3 ms 1792 KB Output is correct
37 Correct 1986 ms 3064 KB Output is correct
38 Correct 1859 ms 3064 KB Output is correct
39 Correct 1899 ms 3064 KB Output is correct
40 Correct 2102 ms 3192 KB Output is correct
41 Correct 1465 ms 2552 KB Output is correct
42 Correct 2290 ms 3448 KB Output is correct
43 Correct 2235 ms 3616 KB Output is correct
44 Correct 2179 ms 3704 KB Output is correct
45 Correct 1456 ms 2956 KB Output is correct
46 Correct 1066 ms 2956 KB Output is correct
47 Correct 1192 ms 3064 KB Output is correct
48 Correct 636 ms 2636 KB Output is correct
49 Correct 971 ms 3064 KB Output is correct
50 Correct 1200 ms 3064 KB Output is correct
51 Correct 983 ms 3064 KB Output is correct
52 Correct 1152 ms 3064 KB Output is correct
53 Correct 1018 ms 3064 KB Output is correct
54 Correct 978 ms 2936 KB Output is correct
55 Correct 1004 ms 3064 KB Output is correct
56 Correct 1035 ms 2936 KB Output is correct
57 Correct 741 ms 2684 KB Output is correct
58 Correct 2533 ms 4236 KB Output is correct
59 Correct 2445 ms 3712 KB Output is correct
60 Correct 1643 ms 3576 KB Output is correct
61 Correct 2761 ms 3232 KB Output is correct
62 Correct 3055 ms 3116 KB Output is correct
63 Correct 3036 ms 3144 KB Output is correct