题目描述:Alice and Bob

One day, Alice asks Bob to play a game called “K-in-a-row”. There is a game board whose size is N*M. Alice plays first, and they alternate in placing a piece of their color on an empty intersection. The winner is the first player to get an unbroken row of K stones horizontally, vertically, or diagonally. Now given the last situation of the game board, I would like to know who win or just a draw?

输入

The first line of input is the number of test cases T.

For each test case. The first line contains three integers N(3<= N <=15), M(3<=M<=15) and K(3<=K<=6).The next N line, each line contains M pieces, ‘A’ means Alice’s place and ‘B’ means Bob’s place while ‘O’ means empty. It is promised that at most one player wins.

输出

For each test case output the answer on a single line, if Alice wins then print “Alice Win!”, if Bob wins then print ”Bob Win!”, if no one wins, then print ”No Win!”.

样例输入

2
6 6 6
AOOOOO
BABBOO
OOAOBO
OOOAOO
OOBOAO
OOOOOA
5 5 3
AOBOA
BABAO
OOBOO
OOOOO
OOOOO

样例输出

Alice Win!
Bob Win! 思路:就是个五子棋,DFS即可。但是我的代码没有A,找不到问题。所以附上我的代码和正确代码。 我的代码:
// Alice and Bob_K_win.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h" //备注:没有AC! #include <iostream>
#include <cstring>
using namespace std; const int MAX = ;
int t, n, m, k,ans, vis[MAX][MAX],dir[][] = { , , -, , , , , -, , , -, -, , -, -, };
char map[MAX][MAX]; void DFS(int x, int y,int a,int b)
{
//cout << "x:" << x << "\ty:" << y << "\ta:" << a << "\tb:" << b << endl; if (ans != ) return; if (a == k || b == k)
{
if (a == k) ans = ;
else ans = ;
return;
} for (int i = ; i < ; i++)
{
int nx = x + dir[i][];
int ny = y + dir[i][];
if (nx >= && nx < n && ny >= && ny < m && !vis[nx][ny] && map[nx][ny] != 'O')
{
//cout << "nx:" << nx << "\tny:" << ny << "\tmap[nx][ny]:" << map[nx][ny] << endl;
vis[nx][ny] = ;
if (map[nx][ny] == 'A') DFS(nx, ny, a + , b);
else DFS(nx, ny, a, b + );
}
} } int main()
{
cin >> t;
while (t--)
{
memset(vis, , sizeof(vis));
memset(map, '\0', sizeof(map));
ans = ; cin >> n >> m >> k;
for (int i = ; i < n; i++)
cin >> map[i]; for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (map[i][j] != 'O' && !vis[i][j])
{
vis[i][j] = ;
if (map[i][j] == 'A') DFS(i, j, , );
else if (map[i][j] == 'B') DFS(i, j, , );
} }
} if (ans == )
cout << "Alice Win!" << endl;
else if (ans == )
cout << "Bob Win!" << endl;
else
cout << "No Win!" << endl; } }

正确的代码:

#include<cstdio>
#include<iostream>
using namespace std;
int n,m,k,flag;
char map[][];
void dfs(int x,int y,int num,int dis,char e)
{
if(num>k)
{
flag=;
if(e=='A')
cout<<"Alice Win!"<<endl;
else
cout<<"Bob Win!"<<endl;
return ;
}
if(map[x][y]==e && x<n && x>= && y<m && y>=)
{
switch(dis)
{
case : dfs(x,y+,num+,dis,e);break;
case : dfs(x,y-,num+,dis,e);break;
case : dfs(x+,y,num+,dis,e);break;
case : dfs(x-,y,num+,dis,e);break;
case : dfs(x+,y+,num+,dis,e);break;
case : dfs(x+,y-,num+,dis,e);break;
case : dfs(x-,y-,num+,dis,e);break;
case : dfs(x-,y+,num+,dis,e);break;
}
}
}
void solve()
{
flag=;
for(int i=;i<n;i++)//A
{
for(int j=;j<m;j++)
{
if(map[i][j]=='A')
{
for(int k=;k<=;k++)
dfs(i,j,,k,'A');
}
if(flag)
break;
}
if(flag)
break;
}
if(!flag)
{
for(int i=;i<n;i++)//B
{
for(int j=;j<m;j++)
{
if(map[i][j]=='B')
{
for(int k=;k<=;k++)
dfs(i,j,,k,'B');
}
if(flag)
break;
}
if(flag)
break;
}
}
if(!flag)
cout<<"No Win!"<<endl;
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n>>m>>k;
for(int i=;i<n;i++)
{
for(int j=;j<m;j++)
cin>>map[i][j];
}
solve();
}
return ;
}

ACM-Alice and Bob的更多相关文章

  1. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

  2. 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂

    题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...

  3. 2013年山东省第四届ACM大学生程序设计竞赛 Alice and Bob

      Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very ...

  4. XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)

    Problem Description The famous "Alice and Bob" are playing a game again. So now comes the ...

  5. 2013年山东省第四届ACM大学生程序设计竞赛E题:Alice and Bob

    题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...

  6. sdutoj 2608 Alice and Bob

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...

  7. 2014 Super Training #6 A Alice and Bob --SG函数

    原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...

  8. SDUT 2608:Alice and Bob

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...

  9. Alice and Bob(贪心HDU 4268)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  10. HDU4268 Alice and Bob(贪心+multiset)

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...

随机推荐

  1. 吴裕雄--天生自然HADOOP操作实验学习笔记:qq好友推荐算法

    实验目的 初步认识图计算的知识点 复习mapreduce的知识点,复习自定义排序分组的方法 学会设计mapreduce程序解决实际问题 实验原理 QQ好友推荐算法是所有推荐算法中思路最简单的,我们利用 ...

  2. 本地虚拟机搭建ES集群

    一.环境说明 1.物理机信息(主要): 内存:8G 系统/主频:Win7(旗舰版)64位/3.70GHZ 2.虚拟机信息: VMware Workstation 14 Pro 下载地址: 链接:htt ...

  3. 格式化JSON插件

    参考:https://www.cnblogs.com/whycxb/p/7126116.html

  4. c++存储区域

    来自:https://www.cnblogs.com/simonote/articles/3146038.html 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储 ...

  5. Jquery设置完颜色后hover不生效的解决办法

    执行完代码后发现写在样式表中的hover效果失效,改了好几遍差点重新写函数,后来发现很简单,是优先级的问题,css()中的内容覆盖了之前的样式 只需要在样式后写!important即可解决! .fil ...

  6. ThinkPHP 3.2 生成静态页面

    1:在根目录下的全局index.php中加下面这行: define('HTML_PATH', './htm');//生成静态页面的文件位置 2:在项目的配置文件config.php中加下面这行: 'H ...

  7. UITree_study

    1.Create canvas 2.Add TreeView 3.Subscribe and unsubscribe events(订阅和取消订阅事件) 4.Data bind items it's ...

  8. Java 实现 POS 打印机无驱打印

    来源:https://www.ibm.com/developerworks/cn/java/j-lo-pos/index.html 行业需求 我们是一家专业做酒店餐饮软件的公司,餐饮软件一个重要的功能 ...

  9. R语言 方差稳定化变换与线性变换 《回归分析与线性统计模型》page96

    > rm(list = ls()) > A=read.csv("data96.csv") > A Y N 1 11 0.0950 2 7 0.1920 3 7 0 ...

  10. 云时代架构阅读笔记十四——我对Hash算法的理解

    Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是 ...