#include <bits/stdc++.h>
using namespace std;
#define mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) x = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ii> vii;
#define MAXN 1005
#define MAXM 10
#define MAXPOW 1050
#define INF 1000000005
int n, m;
char grid[MAXN][MAXM];
int dp[MAXN][MAXM][MAXPOW];
int ansi = -1, ansj;
int main() {
scanf("%d%d", &n, &m);
REP (i, 1, n + 1) {
scanf(" %s", &grid[i]);
}
REP (j, 0, m) {
REP (msk, 0, 1 << m) {
dp[0][j][msk] = msk == 0 ? 0 : INF;
}
}
REP (i, 1, n + 1) {
REP (j, 0, m) {
if (grid[i][j] == '#') {
ansi = i;
ansj = j;
}
int prvj = j - 1, prvi = i;
if (prvj < 0) {
prvj = m - 1;
prvi--;
}
REP (msk, 0, 1 << m) {
if (msk & (1 << j)) {
if (grid[i][j] == '.') {
dp[i][j][msk] = INF;
} else {
dp[i][j][msk] = min(dp[prvi][prvj][msk], dp[prvi][prvj][msk ^ (1 << j)] + 1);
}
} else {
dp[i][j][msk] = min(dp[prvi][prvj][msk], dp[prvi][prvj][msk ^ (1 << j)]);
if (grid[i][j] == '.') continue;
if (j == 0 || grid[i][j - 1] == '.' || msk & (1 << j - 1)) dp[i][j][msk]++;
}
}
}
}
int ans = INF;
if (ansi == -1) ans = 0;
else {
REP (msk, 0, 1 << m) {
ans = min(dp[ansi][ansj][msk], ans);
}
}
printf("%d\n", ans);
return 0;
}
/*
2 3
#.#
###
4 3
.#.
###
.##
.#.
4 4
####
#.#.
#.##
####
*/
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:35:12: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[10]' [-Wformat=]
35 | scanf(" %s", &grid[i]);
| ~^ ~~~~~~~~
| | |
| | char (*)[10]
| char*
Main.cpp:63:59: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
63 | if (j == 0 || grid[i][j - 1] == '.' || msk & (1 << j - 1)) dp[i][j][msk]++;
| ~~^~~
Main.cpp:33:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
33 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:35:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
35 | scanf(" %s", &grid[i]);
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
38 ms |
38252 KB |
Output is correct |
3 |
Correct |
22 ms |
31468 KB |
Output is correct |
4 |
Correct |
29 ms |
38124 KB |
Output is correct |
5 |
Correct |
41 ms |
40812 KB |
Output is correct |
6 |
Correct |
40 ms |
40940 KB |
Output is correct |
7 |
Correct |
47 ms |
39916 KB |
Output is correct |
8 |
Correct |
38 ms |
38252 KB |
Output is correct |
9 |
Correct |
39 ms |
38380 KB |
Output is correct |
10 |
Correct |
47 ms |
41416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
748 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
748 KB |
Output is correct |
5 |
Correct |
1 ms |
748 KB |
Output is correct |
6 |
Correct |
1 ms |
748 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
38 ms |
38252 KB |
Output is correct |
3 |
Correct |
22 ms |
31468 KB |
Output is correct |
4 |
Correct |
29 ms |
38124 KB |
Output is correct |
5 |
Correct |
41 ms |
40812 KB |
Output is correct |
6 |
Correct |
40 ms |
40940 KB |
Output is correct |
7 |
Correct |
47 ms |
39916 KB |
Output is correct |
8 |
Correct |
38 ms |
38252 KB |
Output is correct |
9 |
Correct |
39 ms |
38380 KB |
Output is correct |
10 |
Correct |
47 ms |
41416 KB |
Output is correct |
11 |
Correct |
1 ms |
748 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
748 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
16 |
Correct |
1 ms |
748 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
748 KB |
Output is correct |
19 |
Correct |
11 ms |
20204 KB |
Output is correct |
20 |
Correct |
22 ms |
33772 KB |
Output is correct |
21 |
Correct |
30 ms |
39276 KB |
Output is correct |
22 |
Correct |
39 ms |
40140 KB |
Output is correct |
23 |
Correct |
42 ms |
39916 KB |
Output is correct |
24 |
Correct |
39 ms |
39660 KB |
Output is correct |
25 |
Correct |
41 ms |
40684 KB |
Output is correct |
26 |
Correct |
39 ms |
39020 KB |
Output is correct |
27 |
Correct |
40 ms |
39168 KB |
Output is correct |
28 |
Correct |
40 ms |
40428 KB |
Output is correct |