题目描述: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. C# Connection:连接数据库---转载

    C# 语言中 Connection 类是 ADO.NET 组件连接数据库时第一个要使用的类,也是通过编程访问数据库的第一步. 接下来我们来了解一下 Connection 类中的常用属性和方法,以及如何 ...

  2. EASYUI TREE得到当前节点数据的GETDATA方法

    function show() { var node = $('#tt-c71').tree('getSelected'); var data = $('#tt-c71').tree('getData ...

  3. python设置编码

    import sys sys.getdefaultencoding() #看到默认编码是'ascii' #通常需要的是使用utf8编码,需要这样做: reload(sys) sys.setdefaul ...

  4. Metasploit学习笔记——移动环境渗透测试

    书364页配置假冒AP步骤,因为没有无线网卡,先跳过这个实验.

  5. 前端面试题CSS一(题目来源网络)

    一.什么是html5语义化? 使用合理,正确的html标签格式化文档. 二.CSS样式优先级? 就近原则,行内>内联>外联 三 什么是盒模型? 主要分为两种,w3c标准盒模型,IE标准模型 ...

  6. java核心-多线程(6)-线程池-ThreadPoolExecutor

    1.java多线程编程少不了使用线程池,线程池相关的工具类所在jdk包,java.util.concurrent 2.使用示例 demo1 public class ThreadPoolDemo { ...

  7. Redis的下载与安装

    1). 从官网上下载Redis的压缩包   2). 将压缩包解压到 某个指定的文件目录中         tar -xzvf redis-4.0.9.tar.gz  /xx/xx/xx_dir   3 ...

  8. java多线程(待完善)

    1.小型系统 // 线程完成的任务(Runnable对象)和线程对象(Thread)之间紧密相连 class A implements Runnable{ public void run(){ // ...

  9. 实践 Network Policy 【转】

    为了演示 Network Policy,我们先部署一个 httpd 应用,其配置文件 httpd.yaml 为: httpd 有三个副本,通过 NodePort 类型的 Service 对外提供服务. ...

  10. mysql性能优化及 Comparison method violates its general contract

    项目上嵌套结果集查询,查询的列表再根据每个id进行查询计算,嵌套的sql如下: SELECT SUM(IFNULL(t.out_rate,0)) totalOutRate, SUM(IF(IFNULL ...