Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF = 1e9;
- char ar[201][201];
- int di[] = {0, 0, 0, 1, -1}, dj[] = {0, 1, -1, 0, 0};
- bool vs[201][201][7][1 << 7];
- int n, m;
- struct Data{int d, i, j, c, g, cnt;};
- int main(){
- scanf("%d%d", &n, &m);
- queue < Data > q;
- int cnt = 0;
- for(int i=1;i<=n;i++){
- for(int j=1;j<=m;j++){
- scanf(" %c", &ar[i][j]);
- if(ar[i][j] == 'G')
- ar[i][j] = cnt + 'A', cnt ++;
- else if(ar[i][j] == 'S'){
- vs[i][j][1][0] = true;
- q.push({ 0, i, j, 1, 0, 0 });
- ar[i][j] = '.';
- }
- }
- }
- while(!q.empty()){
- int ud = q.front().d;
- int ui = q.front().i;
- int uj = q.front().j;
- int uc = q.front().c;
- int ug = q.front().g;
- int ucnt = q.front().cnt;
- q.pop();
- for(int d=0;d<5;d++){
- int vi = ui + di[d];
- int vj = uj + dj[d];
- if(!(1 <= vi and vi <= n and 1 <= vj and vj <= m) or ar[vi][vj] == '#') continue;
- int vd = ud + 1;
- int vc = uc + 1;
- if(vc == 7) vc = 1;
- bool check = false;
- if(ar[vi][vj] == '.') check = true;
- else if('1' <= ar[vi][vj] and ar[vi][vj] <= '6' and (ar[vi][vj] - '0' <= ucnt or ar[vi][vj] - '0' == vc))
- check = true;
- int vg = ug;
- int vcnt = ucnt;
- if('A' <= ar[vi][vj] and ar[vi][vj] <= 'G'){
- check = true;
- if((ug & (1 << (ar[vi][vj] - 'A'))) == 0){
- vcnt ++;
- if(vcnt == 7){
- printf("%d", vd);
- return 0;
- }
- }
- vg = ( ug | (1 << (ar[vi][vj] - 'A')) );
- }
- if(!vs[vi][vj][vc][ug] and check){
- vs[vi][vj][vc][ug] = true;
- q.push({ vd, vi, vj, vc, vg, vcnt });
- }
- }
- }
- printf("-1");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement