zoukankan      html  css  js  c++  java
  • DFS(连通块) ZOJ 2743 Bubble Shooter

    题目传送门

    题意:从炮台射出一个球,三个及以上颜色相同的会掉落,问最后会掉落多少个球

    分析:先从炮台找一个连通块,然后与顶部连接的连通块都不会掉落,剩下的就是炮台射出后跟随掉落的。

    #include <bits/stdc++.h>
    
    const int N = 100 + 5;
    char str[N][N];
    int H, W, h, w;
    int ans;
    
    bool check(int x, int y) {
    	if (x < 1 || x > H || y < 1 || y > W || (x%2==0 && y==W) || str[x][y] == '@') return false;
    	return true;
    }
    
    void DFS(int x, int y, char color, int &cnt) {
    	if (!check (x, y)) {
    		return ;
    	}
    	if (color != '@' && color != str[x][y]) {
    		return ;
    	}
    	cnt++;
    	str[x][y] = '@';
    	DFS (x - 1, y, color, cnt);
    	DFS (x + 1, y, color, cnt);
    	DFS (x, y - 1, color, cnt);
    	DFS (x, y + 1, color, cnt);
    	if (x & 1) {
    		DFS (x - 1, y - 1, color, cnt);
    		DFS (x + 1, y - 1, color, cnt);
    	} else {
    		DFS (x - 1, y + 1, color, cnt);
    		DFS (x + 1, y + 1, color, cnt);
    	}
    }
    
    int main() {
    	while (scanf ("%d%d%d%d", &H, &W, &h, &w) == 4) {
            for (int i=1; i<=H; ++i) {
    			scanf ("%s", str[i] + 1);
    			for (int j=1; j<=W; ++j) {
    				if (str[i][j] == 'E') {
    					str[i][j] = '@';
    				}
    			}
            }
    		int cnt = 0;
    		DFS (h, w, str[h][w], cnt);
    		int ans = cnt;
    		if (ans < 3) {
    			puts ("0");
    			continue;
    		}
    		for (int i=1; i<=W; ++i) {
    			DFS (1, i, '@', cnt);
    		}
    		for (int i=1; i<=H; ++i) {
    			for (int j=1; j<=W; ++j) {
    				if (i % 2 == 0 && j == W) continue;
    				if (str[i][j] != '@') {
    					ans++;
    				}
    			}
    		}
    		printf ("%d
    ", ans);
    	}
    
    	return 0;
    }
    

      

    编译人生,运行世界!
  • 相关阅读:
    HDU 1018 Big Number
    UVa 10603 倒水问题
    UVa 12108 特别困的学生
    UVa 1590 IP网络(简单位运算)
    UVa 12325 宝箱
    UVa 253 骰子涂色
    UVa 815 洪水!
    springmvc(2)处理器设配器和映射器
    SpringMVC(1)
    MyBatis(10)逆向工程
  • 原文地址:https://www.cnblogs.com/Running-Time/p/5286910.html
Copyright © 2011-2022 走看看