微软实习笔试题

You work as an intern at a robotics startup. Today is your company's demo day. During the demo your company's robot will be put in a maze and without any information about the maze, it should be able to find a way out.

The maze consists of N * M grids. Each grid is either empty(represented by '.') or blocked by an obstacle(represented by 'b'). The robot will be release at the top left corner and the exit is at the bottom right corner.

Unfortunately some sensors on the robot go crazy just before the demo starts. As a result, the robot can only repeats two operations alternatively: keep moving to the right until it can't and keep moving to the bottom until it can't. At the beginning, the robot keeps moving to the right.

rrrrbb..            
...r....     ====> The robot route with broken sensors is marked by 'r'. 
...rrb..
...bb...

While the FTEs(full-time employees) are busy working on the sensors, you try to save the demo day by rearranging the maze in such a way that even with the broken sensors the robot can reach the exit successfully. You can change a grid from empty to blocked and vice versa. So as not to arouse suspision, you want to change as few grids as possible. What is the mininum number?

输入

Line 1: N, M.

Line 2-N+1: the N * M maze.

 

For 20% of the data, N * M <= 16.

For 50% of the data, 1 <= N, M <= 8.

For 100% of the data, 1<= N, M <= 100.

输出

The minimum number of grids to be changed.

样例输入
4 8
....bb..
........
.....b..
...bb...
样例输出
1

针对题目,采用动态规划,从右下角开始,挨个求出最优子结构,代码如下,为了编程方便先扩展输入和得分,在又变和下边添加新行。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

import javax.swing.plaf.synth.SynthTabbedPaneUI;

public class Max {

	public static void main(String[] args) {
		Scanner scanner=new Scanner(System.in);
		String string=scanner.nextLine();
		String[] tem=string.split(" ");
		int m=Integer.parseInt(tem[0]);
		int n=Integer.parseInt(tem[1]);
		char[][] inputObs=new char[m+1][n+1];
		for(int i=0;i<m;i++) {
			char[] characterArray=scanner.nextLine().toCharArray();
			for(int j=0;j<n;j++){
				inputObs[i][j]=characterArray[j];
			}
		}
		/** 在右边和下边分别开辟一列,全为‘b’ **/
		for(int i=0;i<m+1;i++)inputObs[i][n]='b';
		for(int i=0;i<n+1;i++)inputObs[m][i]='b';
		int[][][] score=new int[2][m+1][n+1];
		score[0][m-1][n-1]=0;
		score[1][m-1][n-1]=0;
		/** 将右边和下边的得分置为最大 */
		for(int dir=0;dir<2;dir++){
			for(int i=0;i<m+1;i++){
				score[dir][i][n]=Integer.MAX_VALUE;
			}
			for(int j=0;j<n+1;j++){
				score[dir][m][j]=Integer.MAX_VALUE;
			}
		}
		for(int j=n-1;j>=0;j--){
			for(int i=m-1;i>=0;i--){
				for(int dir=0;dir<2;dir++){
					/** 右下角的起始点不做运算 */
					if(i==m-1&&j==n-1)continue;
					/** 是否是一个可行的点 */
					boolean notCanGo=score[0][i+1][j]==Integer.MAX_VALUE&&score[1][i][j+1]==Integer.MAX_VALUE;
					if(notCanGo||inputObs[i][j]=='b'){
						score[dir][i][j]=Integer.MAX_VALUE;
						continue;
					}
					/** 分三种情况
					 * 1、下边的得分-右边的得分大于1,值得将路径转到右边、
					 * 2、右边的得分-下边的得分大于1,值得将路径转到下边 
					 * 3、两个得分相等或相差1,则沿着原来的路径走*/
					if(score[0][i+1][j]-score[1][i][j+1]>1){
						score[dir][i][j]=score[1][i][j+1]+(dir==1?0:(inputObs[i+1][j]=='b'?0:1));
					}else if(score[0][i+1][j]-score[1][i][j+1]<-1){
						score[dir][i][j]=score[0][i+1][j]+(dir==0?0:(inputObs[i][j+1]=='b'?0:1));
					}else {
						score[dir][i][j]=dir==0?(score[0][i+1][j]):(score[1][i][j+1]);
					}
				}
			}
		}
		System.out.println(score[1][0][0]);
		/** 向左走需要替换的个数 */
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				System.out.format("%10d"+"  ", score[0][i][j]);
			}
		System.out.println("");
		}
		System.out.println("-------------------------------------------");
		/** 向右的走的需要替换的个数 */
		System.out.println("");
		for(int i=0;i<m;i++){
			for(int j=0;j<n;j++){
				System.out.format("%10d"+"  ", score[1][i][j]);
			}
		System.out.println("");
		}
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值