NYOJ-58 最少步数

最少步数

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 4
描述

这有一个迷宫,有0~8行和0~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


介个题是一个DFS问题,处理好递归函数就行,解释都在代码中。也可以用BFS处理,BFS比较简单,一步一步走就行,不需要找最小值,走到了就是。

DFS代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <queue>
#define M 10000
using namespace std;
int x,y,ex,ey,ans;
int map[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
};
int fx[4]={1,-1,0,0};//定义方向数组 
int fy[4]={0,0,1,-1};
void f(int x,int y,int c)
{
	if(x==ex&&y==ey){    //递归结束条件(在移动到终点时)  
	    if(c<ans)
		    ans=c;
	}
		
	else
	{
		for(int i=0;i<4;i++)  //分四个方向进行判定  
		{
			int nx=x+fx[i],ny=y+fy[i]; //令该点移动  
			if(map[nx][ny]==0&&c+1<ans)  //判断移动后是否是墙  
			{                          //如果不是墙,执行  
				map[nx][ny]=1;         //使这个点标记为已经走过  
				f(nx,ny,c+1);          //继续执行f,判断下一个点 
				map[nx][ny]=0;         //在执行完所有的判断后还原为未走过  
			}
		}
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		int c=0;
		scanf("%d%d%d%d",&x,&y,&ex,&ey);
		map[x][y]=1;   //将起点标记为走过  
		ans=M;         //记录最少移动步数  
		f(x,y,c);
		printf("%d\n",ans);
		map[x][y]=0;   //最后将初始标记点还原  
	}
	return 0;
}

BFS代码在下边:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <queue>
#define M 10000
using namespace std;
int x,y,ex,ey;
int map[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;
};
bool vis[9][9];
int fx[4]={1,-1,0,0};             //定义方向数组 
int fy[4]={0,0,1,-1};
int BFS()
{
	node s;                      //定义初始的结构体  
	s.x=x;s.y=y;s.step=0;
	vis[x][y]=true;
	queue <node > q;             //定义队列,储存数据  
	q.push(s);                   //第一个数据进队列  
	while(!q.empty())            //当队列为空时,结束循环  
	{
		node now=q.front();      //队列第一个取出来  
		q.pop();                 //第一个出队列  
		if(now.x==ex&&now.y==ey){ //如果到达终点,结束输出步数  
			return now.step;
		}
		for(int i=0;i<4;i++)     //分四个方向进行  
		{
			node next;           //定义下一个位置(结构体类型)  
			next.x=now.x+fx[i];  //赋值  
			next.y=now.y+fy[i];
			next.step=now.step;
			if(vis[next.x][next.y]==false&&map[next.x][next.y]==0)
			{
				vis[next.x][next.y]=true; //如果不是墙,走过的点变为墙  
			    next.step++;              //步数+1 
			    q.push(next);             //下一个点进队列  
			}
		}
	}
}
int main()
{
	int n,ans;
	scanf("%d",&n);
	while(n--)
	{
		memset(vis,false,sizeof(vis));
		scanf("%d%d%d%d",&x,&y,&ex,&ey);
		ans=BFS();                 //ans表示步数 
		printf("%d\n",ans);
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/Poseidon__ming/article/details/52052952