找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1216|回复: 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 编辑 , ?2 E  g  N5 U7 G. {+ P* {
; s. `6 [0 l. O9 F2 @  |& h  d( }
Random generate a NxN matrix with only four types of  element: 1,2,3,4.
, z( H3 M) \* [; k9 ]  {/ e0 pHowever, no column or row can have same type of element appears 3 times or above continuously (three same type of elements are connected)6 r+ \, }& ?2 u% E/ `

# b# L2 q& n: Gex:8 ^  |2 c( U4 `/ p+ h. ^8 u+ s

2 e) ~1 e5 W$ \5 t2 f( C. P9 {- uvalid:6 n, C  y( C4 z7 [
1 2 1 18 W$ ?  @2 [% Q( ]
3 1 4 21 m; O- [: }4 q& A
1 2 4 2* {0 n$ Y  E7 x3 G$ V* O" \
3 1 2 32 t( u$ Q" E% f. \  H9 X7 W

% F% Y* W# M* b2 ^6 P! Jinvalid because the first column has element 1 appears three times and all 1s are connected to each other :  j! l2 @. t( r, |  t7 x

6 J# D8 J* @* @$ p" x& @1 2 1 3
  o8 m( D% [" [1 3 4 25 `' }8 ^; c6 q) S3 h) t. q" ~
1 2 4 4; ^1 j4 D' I, q; q4 M" g( F  q
2 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;
, u* ^2 x; T; a# g+ P% ]) T0 d7 V. l
public class candyCrush {. z" V) e- d, V" z* K5 W

9 g+ |" w0 M  w$ H& V3 P	public static void main(String[] args) {
' L! ^  k' H4 W+ y		// TODO Auto-generated method stub
. a; N0 X" P: T* V, e: E( c		int[][] res = generateRandon(6);7 _# j" i( L: m
		for (int i = 0; i < res.length; i++) {
. {" _1 J# y: I2 B, U# t			for (int j = 0; j < res[0].length; j++) {" o8 `. T" B) W0 v2 p. k% @
				System.out.print(res[i][j]);
9 F- O8 V( R7 y% D" {			}
& z3 A4 ^" H" I  }+ {5 G! Z			System.out.println();
) A2 G! c4 x! v		}
+ r8 ~1 l& d0 H& t0 Z% K8 p
+ Q0 f# I4 G  B) g  \	}
6 e. o7 a  v# E5 I3 K& M
) ]  T9 d; V) S( _/ d	public static int[][] generateRandon(int size) {$ S9 ^9 g9 w  K8 J, z
		Random r = new Random();+ n1 e3 T! v  ]; E  S
		int dp[][] = new int[size][size];: _9 |( a4 k5 ^1 j. k
		for (int x = 0; x < size; x++) {
) }6 m( Q) u2 m  _+ p# t			for (int y = 0; y < size; y++) {
& a0 }; @: A+ Z) o7 {				int rand = r.nextInt(4) + 1; 
' K# K$ z7 h# f; y, ^& V				if (x > 1 && y > 1) {7 y' Q5 p$ ~, l! r0 k
					int a = dp[x - 1][y];  W& Z% P2 y3 Z. @1 Q
					int b = dp[x][y - 1];% e* D0 y& A3 ?8 r/ ~8 [
					int c = dp[x - 2][y];
$ Y6 G1 x* {5 L3 J  n					int d = dp[x][y - 2];
4 X8 f( g- v: c; I3 X% C					int newRand = rand;
- c0 T9 g' d" X+ [% Q  ?					if (rand == a && rand == c && b!=d) {
7 i3 }4 D# o$ C. E( H3 v, m4 O2 N						newRand = a;/ l: W8 x* j( N% G" J3 J# Y
						while (newRand == a) {2 M6 @4 [- F( ?- u7 G/ B2 W6 K
							newRand = r.nextInt(4) + 1;, D1 P0 y# p4 Y; w! O1 T0 n' Q: U
						}
8 g* S* i8 I! ?6 {) H  m- Q; ]' T" C					}else if (rand == b && rand == d && a!=c) {
+ {# H* V' K& X1 T. I: F						newRand = b;
& J6 T, x' K  s% s7 d" ^3 K8 c						while (newRand == b) {
) D9 M$ r: `: m* \5 T! i. |- S; h							newRand = r.nextInt(4) + 1;' D& s  |2 |+ C+ |8 d: M
						}$ Q: K( I& n4 L/ ?5 q$ t) q
					}else if(a==c && b==d){
* Z0 ^0 a, L9 k- K- ~# A1 ^						0 r; d6 Z" n! j4 a
						while (newRand == a||newRand==b) {6 r! ?0 a" t8 ^: j8 q
							newRand = r.nextInt(4) + 1;9 j/ i9 B' U" F# Z, _1 B2 U
						}
9 d! e( q' k& Q$ P4 u$ u: Q; T  T  n, J5 W					}
0 F, c) w8 b) b5 S# t" x' j					
; L' M" z) L. s3 d8 V					dp[x][y] = newRand;
8 J; P4 g1 ~* q1 N5 V
. ~+ c" q! F. q1 x				} else if (x <= 1 && y > 1) {! s" |1 C2 w2 G4 u! P
					int a = dp[x][y - 1];0 {$ }6 F2 F+ F. ?! Y
					int b = dp[x][y - 2];
6 f5 o+ b7 S! g. D- @5 p; u* s					int newRand = rand;
+ a$ j6 F; q7 b' S					if (rand == a && rand == b) {
7 M  w, r- x; O						newRand = a;
$ a8 T& c7 h0 h3 n) a5 M2 _						while (newRand == a) {
8 c1 u7 [( v  U* a7 ^							newRand = r.nextInt(4) + 1;1 L6 K+ C, g- G' ^
						}
7 @8 k% z1 l6 J8 ?/ p9 f					}' G) h  m9 h2 u* a" o3 }% V& b
					dp[x][y] = newRand;7 p% T* ]5 U2 w" N7 V/ K
				} else if (y <= 1 && x > 1) {
+ R+ D+ R6 x/ I/ S8 W* R					int a = dp[x - 1][y];. z" F( j% r+ }3 G# k
					int b = dp[x - 2][y];
2 T: M2 L, N1 K					int newRand = rand;
. @" Y7 ]& n. `% R. B* u& s					if (rand == a && rand == b) {  V8 L/ @# A# B8 N
						newRand = a;0 ?0 P* h8 g, C0 ]& k3 H
						while (newRand == a) {
7 F8 t" |5 v/ }! d, F							newRand = r.nextInt(4) + 1;' p7 A6 o3 T  J$ Q
						}' v+ X6 E) |0 q. M) w3 C
					}; |3 T5 @7 a" J( W8 v
					dp[x][y] = newRand;
1 v2 m# d9 p% l1 Q, v				}else{
6 i2 f5 Z) a3 A7 R, u1 q; r					dp[x][y] = rand;6 X  N) ^4 m$ v# B" A& _
				}. q# @, \% U4 Y% T3 C  e# a4 `; [
			}  M/ `1 \" n* |# |
		}
. O* f. `- Y3 `6 _, G" i  K		return dp;
- B# I3 D) `5 q/ g# a/ i( L% u	}5 k/ }2 K, D* H$ i9 F! f6 ^
5 K$ O2 H" e$ v" B+ q7 u
}

回复 支持 反对

使用道具 举报

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.

回复 支持 反对

使用道具 举报

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

本版积分规则

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