微软实习笔试题
2016-08-04 16:40
218 查看
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.
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 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.
样例输入
样例输出
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] ='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] =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(""); } } }
相关文章推荐
- 微软2012暑期实习笔试题(附答案)
- 微软实习笔试的几道程序题
- 2012微软暑期实习笔试
- 微软2012暑期实习笔试题(附答案)
- 微软2013暑期实习笔试题(全)有兴趣可以看看哈
- 2013微软暑期实习笔试题及答案
- 2013微软暑期实习笔试错题、疑题整理
- 2012微软笔试题(暑期实习招聘)
- 微软2013年暑期实习招聘笔试回忆[已补充完整]
- 微软暑期实习笔试题 合并两个有序单链表
- 微软2013暑期实习笔试回忆
- 微软2013年实习招聘笔试“22221111”题目详解
- 2011微软暑期实习笔试海选题目(欢迎讨论)
- hihocoder 1289 2017年微软实习笔试题
- 2012微软实习笔试题及参考答案
- 微软2013暑期实习笔试题目第5题分析
- 2012微软笔试题(暑期实习招聘)
- 微软2013暑期实习笔试回忆
- 微软暑期实习笔试题解析
- 微软2013暑期实习笔试回忆