UESTC OJ1221(DFS)

南阳CCPC的G题,最水的之一。

挖掉那些已经死掉的白子,问能不能一步围杀白子。

数据很小随便搞。

#include 
using namespace std;
#define maxn 11
#define move Move

char mp[maxn][maxn];
const int move[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

bool legal (int i, int j) {
    if (i < 0 || j < 0 || i >= 9 || j >= 9)
        return 0;
    return 1;
}

bool vis[maxn][maxn];
int dfs (int x, int y) {
    int ans = 0;
    vis[x][y] = 1;
    if (mp[x][y] == '.')
        return 1;
    for (int i = 0; i < 4; i++) {
        int xx = x+move[i][0];
        int yy = y+move[i][1];
        if (legal (xx, yy) && !vis[xx][yy] && mp[xx][yy] != 'x') {
            ans += dfs (xx, yy);
        }
    }
    return ans;
}

int main () {
    //freopen ("in", "r", stdin);
    ios::sync_with_stdio(false);
    int t, kase = 0;
    cin >> t;
    while (t--) {
        cout << "Case #" << ++kase << ": ";
        for (int i = 0; i < 9; i++)
            cin >> mp[i];
        bool ok = 0;
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (mp[i][j] == 'o') {
                    memset (vis, 0, sizeof vis);
                    if (dfs (i, j) == 1) {
                        cout << "Can kill in one move!!!" << endl;
                        goto out;
                    }
                }
            }
        }
        cout << "Can not kill in one move!!!" << endl;
        out: ;
    }
    return 0;
}

你可能感兴趣的:(DFS)