找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1189|回复: 5
收起左侧

[Facebook] 2017新鲜面经

[复制链接]

1088

主题

138

精华

3189

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3189
发表于 2-1-2017 10:33 PM | 显示全部楼层 |阅读模式

亲!马上注册或者登录会查看更多内容!

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 Sophia 于 2-1-2017 10:36 PM 编辑 ! N4 M! F4 R  H; G  T, F

; W! U" S/ {2 J& G$ G) ZRandom generate a NxN matrix with only four types of  element: 1,2,3,4. 5 T' d7 p" O7 ]! k
However, no column or row can have same type of element appears 3 times or above continuously (three same type of elements are connected)
$ M, x' q* o9 g$ Q
) k  B' n' r9 e( T  U1 L* pex:% u- m* N6 @- W& e" V
9 t6 H0 \5 x4 W8 b/ N+ y! N0 d
valid:
% p8 A: P! ?9 w4 k" K1 2 1 1! I, i9 y* P6 E0 t* F
3 1 4 2# q! o+ C) o, ?
1 2 4 2: V; ]4 ]0 Z& S
3 1 2 3' B# }6 ^8 q. S* r+ I: Q
( x4 o$ W: v  G" i- `/ t
invalid because the first column has element 1 appears three times and all 1s are connected to each other :
3 N3 F% f! K3 n, T2 G& D' X
, W! J" t4 q2 E, R# a1 2 1 3( _5 Y* ?7 ~! r4 p
1 3 4 2
. r! }' L1 F# B% F9 }1 2 4 4
. u; h! D' j( [1 O2 3 2 2

1206

主题

184

精华

3720

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3720
发表于 2-1-2017 10:33 PM | 显示全部楼层

I post my solution below:

import java.util.Random;1 F1 T$ `3 N9 w2 [. q& {
7 V9 \/ R, [4 {9 E5 b. X: N( q
public class candyCrush {
' X/ H5 M/ |" U3 K/ k& u: w% b7 l: k6 t4 Y5 t. m8 Z
	public static void main(String[] args) {& O% B# ]3 t3 x/ g
		// TODO Auto-generated method stub
2 |6 F' ^: p7 H" ^' Q! ]		int[][] res = generateRandon(6);
/ N) w- x! h* \1 X$ T2 P3 Z2 g+ P3 K		for (int i = 0; i < res.length; i++) {
% v+ p  a# x( M0 b( D			for (int j = 0; j < res[0].length; j++) {7 S7 u3 ~) W3 g+ o. |
				System.out.print(res[i][j]);7 H1 ~5 z. u2 R, f' `
			}% t1 O7 G7 {' V8 j+ h* @% S, y5 c
			System.out.println();; V' Z6 `' n% D
		}
) f- Z' d! T$ k
: M+ f$ b# Y. ^	}
" g4 D+ S  ~3 ]; I# W" R4 ^/ G
  M% P7 y: N& L0 `  o" Q	public static int[][] generateRandon(int size) {( L1 T3 \) |+ K8 `( g6 Z, o0 F
		Random r = new Random();6 f  c" S) z+ X/ _2 p7 [
		int dp[][] = new int[size][size];4 G& O: u4 ~# T5 A
		for (int x = 0; x < size; x++) {
5 Y8 Z* P, y% j, L			for (int y = 0; y < size; y++) {! V( J/ u5 p+ n# C
				int rand = r.nextInt(4) + 1; # d! _  u7 u1 ]1 h, d1 z
				if (x > 1 && y > 1) {' a) O5 q, V& r( S1 Z3 u/ t. T
					int a = dp[x - 1][y];
" O3 \" y9 N3 d( w2 B					int b = dp[x][y - 1];- C! b5 J7 Y, t  r2 Q- H
					int c = dp[x - 2][y];; p$ `2 `  Y' x5 h+ U
					int d = dp[x][y - 2];& ~- X3 C; [% Y+ E6 G) u
					int newRand = rand;* p3 `9 K* M( n' T1 W
					if (rand == a && rand == c && b!=d) {4 ~1 b+ S1 u% G- \( J& N
						newRand = a;
6 Q+ |: @2 V/ Q5 z. R" c* S, r						while (newRand == a) {& S7 Q7 S  Q" ^- L' d  i
							newRand = r.nextInt(4) + 1;
; ?8 m) Y! A. ^7 w						}
( ~% Q& w" f# L! g" S					}else if (rand == b && rand == d && a!=c) {
& g7 T/ A' d: U# }						newRand = b;) l; R" u% n1 u: A; \- T
						while (newRand == b) {: w( q. V1 i! [, `+ Q* T: W: l
							newRand = r.nextInt(4) + 1;
1 c/ v/ C0 Q! E  |4 E' {& G						}
( c- k& `# {1 d. `' z5 ]4 L9 N					}else if(a==c && b==d){
/ S- l! U4 E( w' M) i  `						; D  Q$ |3 B# ?
						while (newRand == a||newRand==b) {
6 j; \- ~$ H1 [0 X( n: E6 f% s							newRand = r.nextInt(4) + 1;
: y5 E  v8 a$ m3 A% j* D2 G# Q						}- t( h% D: r1 S! r( E+ @& E/ T/ C
					}! y# q; e5 \+ X% X. H0 m) v3 I
					
1 a* ?4 M/ c3 f+ J" x# ?					dp[x][y] = newRand;4 {% J; Z6 m$ x
1 F5 a2 D. F5 m+ G
				} else if (x <= 1 && y > 1) {3 Y) u  v; o/ |5 @7 D9 v
					int a = dp[x][y - 1];
2 B* _  M$ G0 Y  S					int b = dp[x][y - 2];
& O& r! V& u5 T					int newRand = rand;4 j/ |% b4 |+ |- D- r
					if (rand == a && rand == b) {
8 F" y. c/ r2 j( q- r7 e						newRand = a;
; S/ @/ v3 K9 C7 p7 c) S: g						while (newRand == a) {
6 B, A; a: y7 @8 W  m							newRand = r.nextInt(4) + 1;) C( }2 v; a5 C2 f
						}5 @* X: a1 L) O" @  ~: ~; q7 ]
					}
# d3 {, |5 T; ~* M5 a1 W; D					dp[x][y] = newRand;' X! A: p. y- l8 o
				} else if (y <= 1 && x > 1) {
$ o! r  E# k# i" h2 @: @					int a = dp[x - 1][y];: ]9 P8 k) z2 D6 i( Z# E
					int b = dp[x - 2][y];0 }5 {( m$ c2 ?( T& h/ F
					int newRand = rand;* A1 A" S) h. F
					if (rand == a && rand == b) {5 _* x# x# c+ m4 T* N# M
						newRand = a;& ]4 B4 M6 {9 `! J! _0 d. S' M
						while (newRand == a) {
; m, ]3 n0 D: |5 ]9 B							newRand = r.nextInt(4) + 1;- s% L; M1 t% J9 V* a
						}
  i3 N# D. m% |					}
; |; F1 e; s5 S. _2 ?$ n					dp[x][y] = newRand;
2 b( V+ T+ }( _+ M8 R% R				}else{
& I/ [+ [1 z! N+ R/ u/ ^2 p- t					dp[x][y] = rand;4 T) L% s; n/ X8 F! B4 p
				}
6 B+ U- V. B2 _# S			}1 J/ p1 u/ n: o. Q, k- E9 G& d+ h
		}
& F$ Y4 O1 M# z; Z$ o		return dp;
5 f1 X8 ?& C$ h' {9 e+ v( W	}
5 \+ B* C7 ]5 I% n9 w4 j( K3 ~! x; I! o0 @
}

回复 支持 反对

使用道具 举报

1117

主题

164

精华

3414

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3414
发表于 2-1-2017 10:33 PM | 显示全部楼层

your solution doesn't allow the case that two elements of same type are next to each other in a row or column, but actually it is allowed. Only three or above connected elements in the same row or column are prohibited.

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表