Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 178 Accepted Submission(s): 57
php
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int dir_x[4]={-1,0,1,0},dir_y[4]={0,1,0,-1}; 8 9 int n,m,cnt=0,ans; 10 char G[250][250]; 11 bool den[250][250]; 12 13 bool inside(int x,int y) 14 { 15 return (x>=0&&x<n)&&(y>=0&&y<m); 16 } 17 18 void SHOWden() 19 { 20 for(int i=0;i<n;i++,putchar(10)) 21 for(int j=0;j<m;j++) 22 cout<<den[i][j]<<" "; 23 cout<<" ... \n"; 24 } 25 26 void SHOWST(int x) 27 { 28 for(int i=0;i<cnt;i++) 29 { 30 if(x&(1<<i)) cout<<"1 "; 31 else cout<<"0 "; 32 } 33 cout<<endl; 34 } 35 36 struct LP 37 { 38 int x,y; 39 }lp[20]; 40 41 void dfs(int state,int lan,int n) 42 { 43 //SHOWST(state); 44 //cout<<lan<<" ...... "<<n<<endl; 45 if(n>=ans) return ; 46 if(lan==cnt) 47 { 48 ans=n; return ; 49 } 50 for(int i=0;i<cnt;i++) 51 { 52 if(state&(1<<i)) continue; 53 int lan0=0,lan1=0,lan2=0; 54 if(den[lp[i].x][lp[i].y]==0) 55 { 56 lan0++; den[lp[i].x][lp[i].y]=1; 57 } 58 int x1=lp[i].x+dir_x[0],x2=lp[i].x+dir_x[1]; 59 int y1=lp[i].y+dir_y[0],y2=lp[i].y+dir_y[1]; 60 if(!inside(x1,y1)||(inside(x1,y1)&&G[x1][y1]=='.')) 61 { 62 if(!inside(x2,y2)||(inside(x2,y2)&&G[x2][y2]=='.')) 63 { 64 if(inside(x1,y1)&&den[x1][y1]==0) 65 { 66 lan1++; den[x1][y1]=1; 67 } 68 if(inside(x2,y2)&&den[x2][y2]==0) 69 { 70 lan2++; den[x2][y2]=1; 71 } 72 dfs(state|(1<<i),lan+lan2+lan0+lan1,n+1); 73 } 74 } 75 if(lan0) den[lp[i].x][lp[i].y]=0; 76 if(lan1) den[x1][y1]=0; 77 if(lan2) den[x2][y2]=0; 78 } 79 } 80 81 int main() 82 { 83 while(scanf("%d%d",&n,&m)!=EOF) 84 { 85 if(n==0&&m==0) break; 86 getchar(); 87 memset(G,' ',sizeof(G)); 88 cnt=0; 89 for(int i=0;i<n;i++) 90 { 91 scanf("%s",G[i]); 92 } 93 /* 94 for(int i=0;i<n;i++) 95 { 96 for(int j=0;j<m;j++) 97 cout<<" "<<G[i][j]; 98 cout<<endl; 99 } 100 cout<<endl; 101 */ 102 memset(lp,0,sizeof(lp)); 103 memset(den,0,sizeof(den)); 104 for(int i=0;i<n;i++) 105 { 106 for(int j=0;j<m;j++) 107 { 108 if(G[i][j]=='.') 109 { 110 lp[cnt].x=i;lp[cnt].y=j; 111 cnt++; 112 } 113 } 114 } 115 if(cnt==0) 116 { 117 printf("0\n"); continue; 118 } 119 ans=0x3f3f3f3f; 120 for(int i=0;i<cnt;i++) 121 { 122 den[lp[i].x][lp[i].y]=1; 123 //cout<<lp[i].x<<" ..... "<<lp[i].y<<endl; 124 for(int j=0;j<4;j++) 125 { 126 int x1=lp[i].x+dir_x[j%4],x2=lp[i].x+dir_x[(j+1)%4]; 127 int y1=lp[i].y+dir_y[j%4],y2=lp[i].y+dir_y[(j+1)%4]; 128 if((inside(x1,y1)&&G[x1][y1]=='.')||!inside(x1,y1)) 129 { 130 if((inside(x2,y2)&&G[x2][y2]=='.')||!inside(x2,y2)) 131 { 132 int lan=1; 133 if(inside(x1,y1)) { lan++; den[x1][y1]=1; } 134 if(inside(x2,y2)) { lan++; den[x2][y2]=1; } 135 //cout<<inside(x1,y1)<<" "<<x1<<","<<y1<<endl; 136 //cout<<inside(x2,y2)<<" "<<x2<<","<<y2<<endl; 137 //cout<<" .. \n"; 138 //SHOWden(); 139 dfs((1<<i),lan,1); 140 //SHOWden(); 141 if(inside(x1,y1)) den[x1][y1]=0; 142 if(inside(x2,y2)) den[x2][y2]=0; 143 } 144 } 145 } 146 den[lp[i].x][lp[i].y]=0; 147 } 148 if(ans==0x3f3f3f3f) puts("-1"); 149 else printf("%d\n",ans); 150 } 151 return 0; 152 }