PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/ACRush/ACRush_0_0/pA.cpp

https://bitbucket.org/vermapratyush/online-judges
C++ | 339 lines | 324 code | 5 blank | 10 comment | 102 complexity | c810dd74de4281cb723360d83cf899b7 MD5 | raw file
  1. #include <vector>
  2. #include <list>
  3. #include <map>
  4. #include <set>
  5. #include <deque>
  6. #include <queue>
  7. #include <stack>
  8. #include <bitset>
  9. #include <algorithm>
  10. #include <functional>
  11. #include <numeric>
  12. #include <utility>
  13. #include <sstream>
  14. #include <iostream>
  15. #include <iomanip>
  16. #include <cstdio>
  17. #include <cmath>
  18. #include <cstdlib>
  19. #include <cctype>
  20. #include <string>
  21. #include <cstring>
  22. #include <cstdio>
  23. #include <cmath>
  24. #include <cstdlib>
  25. #include <ctime>
  26. using namespace std;
  27. //BEGINTEMPLATE_BY_ACRUSH_TOPCODER
  28. #define SIZE(X) ((int)(X.size()))//NOTES:SIZE(
  29. #define LENGTH(X) ((int)(X.length()))//NOTES:LENGTH(
  30. #define MP(X,Y) make_pair(X,Y)//NOTES:MP(
  31. typedef long long int64;//NOTES:int64
  32. typedef unsigned long long uint64;//NOTES:uint64
  33. #define two(X) (1<<(X))//NOTES:two(
  34. #define twoL(X) (((int64)(1))<<(X))//NOTES:twoL(
  35. #define contain(S,X) (((S)&two(X))!=0)//NOTES:contain(
  36. #define containL(S,X) (((S)&twoL(X))!=0)//NOTES:containL(
  37. const double pi=acos(-1.0);//NOTES:pi
  38. const double eps=1e-11;//NOTES:eps
  39. template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}//NOTES:checkmin(
  40. template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}//NOTES:checkmax(
  41. template<class T> inline T sqr(T x){return x*x;}//NOTES:sqr
  42. typedef pair<int,int> ipair;//NOTES:ipair
  43. template<class T> inline T lowbit(T n){return (n^(n-1))&n;}//NOTES:lowbit(
  44. template<class T> inline int countbit(T n){return (n==0)?0:(1+countbit(n&(n-1)));}//NOTES:countbit(
  45. //Numberic Functions
  46. template<class T> inline T gcd(T a,T b)//NOTES:gcd(
  47. {if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
  48. template<class T> inline T lcm(T a,T b)//NOTES:lcm(
  49. {if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
  50. template<class T> inline T euclide(T a,T b,T &x,T &y)//NOTES:euclide(
  51. {if(a<0){T d=euclide(-a,b,x,y);x=-x;return d;}
  52. if(b<0){T d=euclide(a,-b,x,y);y=-y;return d;}
  53. if(b==0){x=1;y=0;return a;}else{T d=euclide(b,a%b,x,y);T t=x;x=y;y=t-(a/b)*y;return d;}}
  54. template<class T> inline vector<pair<T,int> > factorize(T n)//NOTES:factorize(
  55. {vector<pair<T,int> > R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}
  56. i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
  57. template<class T> inline bool isPrimeNumber(T n)//NOTES:isPrimeNumber(
  58. {if(n<=1)return false;for (T i=2;i*i<=n;i++) if (n%i==0) return false;return true;}
  59. template<class T> inline T eularFunction(T n)//NOTES:eularFunction(
  60. {vector<pair<T,int> > R=factorize(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
  61. //Matrix Operations
  62. const int MaxMatrixSize=40;//NOTES:MaxMatrixSize
  63. template<class T> inline void showMatrix(int n,T A[MaxMatrixSize][MaxMatrixSize])//NOTES:showMatrix(
  64. {for (int i=0;i<n;i++){for (int j=0;j<n;j++)cout<<A[i][j];cout<<endl;}}
  65. template<class T> inline T checkMod(T n,T m) {return (n%m+m)%m;}//NOTES:checkMod(
  66. template<class T> inline void identityMatrix(int n,T A[MaxMatrixSize][MaxMatrixSize])//NOTES:identityMatrix(
  67. {for (int i=0;i<n;i++) for (int j=0;j<n;j++) A[i][j]=(i==j)?1:0;}
  68. template<class T> inline void addMatrix(int n,T C[MaxMatrixSize][MaxMatrixSize],T A[MaxMatrixSize][MaxMatrixSize],T B[MaxMatrixSize][MaxMatrixSize])//NOTES:addMatrix(
  69. {for (int i=0;i<n;i++) for (int j=0;j<n;j++) C[i][j]=A[i][j]+B[i][j];}
  70. template<class T> inline void subMatrix(int n,T C[MaxMatrixSize][MaxMatrixSize],T A[MaxMatrixSize][MaxMatrixSize],T B[MaxMatrixSize][MaxMatrixSize])//NOTES:subMatrix(
  71. {for (int i=0;i<n;i++) for (int j=0;j<n;j++) C[i][j]=A[i][j]-B[i][j];}
  72. template<class T> inline void mulMatrix(int n,T C[MaxMatrixSize][MaxMatrixSize],T _A[MaxMatrixSize][MaxMatrixSize],T _B[MaxMatrixSize][MaxMatrixSize])//NOTES:mulMatrix(
  73. { T A[MaxMatrixSize][MaxMatrixSize],B[MaxMatrixSize][MaxMatrixSize];
  74. for (int i=0;i<n;i++) for (int j=0;j<n;j++) A[i][j]=_A[i][j],B[i][j]=_B[i][j],C[i][j]=0;
  75. for (int i=0;i<n;i++) for (int j=0;j<n;j++) for (int k=0;k<n;k++) C[i][j]+=A[i][k]*B[k][j];}
  76. template<class T> inline void addModMatrix(int n,T m,T C[MaxMatrixSize][MaxMatrixSize],T A[MaxMatrixSize][MaxMatrixSize],T B[MaxMatrixSize][MaxMatrixSize])//NOTES:addModMatrix(
  77. {for (int i=0;i<n;i++) for (int j=0;j<n;j++) C[i][j]=checkMod(A[i][j]+B[i][j],m);}
  78. template<class T> inline void subModMatrix(int n,T m,T C[MaxMatrixSize][MaxMatrixSize],T A[MaxMatrixSize][MaxMatrixSize],T B[MaxMatrixSize][MaxMatrixSize])//NOTES:subModMatrix(
  79. {for (int i=0;i<n;i++) for (int j=0;j<n;j++) C[i][j]=checkMod(A[i][j]-B[i][j],m);}
  80. template<class T> inline T multiplyMod(T a,T b,T m) {return (T)((((int64)(a)*(int64)(b)%(int64)(m))+(int64)(m))%(int64)(m));}//NOTES:multiplyMod(
  81. template<class T> inline void mulModMatrix(int n,T m,T C[MaxMatrixSize][MaxMatrixSize],T _A[MaxMatrixSize][MaxMatrixSize],T _B[MaxMatrixSize][MaxMatrixSize])//NOTES:mulModMatrix(
  82. { T A[MaxMatrixSize][MaxMatrixSize],B[MaxMatrixSize][MaxMatrixSize];
  83. for (int i=0;i<n;i++) for (int j=0;j<n;j++) A[i][j]=_A[i][j],B[i][j]=_B[i][j],C[i][j]=0;
  84. for (int i=0;i<n;i++) for (int j=0;j<n;j++) for (int k=0;k<n;k++) C[i][j]=(C[i][j]+multiplyMod(A[i][k],B[k][j],m))%m;}
  85. template<class T> inline T powerMod(T p,int e,T m)//NOTES:powerMod(
  86. {if(e==0)return 1%m;else if(e%2==0){T t=powerMod(p,e/2,m);return multiplyMod(t,t,m);}else return multiplyMod(powerMod(p,e-1,m),p,m);}
  87. //Point&Line
  88. double dist(double x1,double y1,double x2,double y2){return sqrt(sqr(x1-x2)+sqr(y1-y2));}//NOTES:dist(
  89. double distR(double x1,double y1,double x2,double y2){return sqr(x1-x2)+sqr(y1-y2);}//NOTES:distR(
  90. template<class T> T cross(T x0,T y0,T x1,T y1,T x2,T y2){return (x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);}//NOTES:cross(
  91. int crossOper(double x0,double y0,double x1,double y1,double x2,double y2)//NOTES:crossOper(
  92. {double t=(x1-x0)*(y2-y0)-(x2-x0)*(y1-y0);if (fabs(t)<=eps) return 0;return (t<0)?-1:1;}
  93. bool isIntersect(double x1,double y1,double x2,double y2,double x3,double y3,double x4,double y4)//NOTES:isIntersect(
  94. {return crossOper(x1,y1,x2,y2,x3,y3)*crossOper(x1,y1,x2,y2,x4,y4)<0 && crossOper(x3,y3,x4,y4,x1,y1)*crossOper(x3,y3,x4,y4,x2,y2)<0;}
  95. bool isMiddle(double s,double m,double t){return fabs(s-m)<=eps || fabs(t-m)<=eps || (s<m)!=(t<m);}//NOTES:isMiddle(
  96. //Translator
  97. bool isUpperCase(char c){return c>='A' && c<='Z';}//NOTES:isUpperCase(
  98. bool isLowerCase(char c){return c>='a' && c<='z';}//NOTES:isLowerCase(
  99. bool isLetter(char c){return c>='A' && c<='Z' || c>='a' && c<='z';}//NOTES:isLetter(
  100. bool isDigit(char c){return c>='0' && c<='9';}//NOTES:isDigit(
  101. char toLowerCase(char c){return (isUpperCase(c))?(c+32):c;}//NOTES:toLowerCase(
  102. char toUpperCase(char c){return (isLowerCase(c))?(c-32):c;}//NOTES:toUpperCase(
  103. template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();}//NOTES:toString(
  104. int toInt(string s){int r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt(
  105. int64 toInt64(string s){int64 r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toInt64(
  106. double toDouble(string s){double r=0;istringstream sin(s);sin>>r;return r;}//NOTES:toDouble(
  107. template<class T> void stoa(string s,int &n,T A[]){n=0;istringstream sin(s);for(T v;sin>>v;A[n++]=v);}//NOTES:stoa(
  108. template<class T> void atos(int n,T A[],string &s){ostringstream sout;for(int i=0;i<n;i++){if(i>0)sout<<' ';sout<<A[i];}s=sout.str();}//NOTES:atos(
  109. template<class T> void atov(int n,T A[],vector<T> &vi){vi.clear();for (int i=0;i<n;i++) vi.push_back(A[i]);}//NOTES:atov(
  110. template<class T> void vtoa(vector<T> vi,int &n,T A[]){n=vi.size();for (int i=0;i<n;i++)A[i]=vi[i];}//NOTES:vtoa(
  111. template<class T> void stov(string s,vector<T> &vi){vi.clear();istringstream sin(s);for(T v;sin>>v;vi.push_bakc(v));}//NOTES:stov(
  112. template<class T> void vtos(vector<T> vi,string &s){ostringstream sout;for (int i=0;i<vi.size();i++){if(i>0)sout<<' ';sout<<vi[i];}s=sout.str();}//NOTES:vtos(
  113. //Fraction
  114. template<class T> struct Fraction{T a,b;Fraction(T a=0,T b=1);string toString();};//NOTES:Fraction
  115. template<class T> Fraction<T>::Fraction(T a,T b){T d=gcd(a,b);a/=d;b/=d;if (b<0) a=-a,b=-b;this->a=a;this->b=b;}
  116. template<class T> string Fraction<T>::toString(){ostringstream sout;sout<<a<<"/"<<b;return sout.str();}
  117. template<class T> Fraction<T> operator+(Fraction<T> p,Fraction<T> q){return Fraction<T>(p.a*q.b+q.a*p.b,p.b*q.b);}
  118. template<class T> Fraction<T> operator-(Fraction<T> p,Fraction<T> q){return Fraction<T>(p.a*q.b-q.a*p.b,p.b*q.b);}
  119. template<class T> Fraction<T> operator*(Fraction<T> p,Fraction<T> q){return Fraction<T>(p.a*q.a,p.b*q.b);}
  120. template<class T> Fraction<T> operator/(Fraction<T> p,Fraction<T> q){return Fraction<T>(p.a*q.b,p.b*q.a);}
  121. //ENDTEMPLATE_BY_ACRUSH_TOPCODER
  122. struct Point
  123. {
  124. int x,y;
  125. };
  126. bool operator<(const Point &a,const Point &b)
  127. {
  128. return a.x<b.x || a.x==b.x && a.y<b.y;
  129. }
  130. Point B[10],P[10];
  131. int nB,nP,n,m;
  132. int x[6],y[6],father[6];
  133. char mapG[15][15];
  134. int A[15][15];
  135. map<string,int> M;
  136. vector<string> Q;
  137. int bx[6],by[6],mapH[15][15];
  138. string src,dest;
  139. bool flag;
  140. int current_step;
  141. int getfather(int v)
  142. {
  143. return (father[v]<0)?v:(father[v]=getfather(father[v]));
  144. }
  145. void init()
  146. {
  147. scanf("%d%d",&n,&m);
  148. for (int i=0; i<n; i++)
  149. scanf("%s",mapG[i]);
  150. }
  151. string getId(Point A[],int len)
  152. {
  153. string R="";
  154. for (int i=0;i<len;i++)
  155. {
  156. if (A[i].x<10) R=R+"0"+(char)(A[i].x+'0');
  157. if (A[i].x==10) R=R+"10";
  158. if (A[i].x==11) R=R+"11";
  159. if (A[i].y<10) R=R+"0"+(char)(A[i].y+'0');
  160. if (A[i].y==10) R=R+"10";
  161. if (A[i].y==11) R=R+"11";
  162. }
  163. return R;
  164. }
  165. bool checkIt(int n)
  166. {
  167. for (int i=0;i<n;i++) father[i]=-1;
  168. for (int i=0;i<n;i++)
  169. for (int j=i+1;j<n;j++)
  170. if (abs(bx[i]-bx[j])+abs(by[i]-by[j])==1)
  171. {
  172. int u=getfather(i),v=getfather(j);
  173. if (u!=v)
  174. father[u]=v;
  175. }
  176. for (int i=0; i<n; i++)
  177. if (getfather(i)!=getfather(0))
  178. return false;
  179. return true;
  180. }
  181. void push(int K)
  182. {
  183. if (!flag && !checkIt(K))
  184. return;
  185. int x[6],y[6];
  186. for (int i=0;i<K;i++) x[i]=bx[i],y[i]=by[i];
  187. for (int i=0;i<K;i++)
  188. for (int j=i+1;j<K;j++)
  189. if (x[j]<x[i] || (x[j]==x[i] && y[j]<y[i]))
  190. {
  191. swap(x[i],x[j]);
  192. swap(y[i],y[j]);
  193. }
  194. int step=current_step+1;
  195. string state="";
  196. for (int i=0;i<K;i++)
  197. {
  198. if (x[i]<10) state=state+"0"+(char)(x[i]+'0');
  199. if (x[i]==10) state=state+"10";
  200. if (x[i]==11) state=state+"11";
  201. if (y[i]<10) state=state+"0"+(char)(y[i]+'0');
  202. if (y[i]==10) state=state+"10";
  203. if (y[i]==11) state=state+"11";
  204. }
  205. if (M.find(state)!=M.end()) return;
  206. M.insert(make_pair(state, step));
  207. Q.push_back(state);
  208. }
  209. void BFS(string state)
  210. {
  211. memset(mapH,0,sizeof(mapH));
  212. int K=LENGTH(state)/4;
  213. for (int i=0;i<K;i++)
  214. {
  215. bx[i]=(state[i*4]-'0')*10+state[i*4+1]-'0';
  216. by[i]=(state[i*4+2]-'0')*10+state[i*4+3]-'0';
  217. mapH[bx[i]][by[i]]=1;
  218. }
  219. for (int i=0;i<K;i++)
  220. {
  221. bool isGoodX=false;
  222. bool isGoodY=false;
  223. if (bx[i]>0 && bx[i]<n-1 && A[bx[i]-1][by[i]]==0
  224. && A[bx[i]+1][by[i]]==0
  225. && mapH[bx[i]-1][by[i]]==0
  226. && mapH[bx[i]+1][by[i]]==0)
  227. isGoodX=true;
  228. if (by[i]>0 && by[i]<m-1 && A[bx[i]][by[i]-1]==0
  229. && A[bx[i]][by[i]+1]==0
  230. && mapH[bx[i]][by[i]-1]==0
  231. && mapH[bx[i]][by[i]+1]==0)
  232. isGoodY=true;
  233. if (isGoodX)
  234. {
  235. bx[i]--;
  236. push(K);
  237. bx[i]++;
  238. bx[i]++;
  239. push(K);
  240. bx[i]--;
  241. }
  242. if (isGoodY)
  243. {
  244. by[i]--;
  245. push(K);
  246. by[i]++;
  247. by[i]++;
  248. push(K);
  249. by[i]--;
  250. }
  251. }
  252. }
  253. bool checkIt(string state)
  254. {
  255. int n=LENGTH(state)/4;
  256. for (int i=0;i<n;i++)
  257. {
  258. x[i]=(state[i*4]-'0')*10+state[i*4+1]-'0';
  259. y[i]=(state[i*4+2]-'0')*10+state[i*4+3]-'0';
  260. father[i]=-1;
  261. }
  262. for (int i=0; i<n; i++)
  263. for (int j=i+1; j<n; j++)
  264. if (abs(x[i]-x[j])+abs(y[i]-y[j])==1)
  265. {
  266. int x=getfather(i);
  267. int y=getfather(j);
  268. if (x!=y)
  269. father[x]=y;
  270. }
  271. for (int i=0; i<n; i++)
  272. if (getfather(i)!=getfather(0))
  273. return false;
  274. return true;
  275. }
  276. int solve()
  277. {
  278. for (int i=0;i<n;i++)
  279. for (int j=0;j<m;j++)
  280. {
  281. if (mapG[i][j]=='.') A[i][j]=0;
  282. if (mapG[i][j]=='#') A[i][j]=-1;
  283. if (mapG[i][j]=='x') A[i][j]=2;
  284. if (mapG[i][j]=='o') A[i][j]=1;
  285. if (mapG[i][j]=='w') A[i][j]=3;
  286. }
  287. M.clear();
  288. Q.clear();
  289. nB=nP=0;
  290. for (int i=0;i<n;i++)
  291. for (int j=0;j<m;j++)
  292. if (A[i][j]>0)
  293. {
  294. if ((A[i][j]&1)==1) B[nB].x=i,B[nB++].y=j;
  295. if ((A[i][j]&2)==2) P[nP].x=i,P[nP++].y=j;
  296. if (A[i][j]>0) A[i][j]=0;
  297. }
  298. src=dest="";
  299. sort(B,B+nB);
  300. sort(P,P+nP);
  301. if (nB!=nP) return -1;
  302. src=getId(B,nB);
  303. dest=getId(P,nP);
  304. Q.push_back(src);
  305. M.insert(make_pair(src, 0));
  306. for (int cl=0;cl<SIZE(Q);cl++)
  307. {
  308. src=Q[cl];
  309. if (src==dest) return M[src];
  310. current_step=M[src];
  311. flag=false;
  312. if (checkIt(src)) flag=true;
  313. BFS(src);
  314. }
  315. return -1;
  316. }
  317. int main()
  318. {
  319. // freopen("A.in","r",stdin);
  320. freopen("A-small-attempt0.in","r",stdin);freopen("A-small-attempt0.out","w",stdout);
  321. // freopen("A-small-attempt1.in","r",stdin);freopen("A-small-attempt1.out","w",stdout);
  322. // freopen("A-large.in","r",stdin);freopen("A-large.out","w",stdout);
  323. int testcase;
  324. scanf("%d",&testcase);
  325. for (int caseId=1;caseId<=testcase;caseId++)
  326. {
  327. printf("Case #%d: ",caseId);
  328. init();
  329. int ret=solve();
  330. printf("%d\n",ret);
  331. fflush(stdout);
  332. }
  333. return 0;
  334. }