码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-02-23 20:54:42      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:open   ace   div   turn   return   pair   ring   names   type   

简单搜索

判断是否能在最后一步下棋得到胜利

问题转化为 是否有可以胜利的x的摆法

那么就只有两种情况

1、有两个x相连 并且 在端点还有.可以落子 那么就可以在最后一步 胜利

2、两个x中间恰好有一个.空着 那么可以再这里落子胜利

搜索这两种情况 存在则胜利 不存在 则无法再最后一步胜利

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string>
 4 #include <string.h>
 5 #include <map>
 6 #include <queue>
 7 #include <fstream>
 8 #define READ() freopen("in.txt", "r", stdin);
 9 using namespace std;
10 
11 typedef pair<int,int> P;
12 
13 char maze[8][8];
14 struct Point
15 {
16     int x, y;
17 }point[32];
18 int num = 0;
19 int d[8][2] = {-1, 0, -1, 1, 0, 1, 1, 1, 1, 0, 1, -1, 0, -1, -1, -1};
20 bool vis[8][8];
21 bool OK(int x, int y)
22 {
23     if (x < 0 || x >= 4 || y < 0 || y >= 4) return false;
24     return true;
25 }
26 bool Search()
27 {
28 
29     queue<P> que;
30     for (int i = 0; i < num; i++)
31     {
32         que.push(P(point[i].x, point[i].y));
33     }
34    // memset(vis, 0, sizeof(vis));
35     while (!que.empty())
36     {
37         P p = que.front();
38         que.pop();
39        // if(vis[p.first][p.second]) continue;
40        // vis[p.first][p.second] = true;
41         //假设是作为端点 的点
42         for (int i = 0; i < 8; i++)
43         {
44             int nx = p.first+d[i][0], ny = p.second+d[i][1];
45             if (!OK(nx, ny)) continue;
46             if (maze[nx][ny] == x)
47             {
48                 nx += d[i][0];
49                 ny += d[i][1];
50                 if (!OK(nx, ny)) continue;
51                 if (maze[nx][ny] == x)
52                 {
53                     return true;
54                 }
55             }
56         }
57         //假设是作为中间的点
58         for (int i = 0; i < 8; i++)
59         {
60             int nx = p.first+d[i][0], ny = p.second+d[i][1];
61             if (!OK(nx, ny)) continue;
62             if (maze[nx][ny] == x)
63             {
64                 nx = p.first-d[i][0], ny = p.second-d[i][1];
65                 if (!OK[nx][ny]) continue;
66                 if (maze[nx][ny] == x)
67                 {
68                     return true;
69                 }
70             }
71         }
72     }
73     return false;
74 
75 
76 }
77 int main()
78 {
79     READ()
80     for (int i = 0; i < 4; i++)
81     {
82         for (int j = 0; j < 4; j++)
83         {
84             scanf("%c", &maze[i][j]);
85             if (maze[i][j] == .)
86             {
87                 point[num].x = i;
88                 point[num].y = j;
89                 num++;
90             }
91         }
92         getchar();
93     }
94     if (Search()) cout << "YES" << endl;
95     else cout << "NO" << endl;
96 }

 

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

标签:open   ace   div   turn   return   pair   ring   names   type   

原文地址:http://www.cnblogs.com/oscar-cnblogs/p/6435281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!