hdu 2732Leapin' Lizards

Leapin' Lizards

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

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.

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.

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.


zty   |   We have carefully selected several similar problems for you:  3338 1533 3416 1569 2883 

题意:有n行,没有告诉你多少列,一直蜥蜴最多能跳d远,d这个距离指的是 abs(x1-x2)+abs(y1-y2)。每个点都有一个耐久度digit(0-3),蜥蜴跳一次就减少一次耐久度,‘L’代表该点有一个蜥蜴。





#include <bits/stdc++.h>
#include <ext/hash_map>
#include <ext/hash_set>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;
#define XINF INT_MAX
#define INF 0x3F3F3F3F
#define MP(X,Y) make_pair(X,Y)
#define PB(X) push_back(X)
#define REP(X,N) for(int X=0;X<N;X++)
#define REP2(X,L,R) for(int X=L;X<=R;X++)
#define DEP(X,R,L) for(int X=R;X>=L;X--)
#define CLR(A,X) memset(A,X,sizeof(A))
#define IT iterator
#define RIT reverse_iterator
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<PII> VII;
typedef vector<int> VI;
typedef tree<PII, null_type, greater<PII>, rb_tree_tag, tree_order_statistics_node_update > rb_tree_set;
typedef tree<PII, PII, greater<PII>, rb_tree_tag, tree_order_statistics_node_update > rb_tree;
#define PQ std::priority_queue
#define HEAP __gnu_pbds::priority_queue
#define X first
#define Y second
#define lson(X) ((X)<<1)
#define rson(X) ((X)<<1|1)
#define N 100
#define M 100000
char mapt1[N][N];
char mapt2[N][N];
int n, m, k;
int src, des;
struct Edeg
	queue <int> q;
	int g[N*N*2], nt[M], vt[M], c[M], ec;
	int cur[N*N*2], lev[N*N*2];
	inline void init() {
		ec = 1;
		memset(g, 0 ,sizeof(g));
	inline void addedge(int x, int y ,int cap) {
		nt[++ec] = g[x]; g[x] = ec; vt[ec] = y; c[ec] = cap;
		nt[++ec] = g[y]; g[y] = ec; vt[ec] = x; c[ec] = 0;
	inline bool bfs() {
		while(!q.empty()) q.pop();
		memset(lev, 0, sizeof(lev));
		lev[src] = 1;
		while(!q.empty()) {
			int u = q.front();
			for(int e = g[u]; e; e = nt[e]) {
				if(!c[e]||lev[vt[e]])	continue;
				lev[vt[e]] = lev[u] + 1;
				if(vt[e] == des)
					return true;
		return false;
	inline int dinic(int x, int f) {
		if(x == des)	return f;
		int rs = 0;
		for(int &e = cur[x]; e; e = nt[e]) {
			if(!c[e] || lev[vt[e]]!=lev[x]+1)
			int flow = dinic(vt[e], min(f-rs, c[e]));
			c[e] -= flow;
			c[e^1] += flow;
			rs += flow;
			if(rs <= 0) {
                int t;
			if(rs == f)
				return rs;

		return lev[x] = 0, rs;
	inline int maxflow() {
		int rs = 0;
		while(bfs()) {
			int tt;
			memcpy(cur, g, sizeof(cur));
			while((tt=dinic(src, INF)))
				rs+=tt, memcpy(cur, g, sizeof(cur));

		return rs;

int main()
	int T;
	int Case = 1;
	while(T--) {
		scanf("%d%d",&n, &k);
		for(int i = 1; i <= n; i++) {
			scanf("%s", mapt1[i]+1);
			m = strlen(mapt1[i]+1);
			for(int j = 1; j <= m; j++) {
				G.addedge(m*(i-1)+j, (i-1)*m+n*m+j, mapt1[i][j]-'0');
		int ans = 0;
		src = 0, des = n*m*2+1;
		for(int i = 1; i <= n; i++) {
			scanf("%s", mapt2[i]+1);
			for(int j = 1; j <= m;j ++) {
				if(mapt2[i][j] == 'L') {
					ans ++;
					G.addedge(src,(i-1)*m+j, 1);
		for(int i = 1; i <= n ;i++) {
			for(int j = 1; j <= m; j++) {
				if(mapt1[i][j]-'0' == 0)
				if(i-k<=0 || i+k>n || j-k<=0|| j+k > m)
					G.addedge((i-1)*m+n*m+j, des, INF);
				else {
					for(int x = 1 ; x <= n; x++) {
						for(int y = 1; y <= m; y++) {
							if(mapt1[x][y] == '0')
								G.addedge((i-1)*m+n*m+j, (x-1)*m+y, INF);
		int t = ans - G.maxflow();
		if(t == 0) {
            printf("Case #%d: no lizard was left behind.\n", Case++);
		}else if(t == 1) {
		    printf("Case #%d: 1 lizard was left behind.\n", Case++);
		}else {
		    printf("Case #%d: %d lizards were left behind.\n", Case++, t);
    return 0;

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37785469/article/details/79954445
文章标签: hdu 2732 网络流拆点
个人分类: 图论 网络流
想对作者说点什么? 我来说一句