ACM-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的更多相关文章
- Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...
- 计蒜客 ACM训练联盟周赛 第一场 Alice和Bob的Nim游戏 矩阵快速幂
题目描述 众所周知,Alice和Bob非常喜欢博弈,而且Alice永远是先手,Bob永远是后手. Alice和Bob面前有3堆石子,Alice和Bob每次轮流拿某堆石子中的若干个石子(不可以是0个), ...
- 2013年山东省第四届ACM大学生程序设计竞赛 Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 题目描述 Alice and Bob like playing games very ...
- 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 ...
- 2013年山东省第四届ACM大学生程序设计竞赛E题:Alice and Bob
题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...
- sdutoj 2608 Alice and Bob
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...
- 2014 Super Training #6 A Alice and Bob --SG函数
原题: ZOJ 3666 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 博弈问题. 题意:给你1~N个位置,N是最 ...
- SDUT 2608:Alice and Bob
Alice and Bob Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...
- Alice and Bob(贪心HDU 4268)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- HDU4268 Alice and Bob(贪心+multiset)
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
随机推荐
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 表格:悬停表格
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Intend之Date的几个功能
封装为一个方法 1.跳转到拨号页面 //跳转到拨号页面的方法 protected void takeCall(String info){ Intent intent=new Intent(); int ...
- c++中的全排列
next_permutation函数 组合数学中经常用到排列,这里介绍一个计算序列全排列的函数:next_permutation(start,end),和prev_permutation(start, ...
- 小程序列表循环出来的list是不同接口赋的值
需求:首页有三个列表,样式形式都是一样的,可以循环展示,但是循环的内容list部分是来自于不同的三个接口. data: { indexList:[{ name: "中考体能突击营" ...
- HihoCoder第四周:Trie图
第四周的题目是前两周的综合,综合在一个是KMP算法的思想,一个是树的这么一个数据结构. 题目 : Trie图 输入 每个输入文件有且仅有一组测试数据. 每个测试数据的第一行为一个整数N,表示河蟹词典的 ...
- visio 2019 激活方法
今日因工作需要使用visio,无奈下载2019版本需要激活,很多功能无法使用,最近在网上发现一个非常简单就是一个本地可执行脚本,本人已亲测完全激活成功,随分享给大家 复制下面代码: @echo off ...
- Windows使用Nexus搭建Maven私服
简介 Maven私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件,有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库,否则,私服请求外部的 ...
- 15.Pythonic与python杂记
switcher ={ :'sunday', :'monday', :'thuesday' } day = day_name=switcher.get(day,'Unknow') print(day_ ...
- postman 请求get post方法的 区别
1.HTTP的五种请求方法:GET, POST ,HEAD,OPTIONS, PUT, DELETE, TRACE 和 CONNECT 方法. GET请求:请求指定的页面信息,并返回实体主体.(通常用 ...
- 关于重定向RedirectAttributes的用法
刚才做项目,遇到了redirectAttributes使用的问题,上网找了找,看到一篇写的很不错的博客,解决我对于RedirectAttributes的困惑,也给大家推荐下. 原文链接:href=&q ...