bfs codeforces 754B Ilya and tic-tac-toe game

这题简直把我坑死了 所有的坑都被我中了

题意:

思路:bfs or 模拟 模拟似乎没有什么坑 但是bfs真的是坑

AC代码:

 1 #include "iostream"
 2 #include "string.h"
 3 #include "stack"
 4 #include "queue"
 5 #include "string"
 6 #include "vector"
 7 #include "set"
 8 #include "map"
 9 #include "algorithm"
10 #include "stdio.h"
11 #include "math.h"
12 #define ll long long
13 #define ull unsigned ll
14 #define mem(a) memset(a,0,sizeof(a))
15 #define bug cout<<"UUUUUUUUUUUUU\n";
16 using namespace std;
17 struct Node{
18     int xx,yy;
19 };
20 int d[8][2]{{1,1},{1,0},{0,1},{1,-1},{-1,0},{-1,-1},{0,-1},{-1,1}};
21 int flag;
22 char m[10][10];
23 void Bfs(int x,int y){
24     Node now,next;
25     int vis[10][10];
26     mem(vis);
27     queue<Node> Q;
28     while(!Q.empty()) Q.pop();
29     now.xx=x,now.yy=y;
30     Q.push(now);
31     vis[x][y]=1;
32     while(!Q.empty()){
33         now=Q.front();
34         Q.pop();
35         for(int i=0; i<8; i++){
36             next.xx=now.xx+d[i][0];
37             next.yy=now.yy+d[i][1];
38             int s=1;
39             int f=0;
40             if(m[now.xx][now.yy]==‘x‘) f=1;
41             if(!vis[next.xx][next.yy]&&(next.xx>0&&next.xx<5&&next.yy>0&&next.yy<5&&(m[next.xx][next.yy]==‘.‘||m[next.xx][next.yy]==‘x‘))){
42                 Q.push(next);
43                 vis[next.xx][next.yy]=1;
44             }
45             while(next.xx>0&&next.xx<5&&next.yy>0&&next.yy<5&&(m[next.xx][next.yy]==‘.‘||m[next.xx][next.yy]==‘x‘)){
46                 s++;
47                 if(m[next.xx][next.yy]==‘x‘) f++; //printf("%d %d\n%d %d %d\n",s,f,next.xx,next.yy,i);
48                 if(s==3&&f>=2) {printf("YES\n");flag=1;return;}
49                 next.xx+=d[i][0];
50                 next.yy+=d[i][1];
51             }
52         }
53     }
54 }
55 int main(){
56     mem(m);
57     int x1,y1;
58     for(int i=1; i<=4; ++i)
59         for(int j=1; j<=4; ++j)
60             cin>>m[i][j];
61     for(int i=1; i<5; ++i){
62         for(int j=1; j<5; ++j){
63             if(!flag&&(m[i][j]==‘.‘||m[i][j]==‘x‘)) Bfs(i,j);
64         }
65     }
66     if(!flag) printf("NO\n");
67     return 0;
68 }
69 /*
70 o.x.
71 o...
72 .x..
73 ooxx
74
75 x.ox
76 ox..
77 x.o.
78 oo.x
79
80 xoxx
81 ..x.
82 o.oo
83 x.o.
84 */
时间: 2024-10-06 01:21:38

bfs codeforces 754B Ilya and tic-tac-toe game的相关文章

tic tac toe

井字棋 tic tac toe,布布扣,bubuko.com

amazon.设计1. tic tac toe

//不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html 类似相关棋牌坐标对战游戏 一通百通 /** * Enumerations for the various states of the game */ public enum GameState { // to save as "G

Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy

1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputing-001/wiki/view? page=trees 1.2 CODE无parent域的树 http://www.codeskulptor.org/#poc_tree.py class Tree: """ Recursive definition for trees plus

2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

题面: C. Team Tic Tac Toe Input ?le: standard input Output ?le: standard output Time limit: 1 second Memory limit: 256 megabytes Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer J

【leetcode】1275. Find Winner on a Tic Tac Toe Game

题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-Toe: Players take turns placing characters into empty squares (" "). The first player A always places "X" characters, while the second pl

CodeForces - 754B Ilya and tic-tac-toe game

简单搜索 判断是否能在最后一步下棋得到胜利 问题转化为 是否有可以胜利的x的摆法 那么就只有两种情况 1.有两个x相连 并且 在端点还有.可以落子 那么就可以在最后一步 胜利 2.两个x中间恰好有一个.空着 那么可以再这里落子胜利 搜索这两种情况 存在则胜利 不存在 则无法再最后一步胜利 1 #include <iostream> 2 #include <stdio.h> 3 #include <string> 4 #include <string.h> 5

Epic - Tic Tac Toe

N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If 3 consecutive samecolor found, that color will get 1 point. So if 4 red are vertically then pointis 2. Find the winner. def tic_tac_toe(board,n) red,

Learn Python 015: Tic Tac Toe Game

board = [' ' for i in range(9)] def print_board(): row_1 = '|{}|{}|{}|'.format(board[0], board[1], board[2]) row_2 = '|{}|{}|{}|'.format(board[3], board[4], board[5]) row_3 = '|{}|{}|{}|'.format(board[6], board[7], board[8]) print('\n') print(row_1)

UVa 10363 - Tic Tac Toe

题目:给你一个井字棋的状态,判断是否合法. 分析:枚举.直接枚举多有情况判断即可. 合法状态有三种情况:(X先下子) 1.X赢,则O不能赢,且X比O多一子: 2.O赢,则X不能赢,且O和X子一样多: 3.没人赢,此时O的子和可能和X一样多,也可能少一个. 说明:简单题(⊙_⊙). #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include &l