关闭

hdoj 2732 Leapin' Lizards 【拆点网路流】 【题目数据坑。。。】

359人阅读 评论(0) 收藏 举报
分类:


Leapin' Lizards

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1568    Accepted Submission(s): 637


Problem Description
Your platoon of wandering lizards has entered a strange room in the labyrinth you are exploring. As you are looking around for hidden treasures, one of the rookies steps on an innocent-looking stone and the room's floor suddenly disappears! Each lizard in your platoon is left standing on a fragile-looking pillar, and a fire begins to rage below... Leave no lizard behind! Get as many lizards as possible out of the room, and report the number of casualties.
The pillars in the room are aligned as a grid, with each pillar one unit away from the pillars to its east, west, north and south. Pillars at the edge of the grid are one unit away from the edge of the room (safety). Not all pillars necessarily have a lizard. A lizard is able to leap onto any unoccupied pillar that is within d units of his current one. A lizard standing on a pillar within leaping distance of the edge of the room may always leap to safety... but there's a catch: each pillar becomes weakened after each jump, and will soon collapse and no longer be usable by other lizards. Leaping onto a pillar does not cause it to weaken or collapse; only leaping off of it causes it to weaken and eventually collapse. Only one lizard may be on a pillar at any given time.
 

Input
The input file will begin with a line containing a single integer representing the number of test cases, which is at most 25. Each test case will begin with a line containing a single positive integer n representing the number of rows in the map, followed by a single non-negative integer d representing the maximum leaping distance for the lizards. Two maps will follow, each as a map of characters with one row per line. The first map will contain a digit (0-3) in each position representing the number of jumps the pillar in that position will sustain before collapsing (0 means there is no pillar there). The second map will follow, with an 'L' for every position where a lizard is on the pillar and a '.' for every empty pillar. There will never be a lizard on a position where there is no pillar.Each input map is guaranteed to be a rectangle of size n x m, where 1 ≤ n ≤ 20 and 1 ≤ m ≤ 20. The leaping distance is
always 1 ≤ d ≤ 3.
 

Output
For each input case, print a single line containing the number of lizards that could not escape. The format should follow the samples provided below.
 

Sample Input
4 3 1 1111 1111 1111 LLLL LLLL LLLL 3 2 00000 01110 00000 ..... .LLL. ..... 3 1 00000 01110 00000 ..... .LLL. ..... 5 2 00000000 02000000 00321100 02000000 00000000 ........ ........ ..LLLL.. ........ ........
 

Sample Output
Case #1: 2 lizards were left behind. Case #2: no lizard was left behind. Case #3: 3 lizards were left behind. Case #4: 1 lizard was left behind.
 
 
一共做了3个小时,其中调试一个多小时,最后发现题目数据D可以等于4,不是思路错误。。。
 
 
题意:迷宫着火了,在迷宫里有一个N * M地图,地图上每个坐标对应着一个柱子,每个柱子都有对应的高度,也有一些柱子上停留着蜥蜴。当蜥蜴从一个柱子跳走时,该柱子高度减一,若高度为0说明蜥蜴不能跳到该柱子上面,若蜥蜴跳出这个N*M地图,说明逃脱成功。先输入一个N * M矩阵,表示每个柱子的高度,再输入一个N * M矩阵表示该柱子上是否有蜥蜴,若为L表明柱子上有一个蜥蜴反之说明该柱子是空的。现在给出蜥蜴能够跳跃的最大距离,问最少有多少蜥蜴不能逃脱。
 
提醒:输入数据只给出了N,M需要另外求。

 
全靠自己AC的题目,没看任何题解。
就是错了4次后,看讨论区说D可能为4,加上D = 4需要建的边就过了。这次看讨论区不能算我借助外力吧, 题目说好的1 <= D <= 3呢,无语。
害我模拟1个多小时,原来是题目    D的数据有4的情况。。。

 

 


思路:

先求出每行的柱子数目M。

基础拆点:把每根高度不为0的柱子(i, j) 拆分成 j + i * M 和 400 + j + i * M两点权值为柱子高度。

构建源点汇点:建立超级源点source = 900,超级汇点sink = 1000(题目最多建点数目不会过800)。

基础建边:若坐标(i, j)处有蜥蜴,则构建一条由source 指向 j + i * M 的边且权值为1。 

重头戏:对于高度不为0的柱子 (i, j),枚举其D范围内的坐标 (x, y),若坐标 (x, y) 越界说明从该柱子可以直接跳出去,构建一条由400 + j + i * M 指向 sink 的边且权值为INF;若没有越界且(x, y)坐标处柱子高度大于0,则构建一条由400 + j + i * M 指向 y + x * M的边且权值为INF。



AC代码:


#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
#define MAXN 1000+10//最多800个点 
#define MAXM 40000
#define INF 100000+10
using namespace std;
struct Edge
{
    int from, to, cap, flow, next;
}edge[MAXM];
int N, M, D;
int head[MAXN], cur[MAXN], top;
int dist[MAXN];
bool vis[MAXN];
char Map[21][21];
char val[21][21];//记录每个位置最多可以跳几次
int source = 900, sink = 1000;//超级汇点 
int sum;//蜥蜴总数目 
bool judge(int x, int y)//判断是否越界 
{
    return x >= 0 && x < N && y >= 0 && y < M; 
}
void init()
{
    top = 0;
    memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int w)
{
    Edge E1 = {u, v, w, 0, head[u]};
    edge[top] = E1;
    head[u] = top++;
    Edge E2 = {v, u, 0, 0, head[v]};
    edge[top] = E2;
    head[v] = top++;
}
void getMap()
{
    int a, b, c;
    int x, y;
    sum = 0;
    int move1[4][2] = {0,1, 0,-1, 1,0, -1,0};//D为1 可以跳4个点 
    int move2[8][2] = {0,2, 0,-2, 2,0, -2,0, 1,1, 1,-1, -1,1, -1,-1};//D为2 可以跳着8个点加上D为1的4个点  下同 
    int move3[16][2] = {0,3, 0,-3, 3,0, -3,0, 1,2, 1,-2, -1,2, -1,-2, 2,1, 2,-1, -2,1, -2,-1, 2,2, 2-2, -2,2, -2,-2};//D为3
    int move4[20][2] = {0,4, 0,-4, 4,0, -4,0, 1,3, 1,-3, -1,3, -1,-3, 2,3, 2,-3, -2,3, -2,-3, 3,1, 3,-1, -3,1, -3,-1, 3,2, 3,-2, -3,2, -3,-2};
    for(int i = 0; i < N; i++)
    {
        scanf("%s", val[i]);
        M = strlen(val[i]);
        for(int j = 0; j < M; j++)
        { 
            if(val[i][j] == '0') continue; 
            a = j + i * M;
            b = 400 + a; 
            addEdge(a, b, val[i][j]-'0');//拆点建边 
        }
    }
    for(int i = 0; i < N; i++)
    {
        scanf("%s", Map[i]);
        for(int j = 0; j < M; j++)
        {
            if(Map[i][j] == 'L')//有蜥蜴 
            {
                sum++; 
                a = j + i * M;
                addEdge(source, a, 1);//从源点引一条 容量为一的边 
            }
        }
    }
    for(int i = 0; i < N; i++)
    {
        for(int j = 0; j < M; j++)
        {
            if(val[i][j] > '0')
            {
                a = j + i * M;
                b = 400 + a;
                for(int k = 0; k < 4; k++)
                {
                    x = i + move1[k][0];
                    y = j + move1[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')//当前位置必须 可跳跃至少一次 
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else//越界说明可以直接跳出去  
                    addEdge(b, sink, INF);//连接汇点 
                }
                if(D == 1) 
                continue;
                for(int k = 0; k < 8; k++)
                {
                    x = i + move2[k][0];
                    y = j + move2[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else
                    addEdge(b, sink, INF);//连接汇点 
                }
                if(D == 2)
                continue; 
                for(int k = 0; k < 16; k++)
                {
                    x = i + move3[k][0];
                    y = j + move3[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else
                    addEdge(b, sink, INF);//连接汇点 
                }
                if(D == 3)
                continue;
                for(int k = 0; k < 20;  k++)
                {
                    x = i + move4[k][0];
                    y = j + move4[k][1];
                    c = y + x * M;
                    if(judge(x, y))//没有越界
                    {
                        if(val[x][y] != '0')
                        addEdge(b, c, INF);//直接连接 
                    } 
                    else
                    addEdge(b, sink, INF);//连接汇点 
                } 
            } 
        }
    }
} 
bool BFS(int start, int end)//寻找是否存在增广路 
{
    queue<int> Q;
    memset(dist, -1, sizeof(dist));
    memset(vis, false, sizeof(vis));
    Q.push(start);
    dist[start] = 0;
    vis[start] = true;
    while(!Q.empty())
    {
        int u = Q.front();
        Q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            Edge E = edge[i];
            if(!vis[E.to] && E.cap - E.flow > 0)
            {
                dist[E.to] = dist[u] + 1;
                vis[E.to] = true;
                if(E.to == end)
                return true;
                Q.push(E.to); 
            }
        }
    } 
    return false;
}
int DFS(int x, int a, int end)//增广路 
{
    if(x == end || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next)
    {
        Edge &E = edge[i];
        if(dist[E.to] == dist[x] + 1 && (f = DFS(E.to, min(a, E.cap - E.flow), end)) > 0)
        {
            E.flow += f;
            edge[i^1].flow -= f;
            flow += f;
            a -= f;
            if(a == 0)
            break;
        }
    }
    return flow;
}
int Maxflow(int start, int end)
{
    int flow = 0;
    while(BFS(start, end))
    {
        memcpy(cur, head, sizeof(head));
        flow += DFS(start, INF, end);
    }
    return flow;
}
int main()
{
    int t;
    int k = 1;
    int ans;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d%d", &N, &D);
        init();
        getMap();
        ans = sum - Maxflow(source, sink);
        printf("Case #%d: ", k++);
        if(ans == 0)
        printf("no lizard was left behind.\n");
        else if(ans == 1)
        printf("1 lizard was left behind.\n");
        else
        printf("%d lizards were left behind.\n", ans); 
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:512828次
    • 积分:19520
    • 等级:
    • 排名:第452名
    • 原创:1484篇
    • 转载:12篇
    • 译文:0篇
    • 评论:162条
    文章分类