题目描述: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 ;
}

最新文章

  1. 数据结构与算法JavaScript (二) 队列
  2. Web后台技术趋势
  3. JavaScript list 去重复
  4. C++开源hash项目sparsehash
  5. 单片机modebus RTU通信实现,採用C语言,可适用于单片机,VC,安卓等
  6. 转:SqlServer2008误操作数据(delete或者update)后恢复数据
  7. 蓝桥杯-组素数-java
  8. 利用base64库暴力破解base加密
  9. js收藏代码
  10. ACM 排列2
  11. C# Note32: 查漏补缺
  12. Confluence 6 配置站点主页面
  13. Request参数值自动去空格
  14. oracle创建、删除 数据库、建立表空间以及插入 删除 修改表
  15. python学习笔记比较全
  16. JavaScript的基本概念
  17. mq刷盘方式
  18. Entity Framework应用:管理并发
  19. 浅析JavaScript访问对象属性和方法及区别
  20. 聊聊Postgres中的IPC之SI Message Queue

热门文章

  1. Java集合--线程安全(CopyOnWrite机制)
  2. Python学习笔记之基础篇(五)字典
  3. input文件类型上传,或者作为参数拼接的时候注意的问题!
  4. 解决Eclipse Debug 断点调试的source not found问题
  5. SimpleAuthenticationInfo
  6. hostPath Volume【转】
  7. 第1节 IMPALA:6、yum源制作过程
  8. 软件构造 Lab1
  9. ej3-1优先使用静态工厂方法而非构造函数来创建对象
  10. CSS样式表——列表与布局