题解 | #迷宫问题#

迷宫问题

http://www.nowcoder.com/practice/cf24906056f4488c9ddb132f317e03bc

#include<iostream>
#include<vector>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        int c[a+2][b+2];
        
        for(int i=0;i<a;i++)
        {
            for(int j=0;j<b;j++)
            {
                cin>>c[i][j];
            }
        }
       // for(int i=0;i<a+2;i++)
       // {
        //    c[i][0]=1;
       //     c[i][b+1]=1;
     //   }
       //  for(int j=0;j<b+2;j++)
      //  {
       //     c[0][j]=1;
      //      c[a+1][j]=1;
     //   }
        vector<pair<int, int>>e;
        e.push_back(pair<int,int>(0, 0));
      while((e.back().first!=a-1)||(e.back().second!=b-1))
        {
          
            if(c[e.back().first][e.back().second+1]==0&&e.back().second+1<b)
            {
               if(e.size()==1)
               {
                e.push_back(pair<int, int>(e.back().first,e.back().second+1));
               }
                else if(e.size()>1&&(e[e.size()-2].first!=e.back().first||e[e.size()-2].second!=e.back().second+1))
                {
                    e.push_back(pair<int, int>(e.back().first,e.back().second+1));  
                }
                 else if((c[e.back().first][e.back().second-1]==1||e.back().second==0)&&(c[e.back().first+1][e.back().second]==1||e.back().first==a)&&(c[e.back().first-1][e.back().second]==1||e.back().first==0))
                {
                      c[e.back().first][e.back().second]=1;
                    e.pop_back();
                }
                
            }
            if((e.back().first!=a-1)||(e.back().second!=b-1))
          {
            if(c[e.back().first+1][e.back().second]==0&&e.back().first+1<a)
            {
              if(e.size()==1)
              {
               e.push_back(pair<int, int>(e.back().first+1,e.back().second));
              }
             else    if(e.size()>1&&(e[e.size()-2].first!=e.back().first+1||e[e.size()-2].second!=e.back().second))
                {
                      e.push_back(pair<int, int>(e.back().first+1,e.back().second));
                }
                else if((c[e.back().first][e.back().second-1]==1||e.back().second==0)&&(c[e.back().first][e.back().second+1]==1||e.back().second==b)&&(c[e.back().first-1][e.back().second]==1||e.back().first==0))
                {
                     c[e.back().first][e.back().second]=1;
                    e.pop_back();
                }
            } 
            }
            if((e.back().first!=a-1)||(e.back().second!=b-1))
          {
              if(c[e.back().first][e.back().second-1]==0&&e.back().second-1>=0&&e.back().second-1!=e[e.size()-2].second)
            {
                if(e.size()==1)
              { 
              e.push_back(pair<int, int>(e.back().first,e.back().second-1)); 
                }
                else if(e.size()>1&&(e[e.size()-2].first!=e.back().first||e[e.size()-2].second!=e.back().second-1))
                {
                      e.push_back(pair<int, int>(e.back().first,e.back().second-1));  
                }
              } 
               
            }
            if((e.back().first!=a-1)||(e.back().second!=b-1))
          {
                if(c[e.back().first-1][e.back().second]==0&&e.back().first-1>=0&&e.back().first-1!=e[e.size()-2].first)
            {
                   if(e.size()==1)
              { 
                 e.push_back(pair<int, int>(e.back().first-1,e.back().second)); 
                   }
                  else if(e.size()>1&&(e[e.size()-2].first!=e.back().first-1||e[e.size()-2].second!=e.back().second))
                  {
                      e.push_back(pair<int, int>(e.back().first-1,e.back().second));  
                  }
                } 
            }
          if((e.back().first!=a-1)||(e.back().second!=b-1))
          {
            if(c[e.back().first][e.back().second-1]==0&&e.back().second-1>=0&&(e.back().second+1==b||c[e.back().first][e.back().second+1]==1)&&(e.back().first+1==1||c[e.back().first+1][e.back().second]==1))
            {
               c[e.back().first][e.back().second]=1;
                 e.pop_back();
            }
          }
            if((e.back().first!=a-1)||(e.back().second!=b-1))
          {
               if(c[e.back().first-1][e.back().second]==0&&e.back().first-1>=0&&(e.back().second+1==b||c[e.back().first][e.back().second+1]==1)&&(e.back().first+1==1||c[e.back().first+1][e.back().second]==1))
            {
              c[e.back().first][e.back().second]=1;
                e.pop_back();
            }
        }
      }
        for(auto x:e)
            {
                cout<<'('<<x.first<<','<<x.second<<')'<<endl;
            } 
    }
    return(0);
}
全部评论

相关推荐

头像
02-15 16:23
中南大学 Java
野猪不是猪🐗:签了美团真是不一样! 亲戚们都知道我签了美团,过年都围着我问送一单多少钱,还让弟弟妹妹们引以为戒,笑我爸我妈养了个🐢孩子,说从小就知道我这个人以后肯定没出息,我被骂的都快上天了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务