AOJ2299 Tiles are Colorful
問題ページ
Tiles are Colorful | Aizu Online Judge
考えたこと
- Aを消すために消さないといけない色から有向辺を貼ってトポソを考える
- 2パターンあるのをトポソでわけて判断するの無理そう
- 各色のタイルの位置を求めておいて消せるタイルを消すを繰り返せばよさそう
- O(max(H,W))くらいで解けそう
実装でバグらせつつ書くと通った
サンプルがやさしい
#define __USE_MINGW_ANSI_STDIO 0 #include <bits/stdc++.h> using namespace std; using ll = long long; #define int ll using VI = vector<int>; using VVI = vector<VI>; using PII = pair<int, int>; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define PB push_back const ll LLINF = (1LL<<60); const int INF = (1LL<<30); const int MOD = 1000000007; template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); } template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); } template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; } template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); } template<class S,class T> ostream &operator <<(ostream& out,const pair<S,T>& a){ out<<'('<<a.first<<','<<a.second<<')'; return out; } template<class T> ostream &operator <<(ostream& out,const vector<T>& a){ out<<'['; REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';} out<<']'; return out; } int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; string s[505]; signed main(void) { int h, w; cin >> h >> w; REP(i, h) cin >> s[i]; vector<PII> p[30]; REP(i, h) REP(j, w) { if(s[i][j] != '.') p[s[i][j]-'A'].PB({i, j}); } int ret = 0; bool update = true; while(update) { update = false; REP(i, 26) { if(p[i].size() == 0) continue; int l = min(p[i][0].second, p[i][1].second), r = max(p[i][0].second, p[i][1].second); int u = min(p[i][0].first, p[i][1].first), d = max(p[i][0].first, p[i][1].first); // cout << char(i+'A') << " " << l << " " << r << " " << u << " " << d << endl; if((l+1==r && u==d) || (l==r && u+1==d)) continue; if((l == p[i][0].second && u == p[i][0].first) || (l == p[i][1].second && u == p[i][1].first)) { // cout << "a" << endl; bool flag = true; FOR(y, u+1, d) if(s[y][l] != '.') flag = false; FOR(x, l, r) if(s[d][x] != '.') flag = false; if(flag) { s[p[i][0].first][p[i][0].second] = '.'; s[p[i][1].first][p[i][1].second] = '.'; p[i].clear(); ret += 2; update = true; continue; } flag = true; FOR(y, u, d) if(s[y][r] != '.') flag = false; FOR(x, l+1, r) if(s[u][x] != '.') flag = false; if(flag) { s[p[i][0].first][p[i][0].second] = '.'; s[p[i][1].first][p[i][1].second] = '.'; p[i].clear(); ret += 2; update = true; } } else { // cout << "b" << endl; bool flag = true; FOR(y, u+1, d) if(s[y][l] != '.') flag = false; FOR(x, l, r) if(s[u][x] != '.') flag = false; if(flag) { s[p[i][0].first][p[i][0].second] = '.'; s[p[i][1].first][p[i][1].second] = '.'; p[i].clear(); ret += 2; update = true; continue; } flag = true; FOR(y, u+1, d+1) if(s[y][r] != '.') flag = false; FOR(x, l+1, r) if(s[d][x] != '.') flag = false; if(flag) { s[p[i][0].first][p[i][0].second] = '.'; s[p[i][1].first][p[i][1].second] = '.'; p[i].clear(); ret += 2; update = true; } } } // REP(y, h) cout << s[y] << endl; // cout << endl; } cout << ret << endl; return 0; }