1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
   | #include <iostream> #include <cstdio> #include <cstring> #include <algorithm>
  using namespace std; char s[220][220]; int ma[222][222]; int now[222][222]; #define maxn 101 struct Node{     int x,y; }p[maxn]; int pnum; int tmpans;
  void dfs(int id,int step,bool flag){     if (id == pnum){         for (int i = 0; i < pnum; i ++){             if (now[p[i].x][p[i].y] == 0){                 return;             }         }         tmpans = min(tmpans,step);         return ;     }     int x = p[id].x, y = p[id].y;     int t1 = now[x][y],t2 = now[x][y + 1], t3 = now[x - 1][y], t4 = now[x][y - 1],t5 = now[x + 1][y];     if (ma[x][y] == 1 && ma[x - 1][y] != 0 && ma[x][y + 1] != 0){         if (t1 == 1 && t2 == 1 && t3 == 1){         }else {             now[x][y] = 1;             now[x][y + 1] = 1;             now[x - 1][y] = 1;             dfs(id + 1,step + 1, flag);             now[x][y] = t1;             now[x][y + 1] = t2;             now[x - 1][y] = t3;
          }     }     if (!flag && ma[x][y] == 1 && ma[x - 1][y] != 0 && ma[x][y - 1] != 0){         if (t1 == 1 && t3 == 1 && t4 == 1){         }else {             now[x][y] = 1;             now[x][y - 1] = 1;             now[x - 1][y] = 1;             dfs(id + 1,step + 1, 1);             now[x][y] = t1;             now[x][y - 1] = t4;             now[x - 1][y] = t3;
          }     }     if (!flag && ma[x][y] == 1 && ma[x + 1][y] != 0 && ma[x][y + 1] != 0){         if (t1 == 1 && t5 == 1 && t2 == 1){         }else {             now[x][y] = 1;             now[x][y + 1] = 1;             now[x + 1][y] = 1;             dfs(id + 1,step + 1, 1);             now[x][y] = t1;             now[x][y + 1] = t2;             now[x + 1][y] = t5;
          }     }     if (!flag && ma[x][y] == 1 && ma[x + 1][y] != 0 && ma[x][y - 1] != 0){         if (t1 == 1 && t4 == 1 && t5 == 1){         }else {             now[x][y] = 1;             now[x][y - 1] = 1;             now[x + 1][y] = 1;             dfs(id + 1,step + 1, flag);             now[x][y] = t1;             now[x][y - 1] = t4;             now[x + 1][y] = t5;
          }     }     dfs(id + 1,step, flag); }
  int main(){     int n,m;     while (~scanf("%d%d", &n, &m) && (n + m)){         memset(ma, -1, sizeof(ma));         memset(now, -1, sizeof(now));         for (int i = 0; i < n; i ++){             scanf("%s",s[i]);             for (int  j = 0; j < m; j ++){                 if (s[i][j] == '#'){                     ma[i + 1][j + 1] = 0;                     now[i + 1][j + 1] = -1;                 }else {                     ma[i + 1][j + 1] = 1;                     now[i + 1][j + 1] = 0;                 }             }            }
          pnum = 0;         tmpans = 999999;         for (int i = 1; i <= n; i ++){             for (int j = 1; j <= m; j ++){                 if (ma[i][j] == 1){                     p[pnum].x = i;                     p[pnum ++].y = j;                 }                        }         }         dfs(0,0,0);         printf("%dn",tmpans == 999999? -1 : tmpans);                }     return 0; }
   |