Codeforces Round #390 (Div. 2) - B

 

题目链接:http://codeforces.com/contest/754/problem/B

题意:给定4X4的字符矩阵,字符只有'x','o','.'。现在问你能不能最多把一个'.'变成'x'(也可以不提变),使得存在三个'x'连在一起(平行,垂直,对角相连都行)。

思路:暴力枚举每个位置,然后向八个方向找出另外2个位置。最后分类讨论即可。

import java.awt.Point;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
    public static final int MAXN = 10;
    public static String G[] = new String[MAXN];
    public static int dis[][] = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 }, { 1, 1 }, { 1, -1 }, { -1, 1 },
            { -1, -1 } };

    public static boolean check(Point a) {
        return a.x >= 0 && a.x < 4 && a.y >= 0 && a.y < 4;
    }

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        for (int i = 0; i < 4; i++) {
            G[i] = cin.nextLine();
        }
        boolean flag = false;
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                if (G[i].charAt(j) == 'o') {
                    continue;
                }
                Point p = new Point(), q = new Point();
                for (int k = 0; k < 8; k++) {
                    p.x = i + dis[k][0];
                    p.y = j + dis[k][1];
                    q.x = i + 2 * dis[k][0];
                    q.y = j + 2 * dis[k][1];

                    if (check(p) == true && check(q) == true) {
                        if (G[p.x].charAt(p.y) == 'x' && G[q.x].charAt(q.y) == 'x') {
                            flag = true;
                        }
                        if (G[p.x].charAt(p.y) == '.' && G[q.x].charAt(q.y) == 'x' && G[i].charAt(j) != '.') {
                            flag = true;
                        }
                        if (G[p.x].charAt(p.y) == 'x' && G[q.x].charAt(q.y) == '.' && G[i].charAt(j) != '.') {
                            flag = true;
                        }
                    }
                }
            }
        }
        out.printf(flag == true ? "YES" : "NO");
        cin.close();
        out.flush();
    }
}

 

posted @ 2017-01-08 17:52  キリト  阅读(125)  评论(0编辑  收藏  举报