由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - simple question
相关主题
java 接口中的方法 疑惑又问几个c语言编程的题目
讨论一道面试题问个google面试题的最佳解法
amazon电面跪了被默据了,发amazon面经
设计card deck问题,还有shuffle function,大家要搞清楚呀Can a 10-year-Java guy answer these 2 questions promptly?
求介绍设计parking lot的文档讨论一个OO问题
Amazon 两轮电话面经 及 design问题请教用Java面试的大牛们
Amamon onsite 面经问几个最近很头痛的A家的题
C++ Q54: enum (C12)这题到底什么意思?
相关话题的讨论汇总
话题: public话题: piece话题: int话题: shape
进入JobHunting版参与讨论
1 (共1页)
b******g
发帖数: 1721
1
You are given a game board that contains a four by four (4x4) array of
pieces. The pieces can be one of six shapes and can be one of six colors
If the board contains 4 pieces of the same shape and same color, the board
contains a winning pattern.
public enum Color
{
Red,
Blue,
Green,
Yellow,
Black,
Purple
}
public enum Shape
{
Square,
Triangle,
Circle,
Star,
Pentagon,
Octagon
}
public class Piece
{
public Color color;
public Shape shape;
public bool Equals(Piece compareTo) {};
}
public class Board
{
public Piece[,] position = new Piece[4,4];
public void Board(){ /*completely builds the board*/};
public void MakeRandomBoard(){};
public piece GetPiece(int x) {};
public bool IsWinner() {};
}
Write the code to detect when a winning pattern is present in a board.
---_---------
my solution is to use hashtable to record pieces and counts, if anyone is 4,
return true. for hashtable, i did not give a hash fuction. any idea?
we can also use 6*6 array to record counts.
r*c
发帖数: 167
2
using System;
using System.Collections.Generic;
namespace WinningGame
{
class Program
{
static void Main(string[] args)
{
int nCount = 0;
int nTotalGames = 1000;
for (int i = 0; i < nTotalGames; i++)
{
Board bd = new Board();
//bd.PrintBoard();
bool bResult = bd.IsWinner();
if (bResult)
{
nCount++;
bd.PrintBoard();
}
Console.WriteLine(string.Format("Game is {0}", bResult ? "
won" : "lost"));
}
Console.WriteLine(string.Format("{0} Games out of {1} are
winning ones", nCount, nTotalGames));
return;
}
}

public enum Color{
UNKNOWN = -1, Red = 0, Blue, Green, Yellow, Black, Purple
}

public enum Shape{
UNKNOWN = -1, Square = 0, Triangle, Circle, Star, Pentagon, Octagon
}

public class Piece
{
public Color color;
public Shape shape;
public Piece(int c, int s) { color = (Color)c; shape = (Shape)s; }
public bool Equals(Piece rhs) { return this.color == rhs.color &&
this.shape == rhs.shape;}
public override string ToString(){
return string.Format("({0}|{1})", Enum.GetName(typeof(Color),
color), Enum.GetName(typeof(Shape), shape));
}
}
public class PiecePlus
{
public Piece Piece {get; set;}
public int Row {get; set;}
public int Col {get; set;}
public PiecePlus(int row, int col, Piece p) { Piece = p; Row = row;
Col = col; }
public bool Equals(PiecePlus rhs) { return this.Piece.Equals(rhs.
Piece) && this.Row == rhs.Row && this.Col == rhs.Col; }
public override string ToString(){
return string.Format("pattern: {0}", Piece.ToString());
}
}
public class Board
{
public static readonly int boardLength = 4;
public static readonly int nMatchCountForWin = 4;
public static readonly int nNumOfEleInColorEnum = Enum.GetNames(
typeof(Color)).Length - 1; // excluding UNKNOWN
public static readonly int nNumOfEleInShapeEnum = Enum.GetNames(
typeof(Shape)).Length - 1; // excluding UNKNOWN
public static readonly PiecePlus DefaultPiecePlus = new PiecePlus(-1
, -1, new Piece(-1, -1));
public Piece[,] position = new Piece[boardLength, boardLength];
public Board(){
MakeRandomBoard();
}
public void MakeRandomBoard(){
Random rdm = new Random((int)DateTime.Now.Ticks);
for(int i=0; i for (int j = 0; j < boardLength; ++j){
position[i, j] = new Piece(rdm.Next() %
nNumOfEleInColorEnum, rdm.Next() % nNumOfEleInShapeEnum);
}
}
}
public void PrintBoard()
{
for (int i = 0; i < boardLength; ++i){
for (int j = 0; j < boardLength; ++j){
Console.Write(position[i, j].ToString());
if (j <= boardLength - 1)
Console.Write("\t");
}
Console.Write("\n");
}
}
private bool isWinningImpl(int row, int col, Piece p, int
nRequirdMatches, HashSet hs, out PiecePlus ppPattern)
{
PiecePlus temp = new PiecePlus(row, col, p);
if (hs.Contains(temp)){
ppPattern = temp;
return true;
}
int nCount = 0;
for(int j=col; j if(p.Equals(position[row, j]))
nCount++;
}
if (nCount >= nRequirdMatches){
if (!hs.Contains(temp))
hs.Add(temp);
ppPattern = temp;
return true;
}
for (int i = row + 1; i < boardLength; ++i){ //avoid double
counting
if (p.Equals(position[i, col]))
nCount++;
}
if (nCount >= nRequirdMatches){
if (!hs.Contains(temp))
hs.Add(temp);
ppPattern = temp;
return true;
}
if (row < boardLength - 1 && col < boardLength - 1)
return isWinningImpl(row + 1, col + 1, p, nRequirdMatches -
nCount, hs, out ppPattern);
ppPattern = DefaultPiecePlus;
return false;
}
public bool IsWinner()
{
HashSet hs = new HashSet();
PiecePlus ppPattern;
for (int i = 0; i < boardLength; ++i){
for (int j = 0; j < boardLength; ++j){
if (isWinningImpl(0, 0, position[i, j],
nMatchCountForWin, hs, out ppPattern)){
Console.WriteLine(string.Format("winning pattern: {0
}", ppPattern.ToString()));
return true;
}
}
}
return false;
}
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
这题到底什么意思?求介绍设计parking lot的文档
谈谈刚面的一个design题Amazon 两轮电话面经 及 design问题请教
L家电面题目Amamon onsite 面经
一个问题:Track ChangesC++ Q54: enum (C12)
java 接口中的方法 疑惑又问几个c语言编程的题目
讨论一道面试题问个google面试题的最佳解法
amazon电面跪了被默据了,发amazon面经
设计card deck问题,还有shuffle function,大家要搞清楚呀Can a 10-year-Java guy answer these 2 questions promptly?
相关话题的讨论汇总
话题: public话题: piece话题: int话题: shape