Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <queue>
- using namespace std;
- struct node
- {
- int self = 10000000;
- bool gora = false;
- bool dol = false;
- bool prawo = false;
- bool lewo = false;
- bool finish = false;
- };
- node graph[502][502];
- void BFS(int START_X, int START_Y, int kierunek)
- {
- graph[START_X][START_Y].self = 0;
- queue<pair<int,int > > kolejka;
- queue<int> strzalki;
- kolejka.push({START_X,START_Y});
- strzalki.push(kierunek);
- int cur_x, cur_y, cur_str, licznik;
- while(!kolejka.empty())
- {
- cur_x = kolejka.front().first;
- cur_y = kolejka.front().second;
- cur_str = strzalki.front();
- cout << cur_x << " " << cur_y << endl;
- kolejka.pop();
- strzalki.pop();
- licznik = 0;
- if(graph[cur_x][cur_y].lewa)
- licznik++;
- if(graph[cur_x][cur_y].prawa)
- licznik++;
- if(graph[cur_x][cur_y].dol)
- licznik++;
- if(graph[cur_x][cur_y].gora)
- licznik++;
- if(licznik == 2)
- {
- if(cur_str == 1)
- {
- if(!graph[cur_x][cur_y].gora)
- {
- }
- }
- if(cur_str == 2)
- {
- if(!graph[cur_x][cur_y].dol)
- {
- }
- }
- if(cur_str == 3)
- {
- if(!graph[cur_x][cur_y].prawo)
- {
- }
- }
- if(cur_str == 4)
- {
- if(!graph[cur_x][cur_y].lewa)
- {
- }
- }
- }
- //dol
- if(graph[cur_x][cur_y].dol)
- {
- if(cur_str == 1)
- {
- if(graph[cur_x][cur_y].self < graph[cur_x+1][cur_y].self)
- {
- graph[cur_x+1][cur_y].self = graph[cur_x][cur_y].self;
- kolejka.push({cur_x+1, cur_y});
- strzalki.push(1);
- }
- }
- else
- {
- if(graph[cur_x][cur_y].self + 1 < graph[cur_x+1][cur_y].self)
- {
- kolejka.push({cur_x+1, cur_y});
- strzalki.push(1);
- graph[cur_x+1][cur_y].self = graph[cur_x][cur_y].self + 1;
- }
- }
- }
- //gora
- if(graph[cur_x][cur_y].gora)
- {
- if(cur_str == 2)
- {
- if(graph[cur_x][cur_y].self < graph[cur_x-1][cur_y].self)
- {
- graph[cur_x-1][cur_y].self = graph[cur_x][cur_y].self;
- kolejka.push({cur_x-1, cur_y});
- strzalki.push(2);
- }
- }
- else
- {
- if(graph[cur_x][cur_y].self + 1 < graph[cur_x-1][cur_y].self)
- {
- kolejka.push({cur_x-1, cur_y});
- strzalki.push(2);
- graph[cur_x-1][cur_y].self = graph[cur_x][cur_y].self + 1;
- }
- }
- }
- //prawo
- if(graph[cur_x][cur_y].prawo)
- {
- if(cur_str == 3)
- {
- if(graph[cur_x][cur_y].self < graph[cur_x][cur_y+1].self)
- {
- graph[cur_x][cur_y].self = graph[cur_x][cur_y+1].self;
- kolejka.push({cur_x, cur_y+1});
- strzalki.push(3);
- }
- }
- else
- {
- if(graph[cur_x][cur_y].self + 1 < graph[cur_x][cur_y+1].self)
- {
- kolejka.push({cur_x, cur_y+1});
- strzalki.push(3);
- graph[cur_x][cur_y+1].self = graph[cur_x][cur_y].self + 1;
- }
- }
- }
- if(graph[cur_x][cur_y].lewo)
- {
- if(cur_str == 4)
- {
- if(graph[cur_x][cur_y].self < graph[cur_x][cur_y-1].self)
- {
- graph[cur_x][cur_y-1].self = graph[cur_x][cur_y].self;
- kolejka.push({cur_x, cur_y-1});
- strzalki.push(4);
- }
- }
- else
- {
- if(graph[cur_x][cur_y].self + 1 < graph[cur_x][cur_y-1].self)
- {
- kolejka.push({cur_x, cur_y-1});
- strzalki.push(4);
- graph[cur_x][cur_y-1].self = graph[cur_x][cur_y].self + 1;
- }
- }
- }
- }
- }
- int main()
- {
- int n, m, kier;
- string wej;
- string tab;
- string::iterator found;
- cin >> n >> m >> wej;
- if(wej == "DOL")
- kier = 1;
- if(wej == "GORA")
- kier = 2;
- if(wej == "PRAWO")
- kier = 3;
- if(wej == "LEWO")
- kier = 4;
- int start_x, start_y, finish_x, finish_y;
- for(int i = 0; i < 2*n+1; i++)
- {
- cin >> tab;
- if(i == 0 || i == 2*n+1)
- continue;
- found = find(tab.begin(), tab.end(), 'S');
- if(found != tab.end())
- {
- start_x = i/2;
- start_y = (found - tab.begin())/2;
- }
- found = find(tab.begin(), tab.end(), 'R');
- if(found != tab.end())
- {
- finish_x = i/2;
- finish_y = (found - tab.begin())/2;
- }
- if(i % 2 == 1)
- {
- for(int j = 2; j < 2*m+1; j += 2)
- {
- if(tab[j] == '.')
- {
- graph[i/2][j/2 - 1].prawo = true;
- graph[i/2][j/2].lewo = true;
- }
- }
- }
- if(i % 2 == 0)
- {
- for(int j = 1; j < 2*m+1; j += 2)
- {
- if(tab[j] == '.')
- {
- graph[i/2 - 1][j/2].dol = true;
- graph[i/2][j/2].gora = true;
- }
- }
- }
- }
- cout << endl << "l p g d";
- cout << endl << endl;
- for(int i = 0; i < n; i++)
- {
- for(int j = 0; j < m; j++)
- {
- cout << graph[i][j].lewo << graph[i][j].prawo <<graph[i][j].gora <<graph[i][j].dol << " ";
- }
- cout << endl;
- }
- BFS(start_x, start_y,kier);
- cout << graph[finish_x][finish_y].self;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement