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;
}
时间: 2024-07-29 18:25:01

hdu 5254 水题的相关文章

hdu 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画

HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 64256    Accepted Submission(s): 18286 Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N in o

hdu 4802 水题 算GPA 南京现场赛

http://acm.hdu.edu.cn/showproblem.php?pid=4802 今天心情不太好,因为跟一个学姐谈,她似乎保研有点被动,而我的条件其实不如她应该...祝她好运.刷道水题调整下心情 写的有点慢,因为没读题,直接看图,然后N P没考虑以及0/0的情况也没考虑,虽然1A,但是自己代码速度还可以,但是综合起来做题速度还是.... //#pragma comment(linker, "/STACK:102400000,102400000") #include <

hdu 2710 水题

题意:判断一些数里有最大因子的数 水题,省赛即将临近,高效的代码风格需要养成,为了简化代码,以后可能会更多的使用宏定义,但是通常也只是快速拿下第一道水题,涨自信.大部分的代码还是普通的形式,实际上能简化的部分也不太多 1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 #include<cstdio> 5 using namespace std; 6 #define for0n for(i

hdu 3443(水题,公式)Shift Number

题意: 给一个数n,求最小的数生成的shift number等于n. shift number就是一个数x,x*10,x*100-..这样的和. 思路 把那个公式处理一下,其实就是X*(形如11111-.)这样的一个式子,那么找一个最大的11111-.,x就是最小了.水题 复杂度: O(len(n)) 参考code: /* #pragma warning (disable: 4786) #pragma comment (linker, "/STACK:0x800000") */ #in

Dijkstra算法---HDU 2544 水题(模板)

/* 对于只会弗洛伊德的我,迪杰斯特拉有点不是很理解,后来发现这主要用于单源最短路,稍稍明白了点,不过还是很菜,这里只是用了邻接矩阵 套模板,对于邻接表暂时还,,,没做题,后续再更新.现将这题贴上,应该是迪杰斯特拉最水的题没有之一.纯模板 */ 题目大意: 搬东西很累,想省力,给你几个点和点之间的距离:标准题型: #include<stdio.h> #include <iostream> #include<string.h> using namespace std; #

hdu 5162(水题)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5162 题解:看了半天以为测试用例写错了.这题玩文字游戏.它问的是当前第i名是原数组中的第几个. #include<stdio.h> #include<iostream> #include<string.h> #include <stdlib.h> #include<math.h> #include<algorithm> #include

HDU2109 Fighting for HDU【水题】

Fighting for HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7486    Accepted Submission(s): 3854 Problem Description 在上一回,我们让你猜测海东集团用地的形状,你猜对了吗?不管结果如何,都没关系,下面我继续向大家讲解海东集团的发展情况: 在最初的两年里,HDU

hdu 5038 水题 但是题意坑

http://acm.hdu.edu.cn/showproblem.php?pid=5038 就是求个众数  这个范围小 所以一个数组存是否存在的状态就行了 但是这句话真恶心  If not all the value are the same but the frequencies of them are the same, there is no mode. 其实应该是这个意思: 当频率最高的有多个的时候, 如果 所有的grade出现的频率都是相等的,那么是没有mode的 否则按照升序 当然