#include #include #include #include #include #include #include #define f cin #define g cout #define NM 20 using namespace std; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; const int N = 14; char c[NM][NM]; int K, cnt; int marked[NM][NM]; set G[NM*NM]; int color[NM*NM], asize[NM*NM]; void fill (int i, int j, char col) { marked[i][j] = K; cnt++; for (int d=0; d<4; d++) if (c[i+dx[d]][j+dy[d]]==col && marked[i+dx[d]][j+dy[d]]==0) fill(i+dx[d], j+dy[d], col); } int ans[28]; bool in[NM*NM]; vector::iterator a; set::iterator b, d; vector areas; bool counted[7][NM*NM]; void backt (int step) { if (areas.size() >= K) { for (int i=1; i<=step; i++) g << ans[i]-1 << ' '; g << '\n'; exit(0); return; } if (step>=25) return; int countCol[7]; for (int i=0; i<7; i++) countCol[i] = 0; set all; for (a=areas.begin(); a!=areas.end(); a++) for (b=G[*a].begin(); b!=G[*a].end(); b++) if (!in[*b]) all.insert(*b); for (int i=1; i<=K; i++) for (int c=1; c<=6; c++) counted[c][i] = 0; for (a=areas.begin(); a!=areas.end(); a++) for (int c=1; c<=6; c++) counted[c][*a] = 1; for (b=all.begin(); b!=all.end(); b++) { for (d=G[*b].begin(); d!=G[*b].end(); d++) if (color[*d] != color[*b] && counted[color[*b]][*d]==0) { countCol[color[*b]]++; counted[color[*b]][*d]=1; } } vector > colors; for (int i=1; i<=6; i++) colors.push_back(make_pair(countCol[i], i)); sort(colors.begin(), colors.end(), greater >()); for (int i=0; i0) { areas.pop_back(); cnt--; } for (b=all.begin(); b!=all.end(); b++) if (color[*b] == colors[i].second) { in[*b]=0; } ans[step + 1] = 0; } } int main() { for (int i=1; i<=N; i++) f >> (c[i]+1); for (int i=1; i<=N; i++) for (int j=1; j<=N; j++) { if (!marked[i][j]) { ++K; cnt = 0; color[K] = c[i][j]-'0'+1; fill(i, j, c[i][j]); asize[K] = cnt; } for (int d=0; d<4; d++) { int x = marked[i][j]; int y = marked[i+dx[d]][j+dy[d]]; if (x!=y && y!=0) { G[x].insert(y); G[y].insert(x); } } } areas.push_back(1); in[1]=true; backt(0); }