| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
这组数据通过了,但我还是WAIn Reply To:这里提供一组数据~WA的请看 Posted by:lintao at 2009-03-13 01:11:51 #include <iostream>
using namespace std;
int map[30][30];
int m,n;
int startx,starty;
int minstep;
bool dfs(int row,int col,int step)
{
int i,j;
if(step>10||step>minstep)
return false;
if(row-1>=1&&map[row-1][col]!=1)
{
for(i=row,j=col;i>=1&&map[i][j]!=3&&map[i-1][j]!=1;i--); //往上走
if(i>=1) //在界内
{
if(map[i][j]==3)//找到了
{
if(step+1>10)
return false;
if(step+1<minstep)
minstep=step+1;
return true;
}
else //撞到石头了
{
map[i-1][j]=0;
if(dfs(i,j,step+1))
return true;
map[i-1][j]=1;
}
}
}
if(row+1<=m&&map[row+1][col]!=1)
{
for(i=row,j=col;i<=m&&map[i][j]!=3&&map[i+1][j]!=1;i++); //往下走
if(i<=m)
{
if(map[i][j]==3)
{
if(step+1>10)
return false;
if(step+1<minstep)
minstep=step+1;
return true;
}
else
{
map[i+1][j]=0;
if(dfs(i,j,step+1))
return true;
map[i+1][j]=1;
}
}
}
if(col-1>=1&&map[row][col-1]!=1)
{
for(i=row,j=col;j>=1&&map[i][j]!=3&&map[i][j-1]!=1;j--); //往左走
if(j>=1)
{
if(map[i][j]==3)
{
if(step+1>10)
return false;
if(step+1<minstep)
minstep=step+1;
return true;
}
else
{
map[i][j-1]=0;
if(dfs(i,j,step+1))
return true;
map[i][j-1]=1;
}
}
}
if(col+1<=n&&map[row][col+1]!=1)
{
for(i=row,j=col;j<=n&&map[i][j]!=3&&map[i][j+1]!=1;j++); //往右走
if(j<=n)
{
if(map[i][j]==3)
{
if(step+1>10)
return false;
if(step+1<minstep)
minstep=step+1;
return true;
}
else
{
map[i][j+1]=0;
if(dfs(i,j,step+1))
return true;
map[i][j+1]=1;
}
}
}
return false;
}
int main()
{
int i,j;
while(cin>>n>>m)
{
if(m==0&&n==0)
break;
for(i=0;i<30;i++)
for(j=0;j<30;j++)
map[i][j]=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
{
cin>>map[i][j];
if(map[i][j]==2)
{
startx=i;
starty=j;
}
}
minstep=INT_MAX;
if(dfs(startx,starty,0))
cout<<minstep<<endl;
else
cout<<-1<<endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator