题解 | #迷宫问题#
迷宫问题
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);
}
#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);
}