微软校招笔试题#1092 : Have Lunch Together详解以及源码分析

本文介绍了一种算法,用于解决在学校食堂中寻找相邻空闲座位的问题。通过从目标座位出发进行广度优先搜索,该算法能有效找到从当前位置到达相邻座位的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#1092 : Have Lunch Together

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Everyday Littile Hi and Little Ho meet in the school cafeteria to have lunch together. The cafeteria is often so crowded that two adjacent seats are hard to find.

School cafeteria can be considered as a matrix of N*M blocks. Each block can be empty or occupied by people, obstructions and seats. Little Hi and Little Ho starts from the same block. They need to find two adjacent seats(two seats are adjacent if and only if their blocks share a common edge) without passing through occupied blocks. Further more, they want the total distance to the seats is minimal.

Little Hi and Little Ho can move in 4 directions (up, down, left, right) and they can not move outside the matrix.

输入

Input cantains a single testcase.

First line contains two integers N, M, the length and width of school cafeteria.

The next is a matrix consists of N lines, each line containing M characters. Each character describe a block: '.' for empty block, 'P' for people, '#' for obstructions, 'S' for seats and 'H' for Little Hi and Little Ho's starting position.

10 <= N, M <= 100

输出

Output the minimal distance they need to move to reach two adjacent seats. If no such adjacent seats output a line "Hi and Ho will not have lunch." without quotes.

样例输入
10 10
##########
#...P##..#
#S#...#.P#
#S#..#...#
#...#.####
#.#...#.H#
##......##
#..P#..S.#
##.......#
##########
样例输出
25

看到题目,第一个想法竟然是从H开始找到两个S的最短路径,dfs递归,然后找两个S的最短路径的时候怎么写都感觉不对,后来逆向思维,那么我从两个S去找H的最短路径不就好找了么,还有一个问题这个最短路径要怎么找,一开始写了个递归算法,结果超时了,后来想到某个点的最短路径bfs也可以了。

现在写一下步骤:

1.找到两个挨着的S

2.找到每个S到H的最短距离(这里采用广度优先遍历,此处注意已经遍历过得不要再遍历)

import java.util.*;
public class HaveLunch {
	public static int MAX=20005;
	public static int sum=MAX;
	public static int dis=MAX;
	//设置好四个方向
	public static int[][] mark={{1,0},{-1,0},{0,1},{0,-1}};
	public static void main(String args[])
	{
		Scanner in=new Scanner(System.in);
		while(in.hasNextLine())
		{
			String[] line=in.nextLine().split(" ");
			int n=Integer.valueOf(line[0]);
			int m=Integer.valueOf(line[1]);
			char[][] map=new char[n][m];
			for(int i=0;i<n;i++)
			{
				map[i]=in.nextLine().toCharArray();
			}
			sum=MAX;
			for(int i=0;i<n;i++)
			{
				for(int j=0;j<m;j++)
				{
					if(map[i][j]=='S')
					{
						for(int k=0;k<4;k++)
						{
							int tx=i+mark[k][0];
							int ty=j+mark[k][1];
							if(tx<0||tx>=n) continue;
							if(ty<0||ty>=m) continue;
							if(map[tx][ty]=='S')
							{
								dis=MAX;
								//采用广度优先遍历的方式寻找到达H点的最短路径
								bfs(map,i,j,n,m);
								int dis1=dis;
								dis=MAX;
								bfs(map,tx,ty,n,m);
								int dis2=dis;
								if(dis1!=-1&&dis2!=-1&&dis1+dis2<sum)
								{
									sum=dis1+dis2;
								}
							}
						}
					}
				}
			}
			if(sum==MAX)
			{
				System.out.println("Hi and Ho will not have lunch.");
			}
			else
			{
				System.out.println(sum);
			}
		}
	}
	public static void  bfs(char[][] map,int x,int y,int n,int m)
	{
		Queue<Node> queue=new LinkedList<Node>() ;
		boolean[][] dp=new boolean[n][m];
		Node node=new Node(x,y);
		node.step=0;
		queue.add(node);
		dp[x][y]=true;
		//利用队列来实现类似于图的广度优先遍历
		while(!queue.isEmpty())
		{
			Node pre=queue.poll();
			int a=pre.x;
			int b=pre.y;
			if(map[a][b]=='H') 
			{
				dis=pre.step;
				break;
			}
			for(int k=0;k<4;k++)
			{
				int curx=a+mark[k][0];
				int cury=b+mark[k][1];
				if((!dp[curx][cury])&&judge(curx,cury,map,n,m))
				{
					Node cur=new Node(curx,cury);
					cur.step=pre.step+1;
					//找到H节点就可以返回了
					if(map[curx][cury]=='H')
					{
						dis=cur.step;
						return;
					}
					//每个放入的节点都要设置为遍历过
					dp[curx][cury]=true;
					queue.add(cur);
				}
			}
		}
		
	}
	public static boolean judge(int x,int y,char[][] map,int n,int m)
	{
		if(x<0||x>=n) return false;
		if(y<0||y>=m) return false;
		if(map[x][y]=='P'||map[x][y]=='S'||map[x][y]=='#') return false;
		return true;
	}
	public static class Node
	{
		int x;
		int y;
		int step;
		Node(int x,int y)
		{
			this.x=x;
			this.y=y;
		}
		
	}
}


内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值