Gym - 102433M Maze Connect(dfs)(Pacific Northwest Regional Contest 2019)

题目

Given an orthogonal maze rotated 45 degrees and drawn with forward and backward slash characters (see below), determine the minimum number of walls that need to be removed to ensure it is possible to escape outside of the maze from every square of the (possibly disconnected) maze.

/ \

\ /

This maze has only a single square fully enclosed. Removing any wall will connect it to the outside.

/ \ . .

\ . \ .

. \ / \

. . \ /

This maze has two enclosed areas. Two walls need to be removed to connect all squares to the outside.

Input

The first line has two numbers, R and C, giving the number of rows and columns in the maze’s input description. Following this will be R lines each with C characters, consisting only of the characters ‘/’, ‘\’, and ‘.’. Both R and C are in the range 1 . . . 1 000. Define an odd (even) square as one where the sum of the x and y coordinates is odd (even). Either all forward slashes will be in the odd squares and all backslashes in the even squares, or vice versa.

Output

Output on a single line an integer indicating how many walls need to be removed so escape is possible from every square in the maze.

Examples

Sample Input 1

2 2
/\
\/

Sample Output 1

1

Sample Input 2

4 4
/\..
\.\.
.\/\
..\/

Sample Output 2

2

Sample Input 3

2 2
\/
/\

Sample Output 3

0

Sample Input 4

8 20
/\/\/\/\/\/\/\/\/\/\
\../\.\/./././\/\/\/
/./\.././\/\.\/\/\/\
\/\/\.\/\/./\/..\../
/\/./\/\/./..\/\/..\
\.\.././\.\/\/./\.\/
/.../\../..\/./.../\
\/\/\/\/\/\/\/\/\/\/

Sample Output 4

26

题意

给一个n*m的迷宫,其中.是空地,/和\是斜向45度的墙壁,问需要破坏多少个墙壁才能确保无论位于任何一个地方都可以逃出这个迷宫。

解法

dfs算出整个迷宫有n个封闭空间,答案就是n,简单的dfs题目

代码

#include<bits/stdc++.h>
using namespace std;

#define ll long long
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int MAXN = 2e5 + 5;

int t, n, m, k;

char wall[1005][1005];
int mp[1005][1005], mv[1005][1005][4];
int mvx[4] = {
    
    -1, -1, 1, 1}, mvy[4] = {
    
    -1, 1, -1, 1};
int x[4] = {
    
    0, -1, 0, 1}, y[4] = {
    
    -1, 0, 1, 0};
int ans,flag;

void dfs(int i, int j) {
    
    
	mp[i][j] = 1;
	for(int s = 0; s < 4; s++) {
    
    
		if(i + x[s] >= 1 && i + x[s] <= n+1 && j + y[s] >= 1 && j + y[s] <= m + 1) {
    
    
			if(!mp[i + x[s]][j + y[s]])dfs(i + x[s], j + y[s]);
		}else {
    
    
			flag = 0;
		}
	}
	
	for(int s = 0; s < 4; s++) {
    
    
		if(mv[i][j][s])continue;
		if(i + mvx[s] >= 1 && i + mvx[s] <= n+1 && j + mvy[s] >= 1 && j + mvy[s] <= m + 1) {
    
    
			if(!mp[i + mvx[s]][j + mvy[s]])dfs(i + mvx[s], j + mvy[s]);
		}else {
    
    
			flag = 0;
		}
	}
}

int main(void) {
    
    
	IO;
	cin >> n >> m;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> wall[i][j];
	for(int i = 1; i <= n; i++) {
    
    
		for (int j = 1; j <= m; j++) {
    
    
			if(wall[i][j] == '/') {
    
    
				mv[i][j][3] = 1;
				mv[i + 1][j + 1][0] = 1;
				mp[i + 1][j] = 1;
				mp[i][j + 1] = 1;
			}else if(wall[i][j] == '\\') {
    
    
				mv[i + 1][j][1] = 1;
				mv[i][j + 1][2] = 1;
				mp[i][j] = 1;
				mp[i + 1][j + 1] = 1;
			}
		}
	}
	
	for(int i = 1; i <= n + 1; i++) {
    
    
		for(int j = 1; j <= m + 1; j++) {
    
    
			if(mp[i][j])continue;
			flag = 1;
			dfs(i,j);
			ans+=flag;
		}
	}
	
	cout << ans << '\n';
}

猜你喜欢

转载自blog.csdn.net/qq_45682135/article/details/109167288