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

#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;
		}
		
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值