谷歌入场面试题(论坛大佬提供的)
首先是这样,就是我们先给定一个规则如下:
1 0 1 1 1 1
0 0 0 →→→ 1 1 1
1 0 1 1 1 1
就是,我们模拟一个点击模型,当点击位置(1,1)→ 0
则会发生上、下、左和右 的 1 → 0 ,0 → 1
举个稍微简单的例子:
1 0 1 0 1 1 1 1 1 1
0 0 1 0 0 →→→ 1 1 1 1 1
1 0 1 0 1 1 1 1 1 1
点击(1,1)和 (1,3)位置时候,那么所有的地方都是 1 ;
问题
那么,现在就来了,我随机给出一个矩阵,如何确定其能否最终为矩阵为 1
解决方法
枚举法:
简单点说,每一行都变成 1 ,则每一个为 0 的地方需要在其下凡方做一次点击。
import java.util.Random;
public class solve0and1 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
solve_the_problem change = new solve_the_problem();
change.changeit();
for (int j = 0; j < change.problem.length; j++) {
for (int i = 0; i < change.problem[0].length ; i++) {
System.out.print(change.problem[j][i]);
}
System.out.println("");
}
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:" + (endTime - startTime) + "ms");
}
}
class solve_the_problem {
int[][] problem= new int[5][6];
public void changeit() {
for(int i=0,j=problem.length;i<j;i++){
for(int h=0,k=problem[i].length;h<k;h++){
this.problem[i][h]=new Random().nextInt(2);
}
}
//遍历
for(int i=0,j=problem.length;i<j;i++){
System.out.println();
for(int h=0,k=problem[i].length;h<k;h++){
System.out.print(problem[i][h]+"\t");
}
}
for (int j = 0; j < problem.length ; j++) {
for (int i = 0; i < problem[0].length ; i++) {
System.out.println(i+"+"+j);
if(problem[j][i] == 0&&j != problem.length - 1){
System.out.println("发生点击!");
int a = j + 1;
int b = i;
//四个角
if (b == 0 && a == 0) {
checkit(0, 0);
checkit(0, 1);
checkit(1, 0);
}
if (b == problem[0].length - 1 && a == problem.length - 1) {
checkit(problem.length - 1, problem[0].length - 1);
checkit(problem.length - 1, problem[0].length - 2);
checkit(problem.length - 2, problem[0].length - 1);
}
if ( b == 0 && a == problem.length - 1) {
checkit(problem.length - 1, 0);
checkit(problem.length - 2, 0);
checkit(problem.length - 1, 1);
}
if (problem[a][b] == 0 && b == problem[0].length - 1 && a == 0) {
checkit(0, problem[0].length - 1);
checkit(0, problem[0].length - 2);
checkit(1, problem[0].length - 1);
}
//四条边
if (b == 0 && a != 0 && a != problem.length - 1) {
System.out.println("发生点击!1");
checkit(a,b);
checkit(a - 1, 0);
checkit(a, 1);
checkit(a + 1, 0);
}
if (problem[a][b] == 0 && a == 0 && b != 0 && b != problem[0].length - 1) {
System.out.println("发生点击!2");
checkit(a,b);
checkit(0, b-1);
checkit(1, b);
checkit(0, b+1);
}
if (b == problem[0].length - 1 && a != 0 && a != problem.length - 1) {
System.out.println("发生点击!3");
checkit(a,b);
checkit(a - 1, problem[0].length - 1);
checkit(a, problem[0].length - 2);
checkit(a + 1, problem[0].length - 1);
}
if (a == problem.length - 1 && b != 0 && b != problem[0].length - 1) {
System.out.println("发生点击!4");
checkit(a,b);
checkit(problem.length - 1, b - 1);
checkit(problem.length - 2, b);
checkit(problem.length - 1, b + 1);
}
if (b != 0 && a != 0 && b != problem[0].length - 1 && a != problem.length - 1) {
checkit(a,b);
checkit(a - 1, b);
checkit(a, b - 1);
checkit(a + 1, b);
checkit(a, b + 1);
}
}
}
}
}
public void checkit(int j, int i) {
if (this.problem[j][i] == 1) {
this.problem[j][i] = 0;
} else {
this.problem[j][i] = 1;
}
}
}
随机生产矩阵如下:
判断矩阵如下:
代码如上!