1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| #include <stdio.h> #include <stdlib.h> #include <queue> #define gc() getchar_unlocked() using namespace std; struct INFO{ int y; int x; int stick; int cost; }; bool v[1000][1000][11]; int buf[1000][1000]; int dx[4] = {0,1,0,-1}; int dy[4] = {-1,0,1,0}; queue<INFO> q; int fRI(){ int N=gc(),ret = 0; for(;N<48||N>57;N=gc()); do{ ret = (ret<<3) + (ret<<1) + (N&0b1111); N = gc(); }while(0x30<=N); return ret; } int main(){ register int N = fRI(),M = fRI(),K = fRI(),x,y,stick,cost, nx, ny, i, j; for (i = 0; i < N; i++) for (j = 0; j < M; j++) scanf("%1d", &buf[i][j]); q.push({0,0, K,1}); if(N==1&&M==1) {printf("1"); exit(0);} v[0][0][K] = true; while(!q.empty()){ x = q.front().x; y = q.front().y; stick = q.front().stick; cost = q.front().cost; q.pop(); for(i = 0 ; i < 4 ; ++i){ nx = x + dx[i]; ny = y + dy[i]; if(0<=nx&&nx<M && 0<=ny&&ny<N && !v[ny][nx][stick]){ if(!buf[ny][nx]){ if(ny==N-1&&nx==M-1) {printf("%d",cost+1); exit(0);} else { for(j = stick; j >= 0; --j) v[ny][nx][j] = true; q.push({ny, nx, stick, cost + 1}); } } else if(stick){ for(j = stick; j >= 0; --j) v[ny][nx][j] = true; q.push({ny, nx, stick-1, cost + 1}); } } } } printf("-1"); }
|