Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define F first
  3. #define S second
  4. #define tm itsnottime
  5. using namespace std;
  6. using ll = long long;
  7. using db = long double;
  8. const int sz = 1e2+4;
  9. const int oo = 1e9+4;
  10. const int mod = 1e9+9;
  11. const int cfmod = 998244353;
  12. const bool debug=0;
  13.  
  14. int n, m, h, sx, sy, endx, endy;
  15. int ans[sz][sz][sz], p[sz][sz];
  16. string g[sz];
  17. int xx[5] = {-1,0,1,0,0};
  18. int yy[5] = {0,-1,0,1,0};
  19.  
  20. void read() {
  21.     cin>>n>>m>>h;
  22.     for (int i=0; i<n; i++) {
  23.         cin>>g[i];
  24.         for (int j=0; j<m; j++)
  25.             if (g[i][j]=='A') {
  26.                 sx = i; sy = j;
  27.             } else if (g[i][j]=='B') {
  28.                 endx = i; endy = j;
  29.             }
  30.     }
  31.     for (int i=0; i<n; i++)
  32.         for (int j=0; j<m; j++)
  33.             cin>>p[i][j];
  34. }
  35.  
  36. bool ok(int x, int y) {
  37.     return x<n && x>=0 && y<m && y>=0;
  38. }
  39.  
  40. void sf() {
  41.     for (int i=0; i<n; i++)
  42.         for (int j=0; j<m; j++)
  43.             fill(ans[i][j],ans[i][j]+sz,oo);
  44. }
  45.  
  46.  
  47. void bfs(char c) {
  48.     sf();
  49.     queue<pair<pair<int,int>,int> > q;
  50.     ans[sx][sy][h] = 0;
  51.     q.push({{sx,sy},h});
  52.     while (!q.empty()) {
  53.         int x = q.front().F.F, y = q.front().F.S, ch = q.front().S;
  54.         q.pop();
  55.         for (int i=0; i<5; i++) {
  56.             int nx = x+xx[i], ny = y+yy[i], nh;
  57.             if (!ok(nx,ny) || g[nx][ny]=='X') continue;
  58.             if (g[nx][ny]==c) nh = min(h,ch+p[nx][ny]);
  59.                 else nh = ch-p[nx][ny];
  60.             if (nh<1 || ans[nx][ny][nh]<ans[x][y][ch]+1) continue;
  61.             ans[nx][ny][nh] = ans[x][y][ch]+1;
  62.             q.push({{nx,ny},nh});
  63.         }
  64.     }
  65. }
  66.  
  67. void print() {
  68.     int res=oo;
  69.     for (int i=1; i<=h; i++)
  70.         res = min(res,ans[endx][endy][i]);
  71.     if (res==oo) cout<<"impossible\n"; else cout<<res<<endl;
  72. }
  73.  
  74. void solve() {
  75.     read();
  76.     if (debug) cout<<sx<<' '<<sy<<' '<<endx<<' '<<endy<<endl;
  77.     if (debug) cout<<"ok read"<<endl;
  78.     bfs('F');
  79.     if (debug) cout<<"ok bfsF"<<endl;
  80.     print();
  81.     if (debug) cout<<"ok printF"<<endl;
  82.     bfs('I');
  83.     if (debug) cout<<"ok bfsI"<<endl;
  84.     print();
  85.     if (debug) cout<<"ok printI"<<endl;
  86. }
  87.  
  88. int main(){
  89.     ios_base::sync_with_stdio(0);
  90.     cin.tie(NULL); cout.tie(NULL);
  91. //    freopen("in.txt", "r", stdin);
  92. //    freopen("out.txt", "w", stdout);
  93. //    int t; cin>>t; while (t--)
  94.     solve();
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement