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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
| #include <bits/stdc++.h> using namespace std; /* struct Node { int x, y; Node(int a=0, int b=0) : x(a), y(b) {} }; 第三题打个表输出 第n个数对应的斐波那契数列的下标 2 3 5 13 89 分别对应3 4 5 7 11 除了4 后面都是连续的素数 然后矩阵快速幂就可以了
https://www.flyai.com/?s=pmgO8ng7J http://latex.91maths.com/ */ typedef pair<int, int> Node; int maxn = 1e8; bool visited[105][105]; map<Node, Node> befor; int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; int n, m; char mat[105][105]; int dis[105][105]; int cell[105][105]; int ans; queue<Node> Q;
bool bfs() { while (!Q.empty()) { Node cur = Q.front(); Q.pop(); for (int i = 0; i < 4; i++) { int next_x = cur.first + dir[i][0], next_y = cur.second + dir[i][1]; if (next_x >= 0 && next_y >= 0 && next_y < m && next_x < n && !visited[next_x][next_y] && mat[next_x][next_y] != '#') { Q.push(make_pair(next_x, next_y)); visited[next_x][next_y] = 1; if (dis[next_x][next_y] > dis[cur.first][cur.second] + 1) { dis[next_x][next_y] = dis[cur.first][cur.second] + 1; befor[make_pair(next_x, next_y)] = befor[cur]; } } } } int x, y, d = maxn; //cout << "--------------------" << endl; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { //cout << mat[i][j]; if (mat[i][j] >= '0' && mat[i][j] <= '9') { // 当前有培养液 if (dis[i][j] < d) { d = dis[i][j]; x = i; y = j; } } } //cout << endl; } /* for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cout << cell[i][j]; } cout << endl; } cout << d << ' ' << ans << endl; */ if (d == maxn) return false; ans += d; int xx = befor[Node(x, y)].first, yy = befor[Node(x, y)].second; cell[xx][yy]--; cell[x][y] += mat[x][y] - '0' + 1; mat[x][y] = '.'; return true; }
int main() { int T; cin >> T; while (T--) { cin >> n >> m; ans = 0; int liquid_cnt = 0; memset(cell, 0, sizeof(cell)); memset(mat, 0, sizeof(mat)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> mat[i][j]; if (mat[i][j] >= '0' && mat[i][j] <= '9') liquid_cnt++; if (mat[i][j] == 'S') { cell[i][j]++; } } }
for (int i = 0; i < liquid_cnt; i++) { memset(visited, 0, sizeof(visited)); befor.clear(); for (int j = 0; j < n; j++) { for (int k = 0; k < m; k++) { dis[j][k] = maxn; if (cell[j][k] > 0) { Q.push(Node(j, k)); visited[j][k] = 1; dis[j][k] = 0; befor[Node(j, k)] = Node(j, k); } } }
bool flag = bfs(); if (!flag) { ans = -1; break; } } cout << ans << endl; } //pair<int, int>(2, 3) == make_pair(2, 3); return 0; } /* 1 7 10 2342312323 .......... S######### .......... 123....... .......... 123.......
1 5 3 #1# 1S1 #.# #.# #1# */
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <map> #include <cstdlib> #include <algorithm> #include <queue> #define max(a,b) (a>b?a:b) using namespace std; const int maxn=250005; struct node { int x,y,l; }k[maxn]; int t,m,n,lm,d[4][2]={1,0,-1,0,0,1,0,-1}; char mm[105][105]; void add(int u,int v,int w){ k[lm].x=u; k[lm].y=v; k[lm].l=w; lm++; } void bfs(int s,int x,int y){ node a,b; a.x=x,a.y=y,a.l=0; int vis[105][105]={0}; vis[a.x][a.y]=1; queue<node>qu; qu.push(a); while(!qu.empty()){ a=qu.front(); qu.pop(); if((mm[a.x][a.y]>='1'&&mm[a.x][a.y]<='9')||mm[a.x][a.y]=='S'){ if(a.l)add(s,a.x*100+a.y,a.l); } b.l=a.l+1; for(int i=0;i<4;i++){ b.x=a.x+d[i][0]; b.y=a.y+d[i][1]; if(b.x>=0&&b.x<n&&b.y>=0&&b.y<m&&!vis[b.x][b.y]&&mm[b.x][b.y]!='#'){ vis[b.x][b.y]=1; qu.push(b); } } } } void make_adge(){ for(int i=0;i<n;i++) for(int j=0;j<m;j++) if(mm[i][j]!='#'&&mm[i][j]!='.') bfs(i*100+j,i,j); }
int r[10005]; int fi(int n){ return r[n]=r[n]==n?n:fi(r[n]); } int cmp(node x,node y){ return x.l<y.l; } void solve(){ int vis[10005]; for(int i=0;i<10000;i++){ r[i]=i; vis[i]=0; if(mm[i / 100][i % 100] == 'S') mm[i / 100][i % 100] = 47; } sort(k,k+lm,cmp); int out=0; for(int i=0;i<lm;i++){ int fx=fi(k[i].x); int fy=fi(k[i].y); if((mm[k[i].x/100][k[i].x%100]-'0')+2==vis[k[i].x])continue ; if((mm[k[i].y/100][k[i].y%100]-'0')+2==vis[k[i].y])continue ; if(fx!=fy){ r[fx]=fy; vis[k[i].x]++; vis[k[i].y]++; //printf("[%d,%d] -> [%d,%d]\n", k[i].x / 100, k[i].x % 100, k[i].y / 100, k[i].y % 100); out+=k[i].l; } } int ans=0; for(int i=0;i<10000;i++){ if((mm[i/100][i%100]>='1'&&mm[i/100][i%100]<='9')||mm[i/100][i%100]==47){ if(fi(i)==i){ ans++; } } } if(ans!=1)out=-1; cout<<out<<endl; } int main(){ // freopen("test0.in","r",stdin); // freopen("test0.out","w",stdout); scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); memset(mm,0,sizeof(mm)); for(int i=0;i<n;i++){ scanf("%s",mm[i]); } lm=0; make_adge(); solve(); } return 0; }
|