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 都可以

1.BFS 

#include <iostream>
#include <queue>
using namespace std;
int map1[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 map[9][9];
int gonext[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int strx,stry,endx,endy;
bool isfind;
struct Pos{
	int x,y,s;
} p,pt;
queue<Pos> que;
void inist()
{
	for(int i=0;i<9;i++)
		for(int j=0;j<9;j++)
			map[i][j] = map1[i][j];
}

int main()
{
	int T,i,j;
	cin>>T;
	while(T--)
	{
		cin>>strx>>stry>>endx>>endy;
		if(strx==endx && stry == endy)
		{
			cout<<"0"<<endl;
			continue;
		}	
		inist();
		isfind = false;
		while(!que.empty())
				que.pop();
		p.x = strx,p.y = stry,p.s = 0;	
		que.push(p);
		while(!que.empty() && isfind==false)
		{
			p = que.front();que.pop();
			map[p.x][p.y] = 5;
			for(i=0;i<4;i++)
			{
				pt.x = p.x + gonext[i][0];
				pt.y = p.y + gonext[i][1];
				pt.s = p.s + 1;
				if(map[pt.x][pt.y]==0)
				{
					if(pt.x==endx && pt.y==endy)
					{
						isfind = true;
						break;	
					}	
					else
					{
						que.push(pt);
					}
				}
			}
		}
		if(isfind)
			cout<<pt.s<<endl;
		else 
			cout<<"No find"<<endl;		
	}
	return 0;
}
下面是注释

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
using namespace std;
int map1[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 map[9][9];//用来查找 
int gonext[4][2]={{1,0},{0,1},{-1,0},{0,-1}};//用来移动坐标 
int strx,stry,endx,endy;//起点终点 
bool isfind;//标记元素 
struct Pos{//结构体表示坐标 ,s表示步数 
	int x,y,s;
} p,pt; 
queue<Pos> que;//查找队列 
void inist()//初始话map,每次查找都要重新初始化 
{
	for(int i=0;i<9;i++)
		for(int j=0;j<9;j++)
			map[i][j] = map1[i][j];
}

int main()
{
	int T,i,j;
	cin>>T;
	while(T--)
	{
		cin>>strx>>stry>>endx>>endy;
		if(strx==endx && stry == endy)//如果起点等于终点 
		{
			cout<<"0"<<endl;
			continue;
		}	
		inist();//初始化map 
		isfind = false;
		while(!que.empty())//清空队列 
				que.pop();
		p.x = strx,p.y = stry,p.s = 0;	//p 表示当前坐标, 下面pt表示我们走一步后的临时坐标 
		que.push(p);//一定要先把起点放进去 
		while(!que.empty() && isfind==false)//循环条件 1,队列不空,2,还没找到, 
		{
			p = que.front();que.pop();//取出队列的第一项 
			map[p.x][p.y] = 1;//标记为走过 
			for(i=0;i<4;i++)//查看四个方向 
			{
				pt.x = p.x + gonext[i][0];
				pt.y = p.y + gonext[i][1];
				pt.s = p.s + 1;//记录步数 
				if(map[pt.x][pt.y]==0)//判断是否可以走 ,这里其实可以不要判断是否越界,应为周围有墙, 
				{
					if(pt.x==endx && pt.y==endy)//找到了 
					{
						isfind = true;
						break;	
					}	
					else//没找到 
					{
						que.push(pt);
					}
				}
			}
		}
		if(isfind)
			cout<<pt.s<<endl;//输出就会 
		else 
			cout<<"No find"<<endl;//做题这句就没必要,我自己加的,强迫症		
	}
	return 0;
}
回头再试试DFS

2,DFS

dfs不算难

#include <iostream>
#include <queue>
using namespace std;
int map1[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 map[9][9];
int gonext[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
int strx,stry,endx,endy;
int themin;

void inist()
{
	for(int i=0;i<9;i++)
		for(int j=0;j<9;j++)
			map[i][j] = map1[i][j];
}
void dfs(int x,int y,int step)
{
	int tx,ty;
	if(x==endx&&y==endy)//如果找到就退出 
	{
		if(step<themin)
			themin = step;
		return;
	}
	for(int i=0;i<4;i++)//对四个方向dfs 
	{
		tx = x+gonext[i][0];
		ty = y+gonext[i][1];
		if(map[tx][ty]==0)
		{
			map[tx][ty] = 1;
			dfs(tx,ty,step+1);
			map[tx][ty] = 0;
		}
	}
}
int main()
{
	int T,i,j;
	cin>>T;
	while(T--)
	{
		cin>>strx>>stry>>endx>>endy;
		themin = 999999;//初始化下 
		inist();
		map[strx][stry] = 1;
		dfs(strx,stry,0);
		cout<<themin<<endl;
	}
	return 0;
}        

奈何我冒泡的算法如何打动你超时的心!!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
孪生素数是指两个素数之间的差值为2的素数对。通过筛选法可以找出给定素数范围内的所有孪生素数的组数。 在引用的代码中,使用了递归筛选法来解决孪生素数问题。该程序首先使用循环将素数的倍数标记为非素数,然后再遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 具体实现过程如下: 1. 定义一个数组a[N,用来标记数字是否为素数,其中N为素数范围的上限。 2. 初始化数组a,将0和1标记为非素数。 3. 输入要查询的孪生素数的个数n。 4. 循环n次,每次读入一个要查询的素数范围num。 5. 使用两层循环,外层循环从2遍历到num/2,内层循环从i的平方开始,将素数的倍数标记为非素数。 6. 再次循环遍历素数数组,找出相邻素数之间差值为2的素数对,并统计总数。 7. 输出总数。 至此,我们可以使用这个筛选法的程序来解决孪生素数问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [python用递归筛选法求N以内的孪生质数(孪生素数)](https://blog.csdn.net/weixin_39734646/article/details/110990629)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [NYOJ-26 孪生素数问题](https://blog.csdn.net/memoryofyck/article/details/52059096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值