CodeForces 825B(DFS)
来源:互联网 发布:linux配置ip地址命令 编辑:程序博客网 时间:2024/06/06 08:43
问题描述:
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 × 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.
In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.
Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.
InputYou are given matrix 10 × 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.
It is guaranteed that in the current arrangement nobody has still won.
OutputPrint 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
Example
XX.XX..........OOOO.................................................................................
YES
XXOXX.....OO.O......................................................................................
NO
题目题意:给了我们一个10*10的图,问我们能否在加上一个X满足五子棋的成立条件.
题目分析:我们遍历每一个'.'点,把它变成'X‘,然后去沿四个方向去dfs(),
题目代码:
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<string>using namespace std;char mmap[15][15];bool vis[15][15],ans;int Next1[2][2]={{-1,0},{1,0}};int Next2[2][2]={{0,-1},{0,1}};int Next3[2][2]={{-1,-1},{1,1}};int Next4[2][2]={{1,-1},{-1,1}};int cnt;void dfs1(int x,int y){ if (ans) return ; if (cnt==5) { ans=true; return ; } for (int i=0;i<2;i++) { int dx=x+Next1[i][0]; int dy=y+Next1[i][1]; if (dx<1||dx>10||dy<1||dy>10) continue; if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue; if (mmap[dx][dy]=='X') { vis[dx][dy]=true; cnt++; dfs1(dx,dy); } }}void dfs2(int x,int y){ if (ans) return ; if (cnt==5) { ans=true; return ; } for (int i=0;i<2;i++) { int dx=x+Next2[i][0]; int dy=y+Next2[i][1]; if (dx<1||dx>10||dy<1||dy>10) continue; if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue; if (mmap[dx][dy]=='X') { vis[dx][dy]=true; cnt++; dfs2(dx,dy); } }}void dfs3(int x,int y){ if (ans) return ; if (cnt==5) { ans=true; return ; } for (int i=0;i<2;i++) { int dx=x+Next3[i][0]; int dy=y+Next3[i][1]; if (dx<1||dx>10||dy<1||dy>10) continue; if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue; if (mmap[dx][dy]=='X') { vis[dx][dy]=true; cnt++; dfs3(dx,dy); } }}void dfs4(int x,int y){ if (ans) return ; if (cnt==5) { ans=true; return ; } for (int i=0;i<2;i++) { int dx=x+Next4[i][0]; int dy=y+Next4[i][1]; if (dx<1||dx>10||dy<1||dy>10) continue; if (mmap[dx][dy]=='.'||mmap[dx][dy]=='0'||vis[dx][dy]) continue; if (mmap[dx][dy]=='X') { vis[dx][dy]=true; cnt++; dfs4(dx,dy); } }}int main(){ for (int i=1;i<=10;i++) { scanf("%s",mmap[i]+1); } ans=false; for (int i=1;i<=10;i++) { for (int j=1;j<=10;j++) { if (mmap[i][j]=='.'&&!ans) { mmap[i][j]='X'; memset (vis,false,sizeof (vis)); vis[i][j]=true; cnt=1; dfs1(i,j); memset (vis,false,sizeof (vis)); vis[i][j]=true; cnt=1; dfs2(i,j); memset (vis,false,sizeof (vis)); vis[i][j]=true; cnt=1; dfs3(i,j); memset (vis,false,sizeof (vis)); vis[i][j]=true; cnt=1; dfs4(i,j); mmap[i][j]='.'; } if (ans) break; } if (ans) break; } if (ans) puts("YES"); else puts("NO");}
- CodeForces 825B(DFS)
- codeforces 510B dfs
- codeforces 685B (dfs)
- codeforces 768B dfs
- Codeforces 840B(DFS)
- CodeForces 626B-Cards【DFS】
- Codeforces 27B - Tournament (dfs)
- codeforces 510B (dfs 水)
- CodeForces-887B 技巧DFS
- Codeforces Testing Round #5 B DFS
- Codeforces 389B Fox and Cross(dfs)
- Codeforces Round #306 (Div. 2) B(dfs)
- codeforces 550B Preparing Olympiad(DFS+回溯)
- Codeforces 653B Bear and Compressing【DFS】
- Codeforces 283B Distinct Paths (dfs+剪枝)
- codeforces 653B Bear and Compressing (dfs)
- CodeForces 55B Smallest number(DFS)
- Codeforces Round #306 (Div. 2) B DFS
- kk常用vim指令
- QtSerialPort在ARM下面的编译问题
- Perl语言入门
- AccessDatabaseEngine.exe解决ACE.OLEDB.12.0问题
- Linux 进程的 Uninterruptible sleep(D) 状态
- CodeForces 825B(DFS)
- Truffle 4.0、Geth 1.7.2、TestRPC在私有链上搭建智能合约
- C# 将二进制数据转换成BASE64字符串的方法
- bootstrap3中轮播图修改时间
- 微信分享遇到的坑
- linux下安装nginx步骤
- 项目笔记
- [老贴重发]EXT:指定树控件TreePanel 展开到第几层
- 【转】Oracle XA JTA事务报错 不支持XA事务