Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- #define debug(x) cerr << '[' << (#x) << "] = " << x << '\n'
- template<class T> using ordered_set = tree<T, null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update> ;
- int N, M;
- char g[20][20];
- vector<pair<int,int>>gold;
- int initx, inity, GN;
- int dp[20][20][1<<15];
- int dist(int x, int y, int x2, int y2) {
- return max(abs(x-x2), abs(y-y2));
- }
- int solve(int x, int y, int mask) {
- int& ret = dp[x][y][mask];
- if(ret!=-1) return ret;
- if(mask==(1<<GN)-1) {
- return dist(x, y, initx, inity);
- }
- ret = INT_MAX;
- for(int i=0; i<GN; ++i) if(~mask >> i & 1) {
- ret = min(ret, solve(gold[i].first, gold[i].second, mask|(1<<i)) + dist(x, y, gold[i].first, gold[i].second));
- }
- return ret;
- }
- void PlayGround(int T) {
- cin >> N >> M;
- for(int i=0; i<N; ++i)
- for(int j=0; j<M; ++j)
- for(int k=0; k<(1<<15); ++k)
- dp[i][j][k] = -1;
- for(int i=0; i<N; ++i) {
- for(int j=0; j<M; ++j) {
- char& c = g[i][j];
- cin >> c;
- if(c=='x') {
- initx = i, inity = j;
- }
- else if(c=='g') {
- gold.push_back(make_pair(i, j));
- }
- }
- }
- GN = gold.size();
- cout << "Case " << T << ": " << solve(initx, inity, 0) << "\n";
- gold.clear();
- #ifndef ONLINE_JUDGE
- cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
- #endif
- }
- int main() {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin);
- #endif
- int T;
- cin >> T;
- for(int i=1; i<=T; ++i)
- PlayGround(i);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement