ICPC Pacific Northwest Regional Contest 2019 ------------- M

https://nanti.jisuanke.com/t/45395 

  •  5000ms
  •  262144K

Given an orthogonal maze rotated 4545 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 mazefrom 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, RR and CC, giving the number of rows and columns in the maze's input description.Following this will be RR lines each with CC characters, consisting only of the characters ‘/’, ‘\’, and ‘..’. Both RR and CC are in the range 1...1 0001...1000.Define an odd (even) square as one where the sum of the xxand yy coordinates is odd (even). Either all forward slasheswill 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 everysquare in the maze.

样例输入1复制

2 2
/\
\/

样例输出1复制

1

样例输入2复制

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

样例输出2复制

2

样例输入3复制

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

样例输出3复制

26

思路:读懂了就好做了,求被下划线包起来的空白区域个数。将下划线改成数字表示,dfs或bfs找空白区域即可。

样例一:

0110

1001

1001

0110

被1包围的区域只有一个。

#include

using namespace std;

const int N = 1000 + 10;

int st[N*2][N*2];
int mov[4][2]={1,0,-1,0,0,1,0,-1};
int n,m;

int dfs(int x, int y)
{
	st[x][y] = 1;
	for(int i=0; i<4; i++)
	{
		int  a = x + mov[i][0];
		int  b = y + mov[i][1];
		if(a>=0 && a<=n && b>=0 && b<=m && st[a][b] == 0)
			dfs(a,b);
	}
}

int main()
{	
	cin>>n>>m;	
	for(int i=1; i<=n; i++)
	{
		string s;
		cin>>s;
		for(int j=1; j<=s.size(); j++)
		{
			if(s[j-1] == '.')
			{
				st[i*2-1][j*2-1] = 0;
				st[i*2-1][j*2] = 0;
				st[i*2][j*2-1] = 0;
				st[i*2][j*2] = 0;
			}
			else if(s[j-1] == '/')
			{
				st[i*2-1][j*2-1] = 0;
				st[i*2-1][j*2] = 1;
				st[i*2][j*2-1] = 1;
				st[i*2][j*2] = 0;
			}
			else
			{
				st[i*2-1][j*2-1] = 1;
				st[i*2-1][j*2] = 0;
				st[i*2][j*2-1] = 0;
				st[i*2][j*2] = 1;
			}
		}
	}
	n *= 2;
	n++;//n行变成0-2n+1行 
	m *= 2;
	m++;
	int ans=0;
	for(int i=0; i<=n; i++)
	{
		for(int j=0; j<=m; j++)
		{
			if(st[i][j] == 0) 
			{
				ans++;
				dfs(i,j);
			}
		}
	}
	cout<

 

你可能感兴趣的:(ICPC Pacific Northwest Regional Contest 2019 ------------- M)