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 pocetok_i, pocetok_j;
- for(int i = 0; i < redovi; i++) {
- for(int j = 0; j < koloni; j++) {
- cin >> lavirint[i][j];
- if(lavirint[i][j] == 'P') {
- pocetok_i = i;
- pocetok_j = j;
- }
- }
- }
- for(int i = 0; i < redovi; i++) {
- for(int j = 0; j < koloni; j++) {
- poseteno[i][j] = 0;
- }
- }
- queue<int> Q;
- Q.push(pocetok_i); // na koj red se naogame
- Q.push(pocetok_j); // na koja kolona se naogame
- Q.push(0); // kolku polinja sme pominale od pocetnoto pole P do poleto K
- poseteno[pocetok_i][pocetok_j] = 1;
- while(Q.size() > 0) {
- int ci = Q.front();
- Q.pop();
- int cj = Q.front();
- Q.pop();
- int polinja = Q.front();
- Q.pop();
- if(lavirint[ci][cj] == 'K') {
- cout << polinja << endl;
- }
- if(ci + 1 < redovi and lavirint[ci + 1][cj] != '#' and poseteno[ci + 1][cj] == 0) {
- Q.push(ci + 1);
- Q.push(cj);
- Q.push(polinja + 1);
- poseteno[ci + 1][cj] = 1;
- }
- if(ci - 1 >= 0 and lavirint[ci - 1][cj] != '#' and poseteno[ci - 1][cj] == 0) {
- Q.push(ci - 1);
- Q.push(cj);
- Q.push(polinja + 1);
- poseteno[ci - 1][cj] = 1;
- }
- if(cj + 1 < koloni and lavirint[ci][cj + 1] != '#' and poseteno[ci][cj + 1] == 0) {
- Q.push(ci);
- Q.push(cj + 1);
- Q.push(polinja + 1);
- poseteno[ci][cj + 1] = 1;
- }
- if(cj - 1 >= 0 and lavirint[ci][cj - 1] != '#' and poseteno[ci][cj - 1] == 0) {
- Q.push(ci);
- Q.push(cj - 1);
- Q.push(polinja + 1);
- poseteno[ci][cj - 1] = 1;
- }
- }
- return 0;
- }
- /*
- 4 5
- .....
- P#...
- .....
- #.K..
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement