#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(x.begin(), x.end())
#define press(x) x.erase(unique(x.begin(), x.end()), x.end())
#define lb(x, v) lower_bound(x.begin(), x.end(), v)
#define ub(x, v) upper_bound(x.begin(), x.end(), v)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=2000000000000000000;
const LL mod1=1000000007;
const LL mod2=998244353;
const int inf=2000000000;
int n, m;
char str[410][410];
int ans[410][410], sum;
int ch[410][410];
int mx[]={-1, 0, 1, 0}, my[]={0, 1, 0, -1};
void dfs(int x, int y, int dir){
if(ch[x][y]==2||!str[x][y])return;
if(ch[x][y]==1){
sum=inf;
return;
}
ch[x][y]=1;
sum=min(sum+2, inf);
if(str[x][y]=='N'){
if(dir<=1){
dfs(x+mx[0], y+my[0], 0);
dfs(x+mx[1], y+my[1], 1);
}
else{
dfs(x+mx[2], y+my[2], 2);
dfs(x+mx[3], y+my[3], 3);
}
}
else{
if(dir==0||dir==3){
dfs(x+mx[0], y+my[0], 0);
dfs(x+mx[3], y+my[3], 3);
}
else{
dfs(x+mx[1], y+my[1], 1);
dfs(x+mx[2], y+my[2], 2);
}
}
ch[x][y]=2;
}
int main(){
scanf("%d %d", &n, &m);
for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)ans[i][j]=inf;
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
dfs(i, j, 3);
ans[i][j]=min(ans[i][j], sum);
}
sum=0;
memset(ch, 0, sizeof ch);
for(int j=m; j>=1; j--){
dfs(i, j, 1);
ans[i][j]=min(ans[i][j], sum);
}
sum=0;
memset(ch, 0, sizeof ch);
}
for(int j=1; j<=m; j++){
for(int i=1; i<=n; i++){
dfs(i, j, 0);
ans[i][j]=min(ans[i][j], sum);
}
sum=0;
memset(ch, 0, sizeof ch);
for(int i=n; i>=1; i--){
dfs(i, j, 2);
ans[i][j]=min(ans[i][j], sum);
}
sum=0;
memset(ch, 0, sizeof ch);
}
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++)printf("%d ", ans[i][j]==inf?-1:ans[i][j]);
puts("");
}
}
Compilation message
sandwich.cpp: In function 'int main()':
sandwich.cpp:60:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
sandwich.cpp:61:33: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=n; i++)scanf("%s", str[i]+1);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
15 ms |
1152 KB |
Output is correct |
7 |
Correct |
10 ms |
1024 KB |
Output is correct |
8 |
Correct |
11 ms |
1024 KB |
Output is correct |
9 |
Correct |
8 ms |
1024 KB |
Output is correct |
10 |
Correct |
10 ms |
1152 KB |
Output is correct |
11 |
Correct |
12 ms |
1152 KB |
Output is correct |
12 |
Correct |
7 ms |
1024 KB |
Output is correct |
13 |
Correct |
12 ms |
1152 KB |
Output is correct |
14 |
Correct |
19 ms |
1156 KB |
Output is correct |
15 |
Correct |
9 ms |
1152 KB |
Output is correct |
16 |
Correct |
7 ms |
1152 KB |
Output is correct |
17 |
Correct |
11 ms |
1152 KB |
Output is correct |
18 |
Correct |
8 ms |
1152 KB |
Output is correct |
19 |
Correct |
8 ms |
1024 KB |
Output is correct |
20 |
Correct |
8 ms |
1152 KB |
Output is correct |
21 |
Correct |
8 ms |
1152 KB |
Output is correct |
22 |
Correct |
16 ms |
1152 KB |
Output is correct |
23 |
Correct |
9 ms |
1152 KB |
Output is correct |
24 |
Correct |
12 ms |
1152 KB |
Output is correct |
25 |
Correct |
16 ms |
1152 KB |
Output is correct |
26 |
Correct |
20 ms |
1152 KB |
Output is correct |
27 |
Correct |
10 ms |
1152 KB |
Output is correct |
28 |
Correct |
10 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1024 KB |
Output is correct |
2 |
Correct |
3 ms |
1024 KB |
Output is correct |
3 |
Correct |
3 ms |
1024 KB |
Output is correct |
4 |
Correct |
1 ms |
1024 KB |
Output is correct |
5 |
Correct |
3 ms |
1024 KB |
Output is correct |
6 |
Correct |
15 ms |
1152 KB |
Output is correct |
7 |
Correct |
10 ms |
1024 KB |
Output is correct |
8 |
Correct |
11 ms |
1024 KB |
Output is correct |
9 |
Correct |
8 ms |
1024 KB |
Output is correct |
10 |
Correct |
10 ms |
1152 KB |
Output is correct |
11 |
Correct |
12 ms |
1152 KB |
Output is correct |
12 |
Correct |
7 ms |
1024 KB |
Output is correct |
13 |
Correct |
12 ms |
1152 KB |
Output is correct |
14 |
Correct |
19 ms |
1156 KB |
Output is correct |
15 |
Correct |
9 ms |
1152 KB |
Output is correct |
16 |
Correct |
7 ms |
1152 KB |
Output is correct |
17 |
Correct |
11 ms |
1152 KB |
Output is correct |
18 |
Correct |
8 ms |
1152 KB |
Output is correct |
19 |
Correct |
8 ms |
1024 KB |
Output is correct |
20 |
Correct |
8 ms |
1152 KB |
Output is correct |
21 |
Correct |
8 ms |
1152 KB |
Output is correct |
22 |
Correct |
16 ms |
1152 KB |
Output is correct |
23 |
Correct |
9 ms |
1152 KB |
Output is correct |
24 |
Correct |
12 ms |
1152 KB |
Output is correct |
25 |
Correct |
16 ms |
1152 KB |
Output is correct |
26 |
Correct |
20 ms |
1152 KB |
Output is correct |
27 |
Correct |
10 ms |
1152 KB |
Output is correct |
28 |
Correct |
10 ms |
1152 KB |
Output is correct |
29 |
Correct |
19 ms |
1044 KB |
Output is correct |
30 |
Correct |
20 ms |
1792 KB |
Output is correct |
31 |
Correct |
7017 ms |
7104 KB |
Output is correct |
32 |
Correct |
7430 ms |
7308 KB |
Output is correct |
33 |
Correct |
1002 ms |
3064 KB |
Output is correct |
34 |
Correct |
1827 ms |
2740 KB |
Output is correct |
35 |
Correct |
1575 ms |
2376 KB |
Output is correct |
36 |
Correct |
44 ms |
1792 KB |
Output is correct |
37 |
Correct |
2240 ms |
2768 KB |
Output is correct |
38 |
Correct |
2029 ms |
2808 KB |
Output is correct |
39 |
Correct |
2186 ms |
2808 KB |
Output is correct |
40 |
Correct |
3607 ms |
3100 KB |
Output is correct |
41 |
Correct |
1874 ms |
2296 KB |
Output is correct |
42 |
Correct |
2822 ms |
3120 KB |
Output is correct |
43 |
Correct |
2746 ms |
3252 KB |
Output is correct |
44 |
Correct |
2750 ms |
3364 KB |
Output is correct |
45 |
Correct |
1782 ms |
2740 KB |
Output is correct |
46 |
Correct |
1154 ms |
2740 KB |
Output is correct |
47 |
Correct |
1436 ms |
3068 KB |
Output is correct |
48 |
Correct |
830 ms |
2552 KB |
Output is correct |
49 |
Correct |
1205 ms |
2736 KB |
Output is correct |
50 |
Correct |
1338 ms |
2936 KB |
Output is correct |
51 |
Correct |
1185 ms |
2808 KB |
Output is correct |
52 |
Correct |
1259 ms |
2736 KB |
Output is correct |
53 |
Correct |
1194 ms |
2808 KB |
Output is correct |
54 |
Correct |
1119 ms |
2680 KB |
Output is correct |
55 |
Correct |
1200 ms |
2808 KB |
Output is correct |
56 |
Correct |
1187 ms |
2808 KB |
Output is correct |
57 |
Correct |
888 ms |
2552 KB |
Output is correct |
58 |
Correct |
3123 ms |
3568 KB |
Output is correct |
59 |
Correct |
3066 ms |
3332 KB |
Output is correct |
60 |
Correct |
2125 ms |
3064 KB |
Output is correct |
61 |
Correct |
3177 ms |
2996 KB |
Output is correct |
62 |
Correct |
2626 ms |
2936 KB |
Output is correct |
63 |
Correct |
2630 ms |
2920 KB |
Output is correct |