记录编号 88681 评测结果 AAAAAAAAAA
题目名称 [UVa 1501] 修建长城 最终得分 100
用户昵称 Gravatarcstdio 是否通过 通过
代码语言 C++ 运行时间 0.125 s
提交时间 2014-02-23 15:25:23 内存使用 0.41 MiB
显示代码纯文本
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<iomanip>
  6. #include<vector>
  7. using namespace std;
  8. typedef long long ll;
  9. const int SIZEN=12,HASHVAL=13131,INF=0x7fffffff;
  10. char board[SIZEN][SIZEN]={0};
  11. int N,M;
  12. int lastx,lasty;
  13. int ans;
  14. //状态是:轮廓线上M+1个格子(含当前格子的左上格子)的连通性,用最小表示法
  15. //每个格子的最小表示法用四个bit,这样总共是36个bit
  16. //下标大的在高位,下标小的在低位
  17. //本题下标从1开始
  18. class STATE{
  19. public:
  20. int comp[SIZEN];
  21. int ncomp[SIZEN];
  22. void clear(void){memset(comp,0,sizeof(comp));}
  23. void print(void){for(int i=1;i<=M+1;i++){cout<<comp[i]<<" ";}}
  24. int num_comp(void){//连通块的总数
  25. bool flag[SIZEN+3]={0};
  26. int num=0;
  27. for(int i=1;i<=M+1;i++){
  28. if(comp[i]&&!flag[comp[i]]) flag[comp[i]]=true,num++;
  29. }
  30. return num;
  31. }
  32. void calc_num(int y){//计算ncomp,保证comp中是正确的,其中comp[y]不计
  33. memset(ncomp,0,sizeof(ncomp));
  34. for(int i=1;i<=M+1;i++) if(i!=y) ncomp[comp[i]]++;
  35. }
  36. void nextline(void){//从行末转换到行首
  37. for(int i=M+1;i>1;i--) comp[i]=comp[i-1];
  38. comp[1]=0;
  39. }
  40. ll mdl(void){//调制成一个整数
  41. ll ans=0;
  42. for(int i=M+1;i>=1;i--) ans=(ans<<4)+comp[i];
  43. return ans;
  44. }
  45. void demdl(ll s){//把整数解调
  46. for(int i=1;i<=M+1;i++) comp[i]=s&15,s>>=4;
  47. }
  48. void merge(int a,int b){//把编号为b的连通块并入编号为a的连通块
  49. if(a==b) return;
  50. if(b<a) swap(a,b);//把编号较大的并入编号较小的
  51. for(int i=1;i<=M+1;i++) if(comp[i]==b) comp[i]=a;
  52. }
  53. void remark(void){//将comp改成最小表示法,即重新给分量标号
  54. int atp[SIZEN+3]={0};
  55. memset(atp,-1,sizeof(atp));
  56. for(int i=1;i<=M+1;i++){
  57. if(comp[i]==0) continue;
  58. if(atp[comp[i]]==-1) atp[comp[i]]=i;
  59. comp[i]=atp[comp[i]];
  60. }
  61. }
  62. bool placeblock(int x,int y,bool opt,int &dta){//决定第x行y列的格子,opt=1是放,opt=0是不放
  63. //不能放返回false,否则返回true并对状态进行相应改动,增加的长度是dta
  64. //左:y-1,左上:y,上:y+1,右上:y+2
  65. int l=y>1?comp[y-1]:0;//左
  66. int lu=comp[y];//左上
  67. int u=comp[y+1];//上
  68. int ru=l<M?comp[y+2]:0;//右上
  69. if(!opt){//选择了不放
  70. if(board[x][y]=='o') return false;
  71. if(l&&u&&!lu) return false;
  72. if(u&&ncomp[u]==1&&!(x>=lastx&&y>=lasty&&num_comp()==1)) return false;
  73. //将上方连通块独立,但若所有城市均被覆盖而且已经是符合题意的简单多边形那么可以将其独立掉
  74. dta=0;
  75. comp[y]=0;
  76. remark();
  77. }
  78. else{//选择了放
  79. if(board[x][y]=='x') return false;
  80. if(l&&u&&l==u&&!lu) return false;//中间出现了空洞
  81. if(lu&&!l&&!u) return false;//多边形的边界自交
  82. dta=4;if(u) dta-=2;if(l) dta-=2;//增加的边数
  83. comp[y]=M+2;
  84. if(u) merge(u,comp[y]);
  85. if(l) merge(l,comp[y]);
  86. remark();
  87. }
  88. return true;
  89. }
  90. };
  91. class HASHMAP{
  92. public:
  93. int hash[HASHVAL];
  94. vector<pair<ll,int> > st;
  95. void clear(void){
  96. memset(hash,-1,sizeof(hash));
  97. st.clear();
  98. }
  99. void insert(ll s,int f){
  100. if(st.size()>=HASHVAL){cout<<"hash overflow!"<<endl;}
  101. int hashpos=s%HASHVAL;
  102. while(hash[hashpos]!=-1){
  103. if(st[hash[hashpos]].first==s){
  104. st[hash[hashpos]].second=min(st[hash[hashpos]].second,f);
  105. return;
  106. }
  107. hashpos++;
  108. if(hashpos==HASHVAL) hashpos=0;
  109. }
  110. hash[hashpos]=st.size();
  111. st.push_back(make_pair(s,f));
  112. }
  113. };
  114. HASHMAP F[2];
  115. void update(int k,int x,int y){//用F[k]去更新F[k^1],其中新加的一格是(x,y)
  116. F[k^1].clear();
  117. STATE S,T;
  118. int nowf,incf;
  119. for(int i=0;i<F[k].st.size();i++){
  120. S.demdl(F[k].st[i].first);
  121. if(y==1) S.nextline();
  122. S.calc_num(y);
  123. nowf=F[k].st[i].second;
  124. for(int j=0;j<=1;j++){
  125. T=S;
  126. if(T.placeblock(x,y,j,incf)){
  127. F[k^1].insert(T.mdl(),nowf+incf);
  128. }
  129. }
  130. }
  131. }
  132. bool legalend(ll s){
  133. STATE S;
  134. S.demdl(s);
  135. return S.num_comp()<=1;
  136. }
  137. int kase=0;
  138. void work(void){
  139. int k=0;
  140. F[k].clear();
  141. F[k].insert(0,0);//没有连通块
  142. for(int i=1;i<=N;i++){
  143. for(int j=1;j<=M;j++){
  144. update(k,i,j);
  145. k^=1;
  146. }
  147. }
  148. ans=INF;
  149. for(int i=0;i<F[k].st.size();i++){
  150. if(legalend(F[k].st[i].first)) ans=min(ans,F[k].st[i].second);
  151. }
  152. if(ans==INF) ans=-1;
  153. printf("Case #%d: %d\n",++kase,ans);
  154. }
  155. void read(void){
  156. scanf("%d%d",&N,&M);
  157. string str;
  158. for(int i=1;i<=N;i++){
  159. cin>>str;
  160. for(int j=1;j<=M;j++){
  161. board[i][j]=str[j-1];
  162. if(board[i][j]=='o') lastx=i,lasty=j;
  163. }
  164. }
  165. }
  166. int main(){
  167. freopen("constructthegreatwall.in","r",stdin);
  168. freopen("constructthegreatwall.out","w",stdout);
  169. int T;
  170. scanf("%d",&T);
  171. while(T--){
  172. read();
  173. work();
  174. }
  175. return 0;
  176. }