This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("fast-math")
//#pragma GCC optimize("no-stack-protector")
#define F first
#define S second
#define sz(x) int(x.size())
#define pb push_back
#define N 75005
#define M ll(1e9 + 7)
using namespace std;
typedef long double ld;
typedef long long ll;
typedef short int si;
int main()
{
ios_base::sync_with_stdio(0); istream::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
string s[n];
for (int i = 0; i < n; i++) cin >> s[i];
vector <pair <int, int> > g; g.clear();
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (s[i][j] == '1' && ((i + 1 != n && s[i + 1][j] == '0') || (j + 1 != m && s[i][j + 1] == '0'))) g.pb({i, j});
int ans = 1e9;
vector <pair <pair <int, int>, int> > gr; gr.clear();
for (int umsk = 0; umsk < (1 << sz(g)); umsk++)
{
if (__builtin_popcount(umsk) > ans) continue;
vector <int> pr; pr.clear();
for (int i = 0; i < sz(g); i++) if ((1 << i) & umsk) pr.pb(i);
for (int msk = 0; msk < (1 << sz(pr)); msk++)
{
bool gd = 1;
bool mk[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) mk[i][j] = s[i][j] - '0';
for (int i = 0; i < sz(pr); i++)
if ((1 << i) & msk)
{
int x = g[pr[i]].F, y = g[pr[i]].S;
if (y + 1 == m || s[x][y + 1] == '1') {gd = 0; break;}
int j = y + 1;
while (j < m && s[x][j] == '0') {mk[x][j] = 1; j++;}
}
else
{
int x = g[pr[i]].F, y = g[pr[i]].S;
if (x + 1 == n || s[x + 1][y] == '1') {gd = 0; break;}
int j = x + 1;
while (j < n && s[j][y] == '0') {mk[j][y] = 1; j++;}
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) if (!mk[i][j]) gd = 0;
if (gd)
{
ans = sz(pr);
gr.clear();
for (int i = 0; i < sz(pr); i++) gr.pb({{g[pr[i]].F, g[pr[i]].S}, (1 << i) & msk});
break;
}
}
}
cout << sz(gr) << endl;
for (auto it : gr) cout << it.F.F + 1 << " " << it.F.S + 1 << " " << (it.S == 1 ? "DESNO" : "DOLJE") << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |