Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define ld long double
- #define VI vector<int>
- #define _f front
- #define _b back
- #define _pb push_back
- #define _pf push_front
- #define _Pb pop_back
- #define _Pf pop_front
- #define _all(v) v.begin(), v.end()
- #define II pair<int,int>
- #define III pair<int, II>
- #define F first
- #define S second
- #define _mp make_pair
- #define LINE putc('\n', stdout)
- #define SPACE putc(' ' , stdout)
- #define TAB putc('\t', stdout)
- #define _out(_) printf("%i",_)
- #define __out(_,__) printf("%i %i",_,__)
- #define ___out(_,__,___) printf("%i %i %i",_,__,___)
- #define _outLL(_) printf("%I64d",_)
- #define __outLL(_,__) printf("%I64d %I64d",_,__)
- #define ___outLL(_,__,___) printf("%I64d %I64d %I64d",_,__,___)
- #define _in(_) scanf("%i", &_)
- #define __in(_,__) scanf("%i%i",&_,&__)
- #define ___in(_,__,___) scanf("%i%i%i",&_,&__,&___)
- #define _inLL(_) scanf("%I64d", &_)
- #define __inLL(_,__) scanf("%I64d%I64d",&_,&__)
- #define ___inLL(_,__,___) scanf("%I64d%I64d%I64d",&_,&__,&___)
- #define _debug(_) printf("-----------> Debuging : %i\n",_ );
- #define _for(i, n) for(int i=0 ; i<n ; i++)
- #define _rof(i, n) for(int i=n ; i-- ; )
- #define rm_imp(d, p) (round(d*p)/p)
- #define INF 2e8
- char A[33*33];
- VI v;
- int w, h, n, dist[14][14], D[33*33];
- int memo[14][1<<14];
- int TSP(int p, int mask)
- {
- if(mask==(1<<n)-1) return 0;
- if(memo[p][mask]!=-1) return memo[p][mask];
- int ans = INF;
- _rof(i, n)
- if(!(mask&1<<i))
- ans= min(ans, TSP(i, mask|1<<i) + dist[p][i]);
- return memo[p][mask]=ans;
- }
- int generate_D(int source) //BFS search
- {
- // L U R D
- int dir[] = {-1, w, +1 , -w};
- deque<int> dq;
- memset(D, -1, sizeof D);
- D[source]=0;
- dq._pf(source);//push_back
- while(!dq.empty())
- {
- int s = dq._f(); //front
- dq._Pf();//Pop-back
- _for(j, 4) //4 possible directions
- {
- int d = s + dir[j] ;
- if(D[d]==-1 && A[d]!='x') //not visited yet & not obstacle
- D[d]=D[s]+1 , dq._pb(d); //push_back
- }
- }
- }
- int main()
- {
- repeate :
- while(__in(w,h), w)
- {
- memset(memo, -1, sizeof memo);
- v.clear(); //clear previous dirts + robot positions
- char *ptr = A;
- _for(i, w+2) *ptr++='x'; // make an upper bound of obstacles
- _for(i, h) { *ptr++='x'; scanf("%s", ptr); ptr+=w; *ptr++='x'; } //make left and right bound of obstacles
- _for(i, w+2) *ptr++='x'; // make a lower bound of obstacles
- *ptr=0; // end the string
- w+=2; h+=2; //update dementions
- v._pb(strstr(A, "o")-A); //search for robot position
- ptr=A; ptr--;
- while(ptr=strstr(ptr+1, "*")) v._pb(ptr-A); //search for dirts positions
- n = v.size();
- _for(i, n){
- generate_D(v[i]); //generate all distents from the source (v[i])
- _for(j, n){
- if(D[v[j]]==-1) {_out(-1);LINE;goto repeate;} //if a dirst is unreachable
- dist[i][j]=D[v[j]]; //add the sorest path to dist
- }
- }
- _out(TSP(0, 1));LINE; //dp with bitmasking
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement