这有一个迷宫,有8行和8列:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,1
0表示道路,1表示墙。
现在输入一个道路的坐标作为起点,再如输入一个道路的坐标作为终点,问最少走几步才能从起点到达终点?
注:一步是指从一坐标点走到其上下左右相邻坐标点,如:从( 3 , 1 )到( 4 , 1 )
第一行输入一个整数n(0 < n <= 100),表示有n组测试数据;
随后n行,每行有四个整数a,b,c,d(0 <= a,b,c,d <= 8)分别表示起点的行、列,终点的行、列。
输出最少走几步。
输入:
2
3 1 5 7
3 1 6 7
输出:
12
11
典型的一道搜索题。一般最短路径的问题都用bfs解决,但是很多问题也可以用dfs解决,用bfs解决这道题时注意如果每结束一组测试数据要将标记的数组变为0!如果把队列定义为全局变量注意每结束一组测试数据要清空队列。
#include
using namespace std;
struct node
{
int x;
int y;
int step;
node(int xx,int yy,int step1):x(xx),y(yy),step(step1){}
};
int book[9][9];
int fg[9][9]={{1,1,1,1,1,1,1,1,1},{1,0,0,1,0,0,1,0,1},{1,0,0,1,1,0,0,0,1},{1,0,1,0,1,1,0,1,1},{1,0,0,0,0,1,0,0,1},{1,1,0,1,0,1,0,0,1},{1,1,0,1,0,1,0,0,1},{1,1,0,1,0,0,0,0,1},{1,1,1,1,1,1,1,1,1}};
void bfs(int a,int b,int c,int d)
{
queueq;
q.push(node(a,b,0));
book[a][b]=1;
while(!q.empty())
{
node s=q.front();
if(s.x==c&&s.y==d)
{
cout<=0&&s.x-1<9&&s.y>=0&&s.y<9&&!book[s.x-1][s.y]&& fg[s.x-1][s.y]==0)
{
q.push(node(s.x-1,s.y,s.step+1));
book[s.x-1][s.y]=1;
}
if(s.x>=0&&s.x<9&&s.y+1>=0&&s.y+1<9&&!book[s.x][s.y+1]&&fg[s.x][s.y+1]==0)
{
q.push(node(s.x,s.y+1,s.step+1));
book[s.x][s.y+1]=1;
}
if(s.x+1>=0&&s.x+1<9&&s.y>=0&&s.y<9&&!book[s.x+1][s.y]&&fg[s.x+1][s.y]==0)
{
q.push(node(s.x+1,s.y,s.step+1));
book[s.x+1][s.y]=1;
}
if(s.x>=0&&s.x<9&&s.y-1>=0&&s.y-1<9&&!book[s.x][s.y-1]&&fg[s.x][s.y-1]==0)
{
q.push(node(s.x,s.y-1,s.step+1));
book[s.x][s.y-1]=1;
}
}
q.pop();
}
return ;
}
int main()
{
int n,a,b,c,d;
cin>>n;
while(n--)
{
memset(book,0,sizeof(book)); //一定要把用于标记的数组清零
cin>>a>>b>>c>>d;
bfs(a,b,c,d);
}
}
这是BFS常用的模板,对于这道题我们可以把中间的四个if语句化简一下。
#include
using namespace std;
int a[9][9]=
{
1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,0,1,
1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1
};
struct node
{
int x,y,step;
};
queue q;
int c[4]={0,0,-1,1},b[4]={-1,1,0,0};//增加了用于标记的数组
int visite[9][9];
void bfs(int x1,int y1,int x2,int y2)
{
int i,s,t;
while(!q.empty())
q.pop();//把队列清空!! 如果定义的q为局部变量就不用了
node e={x1,y1,0};
visite[x1][y1]=1;
q.push(e);
while(!q.empty())
{
e=q.front(); //队首元素取出来
if(e.x==x2&&e.y==y2)
{
cout<=0&&t>=0&&t<=8&&!visite[s][t]&&!a[s][t])
{
node e1={s,t,e.step+1};
q.push(e1);
visite[s][t]=1;
}
}
q.pop();
}
return ;
}
int main()
{
int n,x1,x2,y1,y2;
cin>>n;
while(n--)
{
memset(visite,0,sizeof(visite));//把标记的数组初始化为0!!!!!!
cin>>x1>>y1>>x2>>y2;
bfs(x1,y1,x2,y2);
}
return 0;
}
#include
using namespace std;
int ans,a,b,c,d;
int vis[9][9];
int s[9][9]=
{
1,1,1,1,1,1,1,1,1,
1,0,0,1,0,0,1,0,1,
1,0,0,1,1,0,0,0,1,
1,0,1,0,1,1,0,1,1,
1,0,0,0,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,1,0,0,1,
1,1,0,1,0,0,0,0,1,
1,1,1,1,1,1,1,1,1
};
void dfs(int i,int j,int cnt)
{
if(i<0||i>8||j<0||j>8||vis[i][j]||s[i][j]||cnt>=ans)
return;
if(i==c&&j==d)
{
ans=cnt;
return;
}
vis[i][j]=1;
dfs(i,j-1,cnt+1);
dfs(i-1,j,cnt+1);
dfs(i,j+1,cnt+1);
dfs(i+1,j,cnt+1);
vis[i][j]=0;
}
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>a>>b>>c>>d;
ans=100;
dfs(a,b,0);
cout<