Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int TABL[52][52];
- int PATH[52][52];
- void Wave(int a,int b, int n){
- TABL[a][b]=n;
- if (a-1>-1){
- if (TABL[a-1][b]>n+1)
- Wave(a-1, b, n+1);
- }
- if (a+1<53){
- if (TABL[a+1][b]>n+1)
- Wave(a+1, b, n+1);
- }
- if (b-1>-1){
- if (TABL[a][b-1]>n+1)
- Wave(a, b-1, n+1);
- }
- if (b+1<53){
- if (TABL[a][b+1]>n+1)
- Wave(a, b+1, n+1);
- }
- return;
- }
- void WaveBack(int a, int b, int n){
- if(TABL[a][b]==-1)
- return;
- PATH[a][b]=1;
- if (a-1>0){
- if (TABL[a-1][b]==n-1){
- WaveBack(a-1, b, n-1);
- //cout<<a<<b<<endl;
- return;
- }
- }
- if (a+1<52){
- if (TABL[a+1][b]==n-1){
- WaveBack(a+1, b, n-1);
- //cout<<a<<b<<endl;
- return;
- }
- }
- if (b-1>0){
- if (TABL[a][b-1]==n-1){
- WaveBack(a, b-1, n-1);
- //cout<<a<<b<<endl;
- return;
- }
- }
- if (b+1<52){
- if (TABL[a][b+1]==n-1){
- WaveBack(a, b+1, n-1);
- //cout<<a<<b<<endl;
- return;
- }
- }
- }
- int main()
- {
- cout<<"Enter firstly length, then width of your field"<<endl;
- int N, M;
- cin>>N>> M;
- //--------------------------------------------------------Writing the table---------------------------------------------
- int stx, sty, endx, endy;
- cout << "If there is a start point here, press 0" << endl ;
- cout << "IF there is a easy go point, press 1" << endl ;
- cout << "If there is an obstacle, press 2" << endl ;
- cout << "If there is end point, press 3" << endl ;
- for ( int i = 1 ; i < N + 1 ; i ++ )
- {
- for ( int j = 1 ; j < M + 1 ; j ++ ) {
- //------------------------------drawing a table-------------------------------------
- for (int i_=1; i_<N+1; i_++) {
- for (int j_=1; j_<M+1; j_++){
- if (i==i_ && j==j_)
- std::cout << "?";
- else if(TABL[i_][j_]==-1)
- cout<<"*";
- else if(endx==i_ && endy==j_)
- cout<<"x";
- else if(i_==stx && j_==sty)
- cout<<"+";
- else
- cout<<"-";
- }
- std::cout << std::endl;
- }
- cout<<"/////////////////////////////////////"<<endl;
- //-----------------------------------going on---------------------------------------
- int cell_;
- cin>>cell_;
- if (cell_==0){
- TABL[i][j]=0;
- stx=i;
- sty=j;
- }
- else if (cell_==1)
- TABL[i][j]=N*M;
- else if (cell_==2)
- TABL[i][j]=-1;
- else if (cell_==3){
- TABL[i][j]=N*M;
- endx=i;
- endy=j;
- }
- }
- for (int i=0; i<N+2; i++){
- TABL[0][i]=-1;
- TABL[N+1][i]=-1;
- TABL[i][0]=-1;
- TABL[i][M+1]=-1;
- }
- }
- //---------------------------------------------------------something-------------------------
- //--------------------------------------------------------------WaveAlg-------------------------------------------
- Wave(stx, sty, 0);
- if (TABL[endx][endy]==M*N){
- cout<<"Sorry, there is no way... :-(";
- cin>>M;
- return 0;
- }
- else
- WaveBack(endx, endy, TABL[endx][endy]);
- //------------------------------------------------------------Drawing your path-----------------------------------------------
- for (int i=1; i<N+1; i++) {
- for (int j=1; j<M+1; j++){
- if (PATH[i][j]==1)
- std::cout << "+";
- else if(TABL[i][j]==-1)
- cout<<"*";
- else
- cout<<"-";
- }
- std::cout << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement