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