#pragma GCC optimize("O3") #include #include using namespace std; using namespace atcoder; #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define print(var) std::cout<<#var<<"="<<(var)< #define vvi vector #define vvvi vector #define ll long long #define vll vector #define vvll vector #define vvvll vector #define vmi vector #define vvmi vector #define vvvmi vector #define vs vector #define pii pair #define vpii vector> #define bit(x,i)(((x)>>(i))&1) #define inf (1<<30) #define INF (1ll<<60) template inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } inline double get_time() { using namespace std::chrono; return duration_cast(system_clock::now().time_since_epoch()).count(); } unsigned long xor128(void){ static unsigned long x=123456789,y=362436069,z=521288629,w=88675123; unsigned long t; t=(x^(x<<11));x=y;y=z;z=w; return( w=(w^(w>>19))^(t^(t>>8)) ); } long long randInt(long long a,long long b){return a+xor128()%(b-a+1);} double randDouble(double a,double b){return a+(b-a)*xor128()/(double)ULONG_MAX;} double start_time; int n,m; char init_A[50][50]; int c[20],l[20]; bitset<2500> explode_range[20]; bitset<2500> mask_bit_x[50]; bitset<2500> mask_bit_y[50]; bitset<2500> init_broken; bitset<2500> init_shop; int center_shop_x,center_shop_y; vpii around{{0,-1},{-1,0},{1,0},{0,1},{-1,-1},{-1,1},{1,-1},{1,1}}; bool is_valid(int x,int y){ return 0<=x && x explode_wind(int k, int x, int y){ int diff_bit = n*(x-20) + (y-20); bitset<2500> tmpran = (diff_bit>0) ? (explode_range[k]<>(-diff_bit)); tmpran &= mask_bit_x[x] & mask_bit_y[y]; return tmpran; } struct Field{ bitset<2500> is_broken; bitset<2500> is_shop; int building = 0; int shop = 0; Field(){} Field(char AA[50][50]){ for(int i=0;i is_broken; bitset<2500> is_shop; set> explodes; int cost; Bombplan(){} Bombplan(char AA[50][50]){ for(int i=0;i0){ #ifdef LOG_DEBUG cerr<<"simulation building is "< broken_cnt int diff_bit = n*(i-20) + (j-20); int broken_cnt = (explode_wind(k,i,j) & (~is_broken)).count(); if(broken_cnt==0){ #ifdef LOG_DEBUG skip_zero++; #endif continue; } double tmp = (double)c[k]/(double)(broken_cnt); if(chmin(best,tmp)){ best_i = i; best_j = j; best_k = k; #ifdef LOG_DEBUG cerr<<"update broken_cnt is "<> explode_list(){ vector> res(all(explodes)); return res; } }; struct State{ bitset<2500> is_broken, is_shop; int bomb[20]; int bomb_sum=0; vector> hist; int building = 0; int shop = 0; int cost=0; int buy_cost=0; int move_cost=0; int nowx=0,nowy=0; State(){} State(char AA[50][50]){ for(int i=0;i> smart_route(int sx,int sy,int gx,int gy){ char x_ope = gx>sx ? 'D' : 'U'; int dx = gx>sx ? 1 : -1; char y_ope = gy>sy ? 'R' : 'L'; int dy = gy>sy ? 1 : -1; vvi dp(abs(gx-sx)+1,vi(abs(gy-sy)+1,inf)); dp[0][0] = 0; for(int i=0;i<=abs(gx-sx);i++)for(int j=0;j<=abs(gy-sy);j++){ if(i+1<=abs(gx-sx)){ chmin(dp[i+1][j],dp[i][j]+(!is_broken[(sx+dx*(i+1))*n + (sy+dy*j)])+1); } if(j+1<=abs(gy-sy)){ chmin(dp[i][j+1],dp[i][j]+(!is_broken[(sx+dx*i)*n + (sy+dy*(j+1))])+1); } } //dpから最短経路を復元 vector res(abs(gx-sx) + abs(gy-sy)); int i=abs(gx-sx),j=abs(gy-sy); int index = res.size() - 1; while(i>0 || j>0){ if(i>0 && dp[i-1][j]+(!is_broken[(sx+dx*i)*n + (sy+dy*j)])+1==dp[i][j]){ res[index] = x_ope; i--; } else{ res[index] = y_ope; j--; } index--; } int cost = dp[abs(gx-sx)][abs(gy-sy)]; return make_pair(cost,res); } //d:方向 void move(char d){ if(d=='U'){ hist.emplace_back(make_pair(1,0)); nowx--; } if(d=='D'){ hist.emplace_back(make_pair(1,1)); nowx++; } if(d=='L'){ hist.emplace_back(make_pair(1,2)); nowy--; } if(d=='R'){ hist.emplace_back(make_pair(1,3)); nowy++; } if(is_broken[nowx*n+nowy]){ cost += (bomb_sum+1)*(bomb_sum+1); move_cost += (bomb_sum+1)*(bomb_sum+1); } else{ cost += 2*(bomb_sum+1)*(bomb_sum+1); move_cost += 2*(bomb_sum+1)*(bomb_sum+1); } } //x,y:目的地の座標 void move(int x,int y){ pair> route_result = smart_route(nowx, nowy, x, y); for (char direction : route_result.second) { move(direction); } } //piiに対応する座標に移動する void move(pii p){ move(p.first,p.second); } //爆弾を買う //i:爆弾の種類 void buy(int i){ assert(is_shop[nowx*n+nowy]); hist.emplace_back(make_pair(2,i)); bomb[i]++; bomb_sum++; cost+=c[i]; buy_cost+=c[i]; } //爆弾を使う //i:爆弾の種類 void use(int i){ assert(i>=0 && i use_simulate(int i,int x,int y){ assert(i>=0 && i tmpran = explode_wind(i,x,y); int broken_building = (tmpran & (~is_broken)).count(); int broken_shop = (tmpran & is_shop).count(); return make_pair(broken_building,broken_shop); } //最も近い店の座標を返す pii nearest_shop(){ int min_dist=inf; pii res; for(int i=0;i> plan){ //planを実行 int i=0; for(;i<(int)plan.size();i++){ auto [k,x,y] = plan[i]; auto [broken_building,broken_shop] = use_simulate(k,x,y); if(broken_buildingbroken_building) return (double)INF; else{ double eval = (double)usecost/(double)broken_building; return eval; } } //出力 void output(){ cout<>n>>m; int center_shop_dist = inf; for(int i=0;i>A[i][j]; if(A[i][j]=='.'){ init_broken[i*n+j] = 1; init_shop[i*n+j] = 0; } if(A[i][j]=='#'){ init_broken[i*n+j] = 0; init_shop[i*n+j] = 0; } if(A[i][j]=='@'){ init_broken[i*n+j] = 0; init_shop[i*n+j] = 1; if(chmin(center_shop_dist,abs(i-n/2)+abs(j-n/2))){ center_shop_x = i; center_shop_y = j; } } } } for(int i=0;i>c[i]>>l[i]; for(int j=0;j>tmpa>>tmpb; tmpa+=20,tmpb+=20; explode_range[i][tmpa*n + tmpb] = 1; } } //mask-bitの計算 bitset<2500> tate(0),yoko(0); for(int i=0;i<50;i++){ tate[n*i] = 1; yoko[i] = 1; } for(int i=0;i<50;i++){ mask_bit_x[i].reset(); mask_bit_y[i].reset(); for(int j=max(0,i-20);j<=min(49,i+20);j++){ mask_bit_x[i] |= yoko<<(n*j); mask_bit_y[i] |= tate<<(j); } } } void solve(){ /* cerr<<"start"<> plan = bombplan.explode_list(); int nn = plan.size(); State best_state; State init_state(A); State state = init_state; state.plan_exec(plan); int pre_score = state.cost; double start_temp = 30; double end_temp = 0.1; int iter = 0; int MAX_TIME = 2900; while(get_time()-start_timerandDouble(0,1)){ pre_score = next_score; best_state = state; #ifdef LOCAL cerr<<"iter:"<