hdu 5254 水题

纯暴力就能过的,可是题目描述真心不清楚,我看了好久好久才明白题目啥意思。

为了迅速打完,代码比较冗余。

/*
 * Author    : ben
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
typedef long long LL;
typedef long long LL;
/*
 * 输入非负整数
 * 支持short、int、long、long long等类型(修改typec即可)。
 * 用法typec a = get_int();返回-1表示输入结束
 */
typedef int typec;
typec get_int() {
    typec res = 0, ch;
    while (!((ch = getchar()) >= '0' && ch <= '9')) {
        if (ch == EOF)
            return -1;
    }
    res = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + (ch - '0');
    return res;
}
//输入整数(包括负整数,故不能通过返回值判断是否输入到EOF,本函数当输入到EOF时,返回-1),用法int a = get_int2();
int get_int2() {
    int res = 0, ch, flag = 0;
    while (!((ch = getchar()) >= '0' && ch <= '9')) {
        if (ch == '-')
            flag = 1;
        if (ch == EOF)
            return -1;
    }
    res = ch - '0';
    while ((ch = getchar()) >= '0' && ch <= '9')
        res = res * 10 + (ch - '0');
    if (flag == 1)
        res = -res;
    return res;
}
/**
 * 输入一个字符串到str中,与scanf("%s", str)类似,
 * 会忽略掉缓冲区中的空白字符。返回值为输入字符串
 * 的长度,返回-1表示输入结束。
 */
int get_str(char *str) {
    char c;
    while ((c = getchar()) <= ' ') {
        if(c == EOF) {
            return -1;
        }
    }
    int I = 0;
    while (c > ' ') {
        str[I++] = c; c = getchar();
    }
    str[I] = 0;
    return I;
}

const int MAXN = 550;
const int MAXM = 260000;
char graph[MAXN][MAXN];
int N, M, cnt;
int X[MAXM], Y[MAXM];

bool dfs(int x, int y) {
    bool flag = false;
    if (graph[x - 1][y - 1] == 1) {
        if (graph[x - 1][y] == 0) {
            graph[x - 1][y] = 1;
            X[cnt] = x - 1;
            Y[cnt] = y;
            cnt++;
            flag = true;
            dfs(x - 1, y);
        }
        if (graph[x][y - 1] == 0) {
            graph[x][y - 1] = 1;
            X[cnt] = x;
            Y[cnt] = y - 1;
            cnt++;
            flag = true;
            dfs(x, y - 1);
        }
    }
    if (graph[x - 1][y + 1] == 1) {
        if (graph[x - 1][y] == 0) {
            graph[x - 1][y] = 1;
            X[cnt] = x - 1;
            Y[cnt] = y;
            cnt++;
            flag = true;
            dfs(x - 1, y);
        }
        if (graph[x][y + 1] == 0) {
            graph[x][y + 1] = 1;
            X[cnt] = x;
            Y[cnt] = y + 1;
            cnt++;
            flag = true;
            dfs(x, y + 1);
        }
    }
    if (graph[x + 1][y - 1] == 1) {
        if (graph[x + 1][y] == 0) {
            graph[x + 1][y] = 1;
            X[cnt] = x + 1;
            Y[cnt] = y;
            cnt++;
            flag = true;
            dfs(x + 1, y);
        }
        if (graph[x][y - 1] == 0) {
            graph[x][y - 1] = 1;
            X[cnt] = x;
            Y[cnt] = y - 1;
            cnt++;
            flag = true;
            dfs(x, y - 1);
        }
    }
    if (graph[x + 1][y + 1] == 1) {
        if (graph[x + 1][y] == 0) {
            graph[x + 1][y] = 1;
            X[cnt] = x + 1;
            Y[cnt] = y;
            cnt++;
            flag = true;
            dfs(x + 1, y);
        }
        if (graph[x][y + 1] == 0) {
            graph[x][y + 1] = 1;
            X[cnt] = x;
            Y[cnt] = y + 1;
            cnt++;
            flag = true;
            dfs(x, y + 1);
        }
    }
    return flag;
}

void work() {
    bool change = true;
    while (change) {
        change = false;
        for (int i = 0; i < cnt; i++) {
            if (dfs(X[i], Y[i])) {
                change = true;
                break;
            }
        }
    }
}

int main() {
    int T = get_int();
    int x, y;
    for (int t = 1; t <= T; t++) {
        N = get_int();
        M = get_int();
        memset(graph, 0, sizeof(graph));
        for (int i = 0; i <= N + 1; i++) {
            graph[i][0] = -1;
            graph[i][M + 1] = -1;
        }
        for (int j = 1; j <= M; j++) {
            graph[0][j] = -1;
            graph[N + 1][j] = -1;
        }
        cnt = get_int();
        for (int i = 0; i < cnt; i++) {
            x = X[i] = get_int();
            y = Y[i] = get_int();
            graph[x][y] = 1;
        }
        work();
        int ans = 0;
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= M; j++) {
                if (graph[i][j] == 1) {
                    ans++;
                }
            }
        }
        printf("Case #%d:\n%d\n", t, ans);
    }
    return 0;
}

 

posted @ 2015-05-31 19:25  moonbay  阅读(157)  评论(0编辑  收藏  举报