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, ...
随机推荐
- 《React后台管理系统实战 :三》header组件:页面排版、天气请求接口及页面调用、时间格式化及使用定时器、退出函数
一.布局及排版 1.布局src/pages/admin/header/index.jsx import React,{Component} from 'react' import './header. ...
- 认识系统服务 (daemons)
daemon(守护进程:后台程序)与服务: 系统为了某些功能必须要提供一些服务 (不论是系统本身还是网络方面),这个服务就称为 service .但是 service 的提供总是需要程序的运作 ...
- c++存储区域
来自:https://www.cnblogs.com/simonote/articles/3146038.html 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储 ...
- ORACLE锁表问题
1.查询锁表的信息 select sess.sid,sess.serial#, lo.oracle_username,lo.os_user_name, ao.object_name,lo.locked ...
- [LuoguP1203][USACO1.1]P1203 Broken Necklace
Solution 这道题数据规模奇小,因此大部分人都使用了暴力搜索的方法,这也是我一开始的想法. 对于 100100%100 的数据,3≤n≤3503≤n≤3503≤n≤350 的确可以如此,但暴力搜 ...
- jquery源码部分分析
1.整体架构和如何辨别浏览器端和node端 自执行函数,判断在什么端,如果在浏览器端就执行factory函数 //(function(){a,b})(a,b) //jq大架构,闭包,自执行函数,传入函 ...
- 「CF741D」Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths
传送门 Luogu 解题思路 考虑把22个字符状压下来,易知合法情况就是状态中之多有一个1,这个可以暴力一点判断23次. 然后后就是 dsu on the tree 了. 细节注意事项 咕咕咕 参考代 ...
- JuJu团队12月29号工作汇报
JuJu团队12月29号工作汇报 JuJu Scrum 团队成员 今日工作 剩余任务 困难 飞飞 数据处理 待安排 无 婷婷 调试代码 提升acc 无 恩升 修正evaluate 待完成 无 金华 ...
- 记-ItextPDF+freemaker 生成PDF文件---导致服务宕机
摘要:已经上线的项目,出现服务挂掉的情况. 介绍:该服务是专门做打印的,业务需求是生成PDF文件进行页面预览,主要是使用ItextPDF+freemaker技术生成一系列PDF文件,其中生成流程有:解 ...
- docker-compose 快速部署Prometheus,监控docker 容器, 宿主机,ceph -- cluster集群
话不多说上菜: 现在环境是这样: ceph 4台: 192.168.100.21 ceph-node1 192.168.100.22 ceph-node2 192.168.100.23 ceph ...