Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define F first
- #define S second
- #define tm itsnottime
- using namespace std;
- using ll = long long;
- using db = long double;
- const int sz = 1e2+4;
- const int oo = 1e9+4;
- const int mod = 1e9+9;
- const int cfmod = 998244353;
- const bool debug=0;
- int n, m, h, sx, sy, endx, endy;
- int ans[sz][sz][sz], p[sz][sz];
- string g[sz];
- int xx[5] = {-1,0,1,0,0};
- int yy[5] = {0,-1,0,1,0};
- void read() {
- cin>>n>>m>>h;
- for (int i=0; i<n; i++) {
- cin>>g[i];
- for (int j=0; j<m; j++)
- if (g[i][j]=='A') {
- sx = i; sy = j;
- } else if (g[i][j]=='B') {
- endx = i; endy = j;
- }
- }
- for (int i=0; i<n; i++)
- for (int j=0; j<m; j++)
- cin>>p[i][j];
- }
- bool ok(int x, int y) {
- return x<n && x>=0 && y<m && y>=0;
- }
- void sf() {
- for (int i=0; i<n; i++)
- for (int j=0; j<m; j++)
- fill(ans[i][j],ans[i][j]+sz,oo);
- }
- void bfs(char c) {
- sf();
- queue<pair<pair<int,int>,int> > q;
- ans[sx][sy][h] = 0;
- q.push({{sx,sy},h});
- while (!q.empty()) {
- int x = q.front().F.F, y = q.front().F.S, ch = q.front().S;
- q.pop();
- for (int i=0; i<5; i++) {
- int nx = x+xx[i], ny = y+yy[i], nh;
- if (!ok(nx,ny) || g[nx][ny]=='X') continue;
- if (g[nx][ny]==c) nh = min(h,ch+p[nx][ny]);
- else nh = ch-p[nx][ny];
- if (nh<1 || ans[nx][ny][nh]<ans[x][y][ch]+1) continue;
- ans[nx][ny][nh] = ans[x][y][ch]+1;
- q.push({{nx,ny},nh});
- }
- }
- }
- void print() {
- int res=oo;
- for (int i=1; i<=h; i++)
- res = min(res,ans[endx][endy][i]);
- if (res==oo) cout<<"impossible\n"; else cout<<res<<endl;
- }
- void solve() {
- read();
- if (debug) cout<<sx<<' '<<sy<<' '<<endx<<' '<<endy<<endl;
- if (debug) cout<<"ok read"<<endl;
- bfs('F');
- if (debug) cout<<"ok bfsF"<<endl;
- print();
- if (debug) cout<<"ok printF"<<endl;
- bfs('I');
- if (debug) cout<<"ok bfsI"<<endl;
- print();
- if (debug) cout<<"ok printI"<<endl;
- }
- int main(){
- ios_base::sync_with_stdio(0);
- cin.tie(NULL); cout.tie(NULL);
- // freopen("in.txt", "r", stdin);
- // freopen("out.txt", "w", stdout);
- // int t; cin>>t; while (t--)
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement