zoukankan      html  css  js  c++  java
  • HDU

    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.

    题意:

    给你两个图,一个用0,1,2,3表示,一个用 L 或 . 表示。其中用L表示的图中,有L的位置表示有蜥蜴,没有L的位置表示没有蜥蜴。用数字表示的图中,数字表示当前位置柱子的高度,每次一个蜥蜴可以从一个柱子跳到距离d以内的另外一个柱子,每跳跃一次(注意是跳走后柱子才降低),当前柱子的高度就减一,问最后会有多少只蜥蜴被困在里面。

    题解:

    主要是建模,把外面当成一个超级汇点t,把每个柱子拆成两个点啊v->v`,flow = 柱子的高度,如果当前柱子有蜥蜴就让超级源点s->v ,flow = 1。如果当前柱子可以跳到另一个柱子j上就v`->j,flow = INF。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <cmath>
    
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int MAXN = 910;
    
    struct Edge
    {
    	int flow,to,rev;
    	Edge() {}
    	Edge(int a,int b,int c):to(a),flow(b),rev(c) {}
    };
    
    vector<Edge> E[MAXN];
    
    inline void Add(int from,int to,int flow)
    {
    	E[from].push_back(Edge(to,flow,E[to].size()));
    	E[to].push_back(Edge(from,0,E[from].size()-1));
    }
    
    int deep[MAXN];
    int iter[MAXN];
    
    bool BFS(int from,int to)
    {
    	memset(deep,-1,sizeof deep);
    	deep[from] = 0;
    	queue<int> Q;
    	Q.push(from);
    	while(!Q.empty())
    	{
    		int t = Q.front();
    		Q.pop();
    		for(int i=0 ; i<E[t].size() ; ++i)
    		{
    			Edge& e = E[t][i];
    			if(e.flow > 0 && deep[e.to] == -1)
    			{
    				deep[e.to] = deep[t] + 1;
    				Q.push(e.to);
    			}
    		}
    	}
    	return deep[to] != -1;
    }
    
    int DFS(int from,int to,int flow)
    {
    	if(from == to || flow == 0)return flow;
    	for(int& i=iter[from] ; i<E[from].size() ; ++i)
    	{
    		Edge& e = E[from][i];
    		if(e.flow > 0 && deep[e.to] == deep[from] + 1)
    		{
    			int nowflow = DFS(e.to,to,min(flow,e.flow));
    			if(nowflow > 0)
    			{
    				e.flow -= nowflow;
    				E[e.to][e.rev].flow += nowflow;
    				return nowflow;
    			}
    		}
    	}
    	return 0;
    }
    
    int Dinic(int from,int to)
    {
    	int sumflow = 0;
    	while(BFS(from,to))
    	{
    		memset(iter,0,sizeof iter);
    		int mid;
    		while((mid = DFS(from,to,INF)) > 0)sumflow += mid;
    	}
    	return sumflow;
    }
    
    char map[25][25];
    char mapt[25][25];
    int book[25][25];
    
    int main()
    {
    
    	int T,N,D,M;
    	char s[25];
    	scanf("%d",&T);
    	for(int _=1 ; _<=T ; ++_)
    	{
    		scanf("%d %d",&N,&D);
    		for(int i=1 ; i<=N ; ++i)scanf("%s",&map[i]);
    		for(int i=1 ; i<=N ; ++i)scanf("%s",&mapt[i]);
    		int num = 0,ti = 0;
    		M = strlen(map[1]);
    		for(int i=1 ; i<=N ; ++i)
    		{
    			for(int j=0 ; j<M ; ++j)
    			{
    				if(mapt[i][j] == 'L')++num;
    				if(map[i][j] != '0')book[i][j] = ++ti;
    			}
    		}
    		for(int i=1 ; i<=N ; ++i)
    		{
    			for(int j=0 ; j<M ; ++j)
    			{
    				if(map[i][j] == '0')continue;
    				int time = book[i][j];
    				if(mapt[i][j] == 'L')Add(0,time,1);
    				int t = (int)(map[i][j]-'0');
    				Add(time,time+400,t);
    				if(i<=D || i+D>N || j+1<=D || j+1+D>M)Add(time+400,MAXN-1,INF);
    				int x,y;
    				for(int k=-D ; k<=D ; ++k)
    				{
    					x = i + k;
    					if(x > N)break;
    					if(x < 1)continue;
    					for(int f=-D ; f<=D ; ++f)
    					{
    						y = j + f;
    						if(y<0 || map[x][y] == '0' || (abs(k)+abs(f))>D || (k==0 && f==0))continue;
    						if(y+1 > M)break;
    						Add(time+400,book[x][y],INF);
    					}
    				}
    			}
    		}
    		num -= Dinic(0,MAXN-1);
    		if(num>=2)printf("Case #%d: %d lizards were left behind.
    ",_,num);
    		else if(num)printf("Case #%d: %d lizard was left behind.
    ",_,num);
    		else printf("Case #%d: no lizard was left behind.
    ",_);
    		for(int i=0 ; i<MAXN ; ++i)E[i].clear();
    	}
    
    	return 0;
    }


    如果某个柱子上可以直接跳到外面就让v`->t,flow = INF。

  • 相关阅读:
    Asp.net开发必备51种代码
    防止页面被多次提交
    c#发送邮件.net1.1和.net2.0中的两个方法
    鼠标移至小图,自动显示相应大图
    NET(C#)连接各类数据库集锦
    在C#中对XML的操作
    Window.ShowModalDialog使用总结
    SQLServer2005 添加用户,及操作权限
    定时器
    Global.asax.cs中的方法的含义
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514081.html
Copyright © 2011-2022 走看看