-->The Game

直接中文

Descriptions:

判断五子棋棋局是否有胜者,有的话输出胜者的棋子类型,并且输出五个棋子中最左上的棋子坐标;没有胜者输出0。棋盘是这样的,如图

Sample Input

1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 2 0 0 2 2 2 1 0 0 0 0 0 0 0 0 0 0
0 0 1 2 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
0 0 0 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 1 2 2 0 0 0 0 0 0 0 0 0 0 0 0
0 0 1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 2 1 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Sample Output

1
3 2

题目链接:
https://vjudge.net/problem/POJ-1970

五子棋,但是有点小坑,不能六子连在一起,简单说就是只能5颗棋子在一起

从上到下、从左到右遍历棋盘,当位置(x, y)有棋子时,则从该位置开始dfs,加上搜索的方向为向右(x, y+1),向下(x+1, y),右下(x+1, y+1),右上(x, y-1),所以如果搜索的结果符合获胜的条件,则(x, y)就为最左上的位置。

除了坐标之外,还要加上方向vist[i][j][d],表示在坐标(i, j)的d方向是否搜索过。

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 25
using namespace std;
int T;
int n=;
int cnt;
int vis[Maxn][Maxn][Maxn];//vist[i][j][d] 坐标(i, j)的d方向是否搜索过
int mp[Maxn][Maxn];//棋盘
int dt[][] = {{-, }, {, }, {, }, {, }}; //方向为右上、右、右下、下
void dfs(int x,int y,int k,int people)//(x,y) k方向 people哪个人的棋子
{
vis[x][y][k]=;
int dx=x+dt[k][];
int dy=y+dt[k][];
if(dx>=&&dy>=&&dx<=n&&dy<=n&&mp[dx][dy]==people)
{
cnt++;
dfs(dx,dy,k,people);
}
}
int main()
{
cin>>T;
while(T--)
{
int f=;//初始化
MEM(vis,);
MEM(mp,);
for(int i=; i<=n; i++)//存图
for(int j=; j<=n; j++)
cin>>mp[i][j];
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][j])//不为0,开始搜
{
int people=mp[i][j];//看看是谁下的棋
for(int k=;k<;k++)//四个方向
{
if(!vis[i][j][k])
{
cnt=;//棋子的个数
dfs(i,j,k,people);
//5颗棋子,防止左上角之前还有一个棋子
if(cnt==&&mp[i-dt[k][]][j-dt[k][]]!=people)
{
f=;
cout<<people<<endl;
cout<<i<<" "<<j<<endl;
break;
}
}
}
}
}
}
if(f==)
cout<<""<<endl;
}
}

最新文章

  1. atitit. access token是什么??微信平台公众号开发access_token and Web session保持状态机制
  2. Codeforces Round #360 (Div. 2) E. The Values You Can Make DP
  3. 2013-07-24 IT 要闻速记快想
  4. iOS 图片加载框架- SDWebImage 解读
  5. 关于 gravity与layout_gravity
  6. 老漏洞easy击:CVE-2012 0158占顶!
  7. 文字超出DIV后,隐藏文字并显示...
  8. PHP加密解密的函数
  9. PV &amp; PVC - 每天5分钟玩转 Docker 容器技术(150)
  10. Touch Handling in Cocos2D 3.x(三)
  11. 拼多多大数据开发工程师SQL实战解析
  12. Typescript高级类型与泛型难点详解
  13. 学以致用三十一-----IPAddressField has been removed
  14. SQL注入之Sqli-labs系列第三十六关(基于宽字符逃逸GET注入)和三十七关(基于宽字节逃逸的POST注入)
  15. SharePoint自动化部署,利用PowerShell 导出/导入AD中的用户
  16. Android 自定义倒计时控件CountdownTextView
  17. 开始 Dojo 开发
  18. nodejs实现拉钩网爬虫
  19. oracle中的一些基本概念
  20. sparkr基本操作1

热门文章

  1. quartz (从原理到应用)详解篇(转)
  2. MySQL索引-B+树(看完你就明白了)
  3. IntelliJ IDEA 创建 Git 分支并且 Push 到远程
  4. python 处理 json 四个函数dumps、loads、dump、load的区别
  5. 【java设计模式】-06原型模式
  6. win10系统配置FTP
  7. logserver 日志服务项目发布
  8. Android APP切换到后台接收不到推送消息
  9. Centos7卸载nginx及php、php-fpm方法
  10. 阿里云Ubuntu 16 FTP安装配置注意事项