找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1143|回复: 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 编辑 . `/ x- B% O3 i8 N3 S' D  r; v3 v

6 Y" G3 b0 y5 H" rRandom generate a NxN matrix with only four types of  element: 1,2,3,4.
3 I7 E  t0 X, H2 ~/ |However, no column or row can have same type of element appears 3 times or above continuously (three same type of elements are connected)
- r6 I; [" a3 f( ^0 e2 B* j1 ~* p
ex:3 O9 d2 B  v! ~. X' G" {

/ n/ `9 d# n, C' avalid:4 x# E0 [1 c$ t
1 2 1 1
7 [) `  e$ [; f9 E* y3 1 4 2
4 k- K, A) @: p" o) h8 w7 l1 2 4 26 z+ ?  L, `. b( u. i2 U
3 1 2 3
& W% d1 n7 \/ m: d: b& C  U9 ~4 s" K3 w; ~! Q, g+ ]
invalid because the first column has element 1 appears three times and all 1s are connected to each other :" @; R+ P6 p5 R# J
* b* p; ^: a% {, ^' G9 i. \
1 2 1 3
; Y- O0 ~. A6 Y  F- Q; O' l( X* v/ a1 3 4 2
; l/ b8 |" D% O4 ~# ]1 2 4 4; h# P1 Y7 \5 e$ z% v- o
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;9 p' A8 [( w3 l* s

5 a! l- Z; ?+ `6 T& G- mpublic class candyCrush {* D1 d/ V3 ^1 F4 @4 J  Y

9 ~/ P# p1 N: X; m	public static void main(String[] args) {( P8 b( I9 H3 ~8 w3 J$ W0 u0 N
		// TODO Auto-generated method stub
1 a3 [; k( L) c( c5 O& v! d& }		int[][] res = generateRandon(6);# t4 O4 C, I: p% k+ |* w" H% z
		for (int i = 0; i < res.length; i++) {! c% Z, ^5 d; @: P9 M
			for (int j = 0; j < res[0].length; j++) {. f6 ?! h6 f! R. U
				System.out.print(res[i][j]);
+ L$ b3 ?+ x7 e			}
: S) i9 M" D" y4 ^/ M			System.out.println();
, Q" X2 ~1 m$ g1 y/ O( d		}
" _! }6 f4 m+ q; w2 a; [! X9 j# b& {
9 U5 {, ?+ ?# {	}' b8 ~, `  s! f  D. ?# z; x4 N

5 @; U! S7 v0 J0 k% x/ I	public static int[][] generateRandon(int size) {' D; G- M$ g, A: v6 Z) W
		Random r = new Random();- l9 d( c! {/ ]$ J% }% {' m* I0 h+ S
		int dp[][] = new int[size][size];
" ~. i1 l' H! x		for (int x = 0; x < size; x++) {' X3 t7 \0 G) Y4 j) s
			for (int y = 0; y < size; y++) {
( \8 E' r, U2 {" {9 U. v, }# G( t				int rand = r.nextInt(4) + 1; ) g: `& s. k) X
				if (x > 1 && y > 1) {/ i2 D- u1 H, t4 C; A
					int a = dp[x - 1][y];
9 h6 Z. J5 G6 z# V					int b = dp[x][y - 1];+ e6 U5 p' [/ {: Z: Q4 l
					int c = dp[x - 2][y];
/ w# b2 Z/ ]9 g8 M$ ^. e- Z6 c( \" A					int d = dp[x][y - 2];
1 n: f/ J2 g3 U6 e: [					int newRand = rand;
+ y  Q( |6 ]7 T: i# K" \3 F) _					if (rand == a && rand == c && b!=d) {
$ \* q) [& e) g/ S						newRand = a;
  u* J+ H  a8 `% |# ?  d! @, T						while (newRand == a) {
8 t& t, f1 d: M/ \							newRand = r.nextInt(4) + 1;
2 v% h5 _; V4 J7 c* C. ?# D9 u: g						}! m9 J) }2 H7 s( o  p! F$ w( m
					}else if (rand == b && rand == d && a!=c) {" A; w/ G1 M) M' Q  F2 x/ l
						newRand = b;, B$ O8 V; L+ f0 J% _
						while (newRand == b) {
2 ?7 [  H5 k' u6 v							newRand = r.nextInt(4) + 1;2 C% W- e$ G# v
						}
8 \* x/ a' U% z; i, T1 R					}else if(a==c && b==d){+ ^: K7 k" n( y$ `8 n
						
; {2 f5 ]% H/ T						while (newRand == a||newRand==b) {4 F. r& s" F8 y- V
							newRand = r.nextInt(4) + 1;; n; j6 O6 x) C! s2 k
						}  V* }3 ?5 ?# T1 c7 B8 |% l
					}2 @; I) @) ~$ a+ ]0 D
					
& K- U, P" s; {					dp[x][y] = newRand;
6 c$ m& P) E: L3 W
1 D# Y/ E0 C7 C6 o; M1 d) I6 Z% y/ x8 J				} else if (x <= 1 && y > 1) {
& x: O* l1 ?/ L9 G0 s					int a = dp[x][y - 1];
/ c2 t+ e4 s1 w  h$ K					int b = dp[x][y - 2];( U( S9 F( I, [: R3 g' M
					int newRand = rand;4 o( S4 f  K* H$ s' D: p; Z$ n
					if (rand == a && rand == b) {: X1 g* l7 U7 z% k0 o
						newRand = a;
" o3 H! P9 a8 \4 P						while (newRand == a) {
6 u/ ]: b- A" c* [. }) u  T							newRand = r.nextInt(4) + 1;- m8 R8 V. J. m7 w- a
						}
& R1 @+ Q* S6 N4 O9 u- K0 \# k7 Y* j					}3 ~3 U& a$ m! I5 g# O
					dp[x][y] = newRand;
0 ~* A+ s  y, S9 b9 Q5 Q: T				} else if (y <= 1 && x > 1) {2 C8 B$ K$ P* K4 D* f
					int a = dp[x - 1][y];2 y+ O* {) ]: e3 J( Y
					int b = dp[x - 2][y];& l  U3 v+ K3 E! k
					int newRand = rand;% M9 l# K: a0 D! Z' ]5 D& U4 [  @9 O8 b
					if (rand == a && rand == b) {5 b! H! C4 h+ t% [" [; p
						newRand = a;
' E2 a* \) b5 p# K+ I* u" |( p						while (newRand == a) {1 g; h: @- n, a- _. {
							newRand = r.nextInt(4) + 1;( d7 E8 M9 U& x7 U; W+ n! W! k
						}
% K- \: [0 E. X' J- [0 C: ]" J					}7 a& V2 ?' ~) d) g
					dp[x][y] = newRand;! h/ y1 n, W" S( h" j* f+ y
				}else{
( D; [) [) ?# X					dp[x][y] = rand;
1 I# ]! w/ r4 D. z; c- X. N$ Q1 \				}
+ ^/ H( D9 W1 F; R! w			}
1 H0 ]3 c5 ~0 Y+ a: S, t3 p  D		}( Z+ D4 r8 G! G5 B6 y
		return dp;
2 A: k' s! e+ u7 S	}, J6 L" b( x7 P' N+ @- C

5 n# _! W# L' w. F. y; d& R+ ^& A6 W}

回复 支持 反对

使用道具 举报

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.

回复 支持 反对

使用道具 举报

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

本版积分规则

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