找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

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

[Facebook] 2017新鲜面经

[复制链接]

1070

主题

136

精华

3119

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

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

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

x
本帖最后由 Sophia 于 2-1-2017 10:36 PM 编辑
, F! f0 w2 P0 e2 N4 G! V) T# [$ j& y
Random generate a NxN matrix with only four types of  element: 1,2,3,4. ( D  Z9 `; B- ?1 Y, t# c
However, no column or row can have same type of element appears 3 times or above continuously (three same type of elements are connected)) a* Y5 P: M7 \" J, l( D  p! q+ T" `

8 T% U" U- A$ hex:
' i( p2 G2 S* r8 C& g( {  f6 J3 f0 P$ Q) W1 M2 y* b
valid:/ O# h) o* d& E
1 2 1 10 e+ |( ~; i7 h, Q/ Q) P
3 1 4 2
7 U# K; |& C" C: O$ U, G1 2 4 2
" y7 D* l" @+ X. u# h5 Q& }) `% o3 1 2 3
' |& `; ]( U3 D# k
, o- i0 n" w5 f9 X9 {1 O* W0 uinvalid because the first column has element 1 appears three times and all 1s are connected to each other :. Z" C  S% _, o* N# i

3 _* f1 n, Q( l+ p0 w- i1 2 1 3
% l' b6 ]$ {7 A! I3 K1 m3 G  p" N1 3 4 2
2 L4 K; ^6 M7 i0 Q3 h# x; U- }8 R1 2 4 4
2 S0 `! _2 i# H( ^2 3 2 2

1198

主题

184

精华

3704

积分

神级会员

Rank: 7Rank: 7Rank: 7

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

I post my solution below:

import java.util.Random;2 v; f0 A9 u3 E3 u3 v; r! [& d
& m- i5 ?$ |3 K7 G
public class candyCrush {
+ r+ _! q$ Q0 `6 @! R& E2 e* e# E# a5 c4 m+ s
	public static void main(String[] args) {
; g# D8 J. f. w		// TODO Auto-generated method stub6 l7 `3 F/ T' U' g9 F+ X7 W
		int[][] res = generateRandon(6);
9 ~" H4 t6 `  J( }. D		for (int i = 0; i < res.length; i++) {
9 ^$ `3 ^1 Q6 G' x7 k+ V			for (int j = 0; j < res[0].length; j++) {
/ ]  n, ]6 G+ y: p( P) L				System.out.print(res[i][j]);" p4 R' z' F* D# R1 r- \
			}
3 f# f- O5 X5 G1 D9 s- R# V; J$ y0 n			System.out.println();$ Y: G6 R9 d5 d6 j
		}/ s5 m/ Z6 |8 [1 f% ?+ w2 p' C
/ u, l4 M+ K7 v% j
	}
% r" z% G, r4 q3 }3 B( w1 V; p: Y: @9 O2 D
	public static int[][] generateRandon(int size) {) C* a" u+ ?- o8 c
		Random r = new Random();# e' }0 h. Q) C" b
		int dp[][] = new int[size][size];  M# q4 i6 H- c& E" G" |! I5 R7 [9 s
		for (int x = 0; x < size; x++) {, U% {6 Q; X4 n
			for (int y = 0; y < size; y++) {
. X* M% a$ s2 [# y& C				int rand = r.nextInt(4) + 1; 
+ Q7 t  I/ Z4 ]7 L+ G, D( `6 p				if (x > 1 && y > 1) {/ \8 d) Z& ]2 q7 }+ g+ H, E) ~1 N
					int a = dp[x - 1][y];4 T- E( \+ Z+ W' e3 H
					int b = dp[x][y - 1];" S* v' V, s) v
					int c = dp[x - 2][y];, l, `7 f3 u! ]' P8 f: D
					int d = dp[x][y - 2];
2 ~- a8 V' {6 B5 I					int newRand = rand;
& U! p6 i$ i* }0 R/ g+ s					if (rand == a && rand == c && b!=d) {
' \+ C  \( _2 t; H5 k0 A						newRand = a;4 ?. h" B& u3 O* Y6 j9 ~
						while (newRand == a) {
% n  ~" L! Q* s$ [  X! Q							newRand = r.nextInt(4) + 1;
0 {5 R3 j, j3 R6 b6 J1 X- c! a$ P$ J						}9 D$ t! ?6 v5 P* _" T
					}else if (rand == b && rand == d && a!=c) {3 D7 L; {, Y# E" W3 }  u+ ]0 D5 e
						newRand = b;. H  `" b4 {4 N5 l
						while (newRand == b) {3 O! `) @+ O/ l  }% K: P
							newRand = r.nextInt(4) + 1;
+ ~$ k7 w. {& S* F2 d2 C! I						}' k$ L: w# S* B, l8 d
					}else if(a==c && b==d){
1 ~) v7 x5 T& _7 x, |  G						% k; T& M' h, G) R+ z4 T0 V) Z
						while (newRand == a||newRand==b) {2 n8 P3 K! E) ^; k; e
							newRand = r.nextInt(4) + 1;  s4 w8 Q  |- f6 \0 B! ]
						}/ W" M2 P+ t( j9 P& ?$ I1 z1 T
					}
& g) T8 F+ l* K$ \					1 h4 q, V, b$ u6 \1 ~$ ?
					dp[x][y] = newRand;4 w: J/ w7 j" q, I  H: y2 m  u1 B" [
. k9 Q8 K0 l- L5 P
				} else if (x <= 1 && y > 1) {
5 q0 Z4 g* |! u, j$ T0 g1 q$ _4 ?8 i, S					int a = dp[x][y - 1];
# v2 k0 K$ U' W) a5 n6 C* p					int b = dp[x][y - 2];. o( H" t' \) n% G" A, [
					int newRand = rand;
, A5 ~- T) k0 U					if (rand == a && rand == b) {
* F3 L5 _4 w* ]4 m4 z						newRand = a;2 o+ ^( G+ p4 J" T6 o
						while (newRand == a) {
/ c% x" G5 O( b/ R+ i% E3 H: i							newRand = r.nextInt(4) + 1;
; j( E4 m3 n! w						}; a7 ~$ _! r2 [. E, r4 w
					}
; ~. d+ q2 N9 ?" Y" i, f$ y					dp[x][y] = newRand;
* E' s( h+ }# d( G$ s% Q				} else if (y <= 1 && x > 1) {, l5 b; _5 M" a+ g0 s7 f7 w. ~) @5 h
					int a = dp[x - 1][y];
$ f  e4 e5 l7 z# }9 R# k$ a					int b = dp[x - 2][y];
0 V' }+ _& j8 |$ p					int newRand = rand;4 Z; I( F6 H( h* K: {% z
					if (rand == a && rand == b) {
: t+ k7 j- X0 n) z! }) [						newRand = a;
1 p* Y8 s7 K2 i2 n						while (newRand == a) {
6 `. a( R# I# G. {6 w% d8 |							newRand = r.nextInt(4) + 1;  B! G+ \5 I: a: p
						}% ~; ^) E( p5 z/ n! X2 u1 a, `
					}0 X: e* p  C1 ^3 c$ I$ c) E
					dp[x][y] = newRand;
$ e( G. @+ x- o0 r( i9 p$ @				}else{, d8 z, Y8 `* g- y  j9 y/ k' m+ ?
					dp[x][y] = rand;! D3 f9 \7 o9 |: x. l1 k0 n
				}
; [) U* ~% T: u) Z, K5 [			}4 F( l, C. j4 Z1 e2 P% X( {! r
		}
, I+ F( `, Z+ a# w, n$ v$ z3 O! u# r		return dp;
3 S' y8 }9 ?( d' v1 ]* F" t' ?/ p	}% i' L2 J: ^  e' S2 {
8 X) b! z5 w3 _$ O7 N
}

回复 支持 反对

使用道具 举报

1109

主题

164

精华

3394

积分

神级会员

Rank: 7Rank: 7Rank: 7

积分
3394
发表于 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.

回复 支持 反对

使用道具 举报

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

本版积分规则

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