Make Three Regions

Make Three Regions

题面翻译

t t t 组数据,每组数据给定一个 2 × n 2 \times n 2×n 的矩阵,每个格点只有可能是 .x 的一种,保证所有的 . 构成一个联通块。

你需要输出符合下面条件的格点个数:

  • 该格点为 .

  • 若将该格点的 . 改为 x,则剩下的 . 被分成三个联通块。

1 ≤ t ≤ 1 0 4 , 1 ≤ ∑ n ≤ 2 × 1 0 5 1 \le t \le 10^4, 1 \le \sum n \le 2 \times 10^5 1t104,1n2×105

translated by Ray662

题目描述

There is a grid, consisting of $ 2 $ rows and $ n $ columns. Each cell of the grid is either free or blocked.

A free cell $ y $ is reachable from a free cell $ x $ if at least one of these conditions holds:

  • $ x $ and $ y $ share a side;
  • there exists a free cell $ z $ such that $ z $ is reachable from $ x $ and $ y $ is reachable from $ z $ .

A connected region is a set of free cells of the grid such that all cells in it are reachable from one another, but adding any other free cell to the set violates this rule.

For example, consider the following layout, where white cells are free, and dark grey cells are blocked:

There are $ 3 $ regions in it, denoted with red, green and blue color respectively:

The given grid contains at most $ 1 $ connected region. Your task is to calculate the number of free cells meeting the following constraint:

  • if this cell is blocked, the number of connected regions becomes exactly $ 3 $ .

输入格式

The first line contains a single integer $ t $ ( $ 1 \le t \le 10^4 $ ) — the number of test cases.

The first line of each test case contains a single integer $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the number of columns.

The $ i $ -th of the next two lines contains a description of the $ i $ -th row of the grid — the string $ s_i $ , consisting of $ n $ characters. Each character is either . (denoting a free cell) or x (denoting a blocked cell).

Additional constraint on the input:

  • the given grid contains at most $ 1 $ connected region;
  • the sum of $ n $ over all test cases doesn’t exceed $ 2 \cdot 10^5 $ .

输出格式

For each test case, print a single integer — the number of cells such that the number of connected regions becomes $ 3 $ if this cell is blocked.

样例 #1

样例输入 #1

4
8
.......x
.x.xx...
2
..
..
3
xxx
xxx
9
..x.x.x.x
x.......x

样例输出 #1

1
0
0
2

提示说明

In the first test case, if the cell $ (1, 3) $ is blocked, the number of connected regions becomes $ 3 $ (as shown in the picture from the statement).

代码内容

// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const ll N=2e5+10;
char a[N][2];

int main()
{
    ll t;
    cin>>t;
    
    while(t--)
    {
        ll n;
        cin>>n;
        
        for(ll i=1;i<=2;i++)
            for(ll j=1;j<=n;j++)
                cin>>a[j][i];
        
        ll ans=0;
        for(ll i=2;i<n;i++)
        {
            if(a[i][1]=='.')
            {
                if(a[i][2]=='.'&&a[i-1][2]=='x'&&a[i+1][2]=='x'&&a[i-1][1]=='.'&&a[i+1][1]=='.')
                    ans++;
            }
        }
        
        for(ll i=2;i<n;i++)
        {
            if(a[i][2]=='.')
            {
                if(a[i-1][1]=='x'&&a[i+1][1]=='x'&&a[i][1]=='.'&&a[i-1][2]=='.'&&a[i+1][2]=='.')
                    ans++;
            }
        }
        
        cout<<ans<<endl;
    }
    
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Pretty Boy Fox

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值