careercup-中等难题 17.2
17.2 设计一个算法,判断玩家是否赢了井字游戏。
解法:
假设这个检查某人是否赢得了井字游戏的函数为HasWon,那么我们第一步要向面试官确认, 这个函数是只调用一次,还是要多次频繁调用。如果是多次调用, 我们可以通过预处理来得到一个非常快速的版本。
方法一:如果HasWon函数需要被频繁调用
对于井字游戏,每个格子可以是空,我的棋子和对方的棋子3种可能,总共有39 = 19683 种可能状态。我们可以把每一种状态转换成一个整数, 预处理时把这个状态下是否有人赢得了井字游戏保存起来,每次检索时就只需要O(1)时间。 比如每个格子上的3种状态为0(空),1(我方棋子),2(对方棋子),棋盘从左到右, 从上到下依次记为0到8,那么任何一个状态都可以写成一个3进制的数,再转成10进制即可。 比如,下面的状态:
可以写成:
=*^ + *^ +...+ *^ + *^
这时,需要一个19683 大的数组来存放每一个计算出的整数表示哪一方赢了,或者出现平局。
如果只需要返回是否有人赢,而不需要知道是我方还是对方。 那可以用一个二进制位来表示是否有人赢。比如上面的状态,1赢, 就可以把那个状态转换成的数对应的位置1。如果需要知道是谁赢, 可以用一个char数组来保存预处理结果。
方法二:如果HasWon函数只被调用一次或很少次
如果HasWon函数只被调用一次或很少次,那我们就没必要去预存结果了, 直接判断一下就好。只为一次调用去做预处理是不值得的。
代码如下,判断n*n的棋盘是否有人赢,即同一棋子连成一线:
从上到下,从左到右,两条对角线。
C++实现代码:
#include<vector>
#include<iostream>
using namespace std; int convertBoardToInt(vector<vector<char> > &board)
{
int factor=;
int sum=;
int i,j;
int v;
for(i=; i<; i++)
for(j=; j<; j++)
{
v=;
if(board[i][j]=='X')
v=;
if(board[i][j]=='O')
v=;
sum+=v*factor;
factor*=;
}
return sum;
} char hasWon(vector<vector<char> > &board)
{
int i,j;
int n=board.size();
//检查每一行
for(i=; i<n; i++)
{
for(j=; j<n-; j++)
{
if(board[i][j]!=board[i][j+])
break;
}
if(j==n-)
return board[i][j];
}
//检查每一列
for(j=; j<n; j++)
{
for(i=; i<n-; i++)
{
if(board[i][j]!=board[i+][j])
break;
}
if(i==n-)
return board[i][j];
}
//检查主对角线 for(j=; j<n-; j++)
{
if(board[j][j]!=board[j+][j+])
break;
}
if(j==n-)
return board[j][j];
for(i=; i<n-; i++)
if(board[i][n-i-]!=board[i+][n-i-])
break;
if(i==n-)
return board[i][];
return board.empty();
} int main()
{
vector<vector<char> > vec={{'X','X','O'},{'X','X','O'},{'X','O','O'}};
cout<<hasWon(vec)<<endl;
}
careercup-中等难题 17.2的更多相关文章
- 面试题目——《CC150》中等难题
面试题17.1:编写一个函数,不用临时变量,直接交换两个数. 思路:使用差值或者异或 package cc150.middle; public class Exchange { public stat ...
- 《程序员面试金典(第5版)》【PDF】下载
<程序员面试金典(第5版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382252 内容简介 本书作者Gayle Laakma ...
- Service Function Chaining Resource Allocation: A Survey
摘要: 服务功能链(SFC)是未来Internet的一项关键技术. 它旨在克服当前部署模型的僵化和静态限制. 该技术的应用依赖于可以将SFC最佳映射到衬底网络的算法. 这类算法称为"服务功能 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- [CareerCup] 17.14 Unconcatenate Words 断词
17.14 Oh, no! You have just completed a lengthy document when you have an unfortunate Find/Replace m ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- [CareerCup] 17.12 Sum to Specific Value 和为特定数
17.12 Design an algorithm to find all pairs of integers within an array which sum to a specified val ...
- [CareerCup] 17.11 Rand7 and Rand5 随机生成数字
17.11 Implement a method rand7() given rand5(). That is, given a method that generates a random numb ...
- [CareerCup] 17.10 Encode XML 编码XML
17.10 Since XML is very verbose, you are given a way of encoding it where each tag gets mapped to a ...
随机推荐
- LeetCode Same Tree (判断相同树)
题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. /** * Definition for a binary t ...
- 【转】Android学习基础自定义Checkbox组件
原文网址:http://forum.maiziedu.com/thread-515-1-1.html heckbox组件是一种可同时选中多项的基础控件,即复选框,在android学习中,Checkbo ...
- mysql 交叉表
交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义.http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4c ...
- HDU 3642 Get The Treasury 线段树+分层扫描线
http://www.acmerblog.com/hdu-3642-get-the-treasury-6603.html 学习:三维就是把竖坐标离散化分层,每一层进行线段树二维面积并就好了
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之_get_select_list_options(self, select_list_or_locator)
def _get_select_list_options(self, select_list_or_locator): if isinstance(select_list_or_locator, Se ...
- C#通过DllImport引入dll中的C++非托管类(转)
http://blog.sina.com.cn/s/blog_70a144580100tmj8.html
- 宿主进程 vshost.exe
Hosting Process (vshost.exe) 宿主进程是VS的一个特性.可以提高调试的性能,可以进行部分信任调试(partial trust debugging),可以进行设计时表达式计算 ...
- leetcode@ [318] Maximum Product of Word Lengths (Bit Manipulations)
https://leetcode.com/problems/maximum-product-of-word-lengths/ Given a string array words, find the ...
- HDU-4716 A Computer Graphics Problem 水题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 直接搞.. //STATUS:C++_AC_0MS_288KB #include <fun ...
- Camtasia Studio的安装步骤
总的来说: 1.安装 2.安装之后,进行汉化. 破解方法: 1):输入注册码安装 用户名:6-Y 注册码:GCABC-CPCCE-BPMMB-XAJXP-S8F6R 2):软件汉化 安装完成后使用汉化 ...