#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;
static const int NV = 18*18*2+18+2;
typedef int flow;
typedef LL cost;
typedef int vert;
typedef vert edge;
typedef vector<edge> edges;
typedef vector<edges> graph;
typedef flow flow_graph[NV][NV];
typedef cost cost_graph[NV][NV];
static const flow FLOW_INF = 0x7FFFFFFF;
static const cost COST_INF = 0x7FFFFFFFFFFFFFFFLL;
// edges(bidi), capacity, cost(s.t. C[x][y]=-C[y][x]) per 1 flow, src, dst
pair<cost,flow> mincostFlow(graph& G, flow_graph F, cost_graph C, vert S, vert D)
{
int N = G.size();
cost total_cost = 0;
flow total_flow = 0;
vector<cost> h(N, 0); // potential
for(cost RF=FLOW_INF; RF>0; ) // residual flow
{
// Dijkstra -- find the min-cost path
vector<cost> d(N, COST_INF); d[S] = 0;
vector<vert> prev(N, -1);
typedef pair<cost, pair<vert,vert> > cedge;
priority_queue< cedge, vector<cedge>, greater<cedge> > Q;
Q.push( cedge(0, make_pair(S,S)) );
while( !Q.empty() ) {
cedge e = Q.top(); Q.pop();
if( prev[e.second.second] >= 0 )
continue;
prev[e.second.second] = e.second.first;
vert u = e.second.second;
for(int i=0; i<G[u].size(); ++i) {
vert v = G[u][i];
cost r_cost = C[u][v] + h[u] - h[v];
if( F[u][v] > 0 && d[v] > d[u]+r_cost )
Q.push( cedge(d[v]=d[u]+r_cost, make_pair(u,v)) );
}
}
if( prev[D] < 0 )
break; // Finished
// As much as possible
flow f = RF;
for(vert u=D; u!=S; u=prev[u])
f = min(f, F[prev[u]][u]);
RF -= f;
total_flow += f;
for(vert u=D; u!=S; u=prev[u])
{
total_cost += f * C[prev[u]][u];
F[prev[u]][u] -= f;
F[u][prev[u]] += f;
}
// Update the potential
for(vert u=0; u<N; ++u)
h[u] += d[u];
}
return make_pair(total_cost, total_flow);
}
flow_graph F;
cost_graph C;
LL f(LL n) {
return n*n*n*n*n;
}
class TheSquareDivOne { public:
vector <string> solve(vector <string> board)
{
int n = board.size();
vector<int> R(n);
for(int y=0; y<n; ++y)
for(int x=0; x<n; ++x)
if( board[y][x]=='C' )
R[y] ++;
graph G(2+n+n*n*2);
memset(F, 0, sizeof(F));
memset(C, 0, sizeof(C));
vert S = 0;
vert D = 1;
for(int y=0; y<n; ++y)
for(int x=0; x<n; ++x) {
vert v = (2+n) + y*n + x;
G[S].push_back(v);
G[v].push_back(S);
if( board[y][x]=='C' )
F[S][v] = 1;
}
for(int y=0; y<n; ++y)
for(int x=0; x<n; ++x) {
vert v = (2+n) + (n*n) + y*n + x;
vert u = 2+x;
G[v].push_back(u);
G[u].push_back(v);
F[v][u] = 1;
// C[u][v] = -(C[v][u] = (1LL<<(n-y-1))<<30);
}
for(int x=0; x<n; ++x) {
vert u = 2+x;
G[u].push_back(D);
G[D].push_back(u);
F[u][D] = R[x];
// C[u][D] = -(C[D][u] = (1LL<<2*(n-x-1)));
}
for(int y1=0; y1<n; ++y1)
for(int x1=0; x1<n; ++x1)
for(int y2=0; y2<n; ++y2)
for(int x2=0; x2<n; ++x2) {
vert v = (2+n) + y1*n + x1;
vert u = (2+n) + (n*n) + y2*n + x2;
G[u].push_back(v);
G[v].push_back(u);
F[v][u] = 1;
C[u][v] = -(C[v][u] = abs(y1-y2)+abs(x1-x2));
// C[u][v] = -(C[v][u] = LL(abs(y1-y2)+abs(x1-x2))<<50);
}
pair<cost,flow> mf = mincostFlow(G,F,C,S,D);
// cerr << "Cost: " << (mf.first>>50) << "+" << ((mf.first&(1LL<<50)-1)) << ", Flow: " << mf.second << endl;
for(int y=0; y<n; ++y)
for(int x=0; x<n; ++x)
board[y][x] = (F[(2+n) + (n*n) + y*n + x][2+x]==0 ? 'C' : '.');
// not lexicographically first ...orz
return board;
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const vector <string>& Expected, const vector <string>& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, TheSquareDivOne().solve(board));}
int main(){
CASE(0)
string board_[] = {"...",
"...",
"C.."};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
string __[] = {"...", "...", "..C" };
vector <string> _(__, __+sizeof(__)/sizeof(*__));
END
CASE(1)
string board_[] = {"CCC",
".C.",
"CCC"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
string __[] = {"C.C", "C.C", "CCC" };
vector <string> _(__, __+sizeof(__)/sizeof(*__));
END
CASE(2)
string board_[] = {"C..",
".C.",
"..C"};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
string __[] = {"C..", ".C.", "..C" };
vector <string> _(__, __+sizeof(__)/sizeof(*__));
END
CASE(3)
string board_[] = {"C....","CCCCC","...CC",".CC..",".C.C."};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
string __[] = {".C...", "CCCCC", ".C..C", ".CC..", ".C.C." };
vector <string> _(__, __+sizeof(__)/sizeof(*__));
END
CASE(4)
string board_[] = {
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
};
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
string __[] = {
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCC",
};
vector <string> _(__, __+sizeof(__)/sizeof(*__));
END
/*
CASE(5)
string board_[] = ;
vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_));
string __[] = ;
vector <string> _(__, __+sizeof(__)/sizeof(*__));
END
*/
}
// END CUT HERE