# Samsung机试题分析

3月1日，三星电子研究所公司机试。

## 输入输出

1、Input

0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 1 1 0
0 0 0 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 0 0
……

2、Output

*1 2212110330
……
10个case，每个case的结果都按行输出(*1表示case1）

## 程序

``````public class ExamSamsun {

public static void main(String[] args) {
// TODO Auto-generated method stub
int[][] A={{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,0,0,1,1,0},{0,0,0,0,1,0,0,0,0,0,0},{0,1,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,1,1,0,0,0,0},{0,0,1,1,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,1,0,0},
{0,0,0,0,0,0,0,0,1,0,0},{0,0,0,0,0,0,0,0,0,0,0}};

int[] height=new int[11];  //测试例结果应为1 1 2 2 1 2 1 1 0 3 0 0
for(int i=10;i>=1;i--) {
for(int j=1;j<=10;j++) {
if(A[i][j]==1) {
//特殊情况
if(j==1&&A[i][j+1]==1) {
if(height[j+1]>height[j])
height[j]=height[j+1]+1;
else
height[j]++;
continue; //当前列已迭代完，执行下列
}
if(j==10&&A[i][j]==1) {
if(height[j-1]-1>height[j])
height[j]=height[j-1];
else
height[j]++;
continue;
}
//一般情形
if(j>=2&&j<=9) {
if(A[i][j-1]==1) {
if(height[j-1]-1>height[j])
height[j]=height[j-1];
else
height[j]++;
continue;
}

if(A[i][j+1]==1) {
if(height[j+1]>height[j])
height[j]=height[j+1]+1;
else
height[j]++;
continue;
}
height[j]++;
}

}

}//inner
}//outer

//output
for(int i=1;i<=10;i++)
System.out.print(height[i]+" ");
}

}``````