找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 1495|回复: 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 编辑
7 X7 |8 E, n" {5 J3 j) o8 ?3 ?" m, `  ^( L% v; w" {; Q
Random generate a NxN matrix with only four types of  element: 1,2,3,4.
2 b$ I/ E& O: k! O. t8 tHowever, no column or row can have same type of element appears 3 times or above continuously (three same type of elements are connected)# s3 u9 q$ A4 v0 O, b

0 z/ S) r( X* Tex:8 g9 Y7 E- v7 T9 Z" i# T

# X5 u7 b' @7 X7 w# Y' O% Nvalid:3 n7 e1 m7 p5 Y$ g. k9 Q2 Z
1 2 1 1
4 v" [% d. o( }: H) B# v$ n- e3 1 4 2' v2 b3 A5 D: O" B- N# V0 e, x
1 2 4 25 o4 F2 c& v5 k% y. G5 N( i9 H
3 1 2 3
& ~! p: T' K" ~% g' v+ ~6 x8 d# z% z" I" S; n
invalid because the first column has element 1 appears three times and all 1s are connected to each other :
6 n! l* c8 j2 r0 L( i* P. L( F+ i  |/ @3 T. x5 N6 e
1 2 1 3; D& V- U7 |1 f" @. H  x4 [
1 3 4 2
! l6 r# t: I% S% r4 p1 2 4 4
7 E- W3 f' m. S% `2 s& 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;" H, l5 G0 R+ D) R, \

# {+ H- w+ r8 ~$ ]public class candyCrush {1 ]* L& o9 d3 J) ?
. g; a2 H+ I$ B; H. a- n3 I" p
	public static void main(String[] args) {( E( h0 l# M; }2 F* S9 C0 j
		// TODO Auto-generated method stub
  h4 A- Y4 b4 B9 A& h( i		int[][] res = generateRandon(6);
1 {/ f+ B2 Y% l2 f  Y		for (int i = 0; i < res.length; i++) {
$ [9 s( h/ r  R! S# I			for (int j = 0; j < res[0].length; j++) {. I8 {$ Z( s) O
				System.out.print(res[i][j]);! L% E) y% [- `
			}! N, h/ ?7 Z* ]4 l
			System.out.println();  i& Y5 q: J( M, s
		}/ S1 W% E: K5 P" z
" v" r4 H. b, w' \% L& r% X. [
	}
8 v& [# h6 I! h$ N& D7 f
, W9 r. G7 K$ H) u	public static int[][] generateRandon(int size) {
' f. [8 t. h$ i2 w$ i		Random r = new Random();' X& x7 h2 V1 D4 u; c9 C+ D
		int dp[][] = new int[size][size];! v. Q5 b" D3 v9 q  R  e
		for (int x = 0; x < size; x++) {
" v2 l8 _* n9 R% p			for (int y = 0; y < size; y++) {4 w  \& B0 T1 L' G% N4 T
				int rand = r.nextInt(4) + 1; 
. ]3 g" {+ X7 b% S. p9 J: G  q				if (x > 1 && y > 1) {# W& q4 y! ?8 y# Q) |. ?
					int a = dp[x - 1][y];
# H. R/ D. t, [3 f: e					int b = dp[x][y - 1];
- C4 U  S- q- n					int c = dp[x - 2][y];
' k7 m4 v4 w' o( K					int d = dp[x][y - 2];: d8 E1 h% I: [
					int newRand = rand;
) m6 d. G& Y  h( w: e1 m					if (rand == a && rand == c && b!=d) {' ?: Z2 k, T! \4 u# k/ y( B2 {
						newRand = a;
* d. D! F  l' S4 D9 {						while (newRand == a) {8 d8 A3 c# N% ~+ e1 ^
							newRand = r.nextInt(4) + 1;7 g3 V& k) Y7 |7 G3 g
						}
1 h! k0 \) K0 t' g% w9 J# V					}else if (rand == b && rand == d && a!=c) {
0 C2 M/ N3 K, Y2 V, v						newRand = b;* m/ I! M  h/ N% M
						while (newRand == b) {
0 S4 O( }+ D# q2 o1 b' z							newRand = r.nextInt(4) + 1;/ e2 b2 L: t0 @5 y& C1 J
						}
& R- E: u  e/ h					}else if(a==c && b==d){
+ u. }, M( S$ a9 Q% ?						
. ^% P. l8 J$ i! Y& K1 Q						while (newRand == a||newRand==b) {8 r5 |( e# l+ ?5 i+ \% v, c& [, l' b
							newRand = r.nextInt(4) + 1;+ o3 D  u- [3 n3 N9 a5 u6 \
						}
& B0 _7 ~2 x* Q- g$ {					}' Q+ a0 k0 s! r' r5 @
					( i0 N0 ~0 ]- U: e: [& _6 y& F
					dp[x][y] = newRand;& @: v; B* c7 Z1 s( l2 y1 O  f! K
$ E4 E- \$ x/ l% F+ f- L& G
				} else if (x <= 1 && y > 1) {
7 ?* X9 m  h* h  n1 n					int a = dp[x][y - 1];/ R$ {9 u2 n0 Y* R. Y
					int b = dp[x][y - 2];
) c# ^) {: @' U# A					int newRand = rand;1 U: p4 G0 D3 b
					if (rand == a && rand == b) {( _" L- v4 @0 u9 V* y9 B- ^
						newRand = a;2 t' X# i$ Y* [( l
						while (newRand == a) {: n1 N; n$ y* B8 j9 Y2 n* ~, o( |
							newRand = r.nextInt(4) + 1;) X5 o/ B; g, U( G: Z3 }
						}
% _  k/ t6 j2 _8 Y* L, O) M					}
3 _4 A; W2 d  C1 @3 k* G- c					dp[x][y] = newRand;
  V7 c5 E0 V4 j7 }$ D5 s				} else if (y <= 1 && x > 1) {
2 p8 p: z  X2 g7 I) j: V" u					int a = dp[x - 1][y];, J! f" G3 e" J7 Q+ W7 S, ^, ^1 Z' Z
					int b = dp[x - 2][y];
1 G' k, _( O+ B( ?& J/ }4 v, i					int newRand = rand;1 M9 G3 X0 a: b$ I% r$ S4 z; V/ ]
					if (rand == a && rand == b) {1 M4 J: i1 g+ \, B3 M+ Y- n  B, @
						newRand = a;
8 _/ S$ Q' P7 V# z						while (newRand == a) {* s. ^  E! F# [7 I5 q
							newRand = r.nextInt(4) + 1;
% w0 a4 Q* T" P9 M						}
" P& f1 v4 z! q/ L) O; r					}5 L) M% B  `6 K3 a& F
					dp[x][y] = newRand;
! u6 h! f7 I  v6 e9 ~' R				}else{
  b" b" T" w- l4 W					dp[x][y] = rand;
) y/ T% Z/ I9 T. y, ^				}/ S! @3 f2 z# n9 \  z" ?/ L
			}- ?+ ?) c" ]* D5 b
		}/ U9 s$ Q. x' T$ T, V0 B
		return dp;
: \& h' O$ Y% A, {8 Y	}
) p: C# U3 }; @
; w9 e: t3 {9 F$ x1 d+ [5 q" @}

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.

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

本版积分规则

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