Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <queue>
- using namespace std;
- int main() {
- int redovi, koloni;
- cin >> redovi >> koloni;
- char lavirint[redovi][koloni];
- int poseteno[redovi][koloni];
- int si, sj;
- for(int i = 0; i < redovi; i++) {
- for(int j = 0; j < koloni; j++) {
- cin >> lavirint[i][j];
- if(lavirint[i][j] == 'P') {
- si = i;
- sj = j;
- }
- poseteno[i][j] = 0;
- }
- }
- int direction_i[] = {+1, -1, 0, 0};
- int direction_j[] = {0, 0, +1,-1};
- queue<int> Q;
- Q.push(si);
- Q.push(sj);
- Q.push(0);
- while(Q.size() > 0) {
- int ci = Q.front();
- Q.pop();
- int cj = Q.front();
- Q.pop();
- int cekor = Q.front();
- Q.pop();
- if(lavirint[ci][cj] == 'K') {
- cout << cekor << endl;
- break;
- }
- for(int i = 0; i < 4; i++) {
- int ti = ci + direction_i[i];
- int tj = cj + direction_j[i];
- if(ti < redovi and ti >= 0 and tj < koloni and tj >= 0 and lavirint[ti][tj] != '#' and poseteno[ti][tj] == 0) {
- Q.push(ti);
- Q.push(tj);
- Q.push(cekor + 1);
- poseteno[ti][tj] = 1;
- }
- }
- }
- return 0;
- }
- /*
- 8 8
- P...#..A
- ........
- ...#K...
- B.......
- ##......
- ########
- ........
- ........
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement