#include <bits/stdc++.h>
#define sz(x) (int)(x.size())
#define debug printf
#define lp(i,a,b) for(int i = a ; i < b; i++)
#define pb push_back
#define ff first
#define ss second
#define mk make_pair
#define pii pair<int,int>
#define ll long long
#define all(x) x.begin(),x.end()
const int MAXN = 1010 ;
const int MAX = 1e4+10 ;
using namespace std ;
int N , M , toGet, toFill = 1 , inf ;
int dp[2][(1<<11)] ;
char grid[MAXN][12] ;
vector<int> freq[MAX+100] ;
bool isOn(int m, int i ) { return ((1<<i)&m) != 0 ; }
void dijkstra(int l)
{
for(int i = 0 ; i < MAX ; i++ )
while( !freq[i].empty() )
{
int e = freq[i][0] ;
if( dp[toFill][e] < i )
{
freq[i][0] = freq[i].back() ;
freq[i].pop_back() ;
continue ;
}
vector<pair<int,int> > neigh ;
for(int g = 0 ; g < M ; g++ )
{
if(grid[l][g] == '#')
neigh.push_back( make_pair(e^(1<<g) , i+!isOn(e,g) ) ) ;
}
for(auto viz : neigh )
{
if(viz.first == 1 && l == 0 && viz.second == 1 ) cout << "----- " << e << endl ;
if( dp[toFill][viz.first] <= viz.second ) continue ;
freq[ dp[toFill][viz.first] = viz.second ].push_back(viz.first) ;
}
swap(freq[i][0] , freq[i].back()) ;
freq[i].pop_back() ;
}
}
int main()
{
scanf("%d %d", &N, &M ) ;
for(int i = 0 ; i < N ; i++ )
for(int j = 0 ; j < M ; j++ ) scanf(" %c", &grid[i][j] ) ;
inf = (N*M)+2 ;
for(int i = 1 ; i < (1<<M) ; i++ ) dp[0][i] = __builtin_popcount(i) ;
for(int i = 0 ; i < N ; i++, swap(toGet, toFill) )
{
for(int j = 0 ; j < (1<<M) ; j++ )
{
int t = 0 , m = j ;
dp[toFill][j] = inf ;
bool ok = false ;
for(int g = 0 ; g < M ; g++ )
{
if( !isOn(j,g) ) continue ;
if( grid[i][g] == '.' ) ok = true ;
if(i-1 >= 0 && grid[i-1][g] == '.')
{
t++ ;
m^=(1<<g) ;
}
}
if(ok ) continue ;
int qtd = 0 , isGoing = -1 ;
for(int g = 0 ;g < M ; g++ )
{
if( isOn(j,g) )
{
isGoing = -1 ;
continue ;
}
if( grid[i][g] == '.' ) isGoing = -1 ;
else
{
if(isGoing == -1 ) qtd++ ;
isGoing = 1 ;
}
}
dp[toFill][j] = min(inf,qtd+dp[toGet][m]+t) ;
}
for(int j = 0 ; j <= N*M+10 ; j++ ) freq[j].clear() ;
for(int j = 0 ; j < (1<<M) ; j++ ) freq[ dp[toFill][j] ].push_back(j) ;
dijkstra(i) ;
}
int ans = *min_element(dp[toGet] , dp[toGet]+(1<<M)) ;
printf("%d\n" , ans ) ;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%d %d", &N, &M ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~
Main.cpp:69:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | for(int j = 0 ; j < M ; j++ ) scanf(" %c", &grid[i][j] ) ;
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
146 ms |
600 KB |
Output is correct |
3 |
Correct |
78 ms |
580 KB |
Output is correct |
4 |
Correct |
102 ms |
576 KB |
Output is correct |
5 |
Correct |
197 ms |
580 KB |
Output is correct |
6 |
Correct |
209 ms |
668 KB |
Output is correct |
7 |
Correct |
203 ms |
680 KB |
Output is correct |
8 |
Correct |
145 ms |
696 KB |
Output is correct |
9 |
Correct |
205 ms |
692 KB |
Output is correct |
10 |
Correct |
244 ms |
1520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
460 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
5 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
460 KB |
Output is correct |
6 |
Correct |
3 ms |
460 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
460 KB |
Output is correct |
2 |
Correct |
146 ms |
600 KB |
Output is correct |
3 |
Correct |
78 ms |
580 KB |
Output is correct |
4 |
Correct |
102 ms |
576 KB |
Output is correct |
5 |
Correct |
197 ms |
580 KB |
Output is correct |
6 |
Correct |
209 ms |
668 KB |
Output is correct |
7 |
Correct |
203 ms |
680 KB |
Output is correct |
8 |
Correct |
145 ms |
696 KB |
Output is correct |
9 |
Correct |
205 ms |
692 KB |
Output is correct |
10 |
Correct |
244 ms |
1520 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
3 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
5 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
2 ms |
460 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
37 ms |
588 KB |
Output is correct |
20 |
Correct |
80 ms |
700 KB |
Output is correct |
21 |
Correct |
134 ms |
736 KB |
Output is correct |
22 |
Correct |
96 ms |
460 KB |
Output is correct |
23 |
Correct |
135 ms |
544 KB |
Output is correct |
24 |
Correct |
176 ms |
728 KB |
Output is correct |
25 |
Correct |
242 ms |
908 KB |
Output is correct |
26 |
Correct |
287 ms |
2160 KB |
Output is correct |
27 |
Correct |
334 ms |
2536 KB |
Output is correct |
28 |
Correct |
373 ms |
884 KB |
Output is correct |