CF1549B.Gregor and the Pawn Game

入门

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

There is a chessboard of size nn by nn. The square in the ii-th row from top and jj-th column from the left is labelled (i,j)(i,j).

Currently, Gregor has some pawns in the nn-th row. There are also enemy pawns in the 11-st row. On one turn, Gregor moves one of his pawns. A pawn can move one square up (from (i,j)(i,j) to (i−1,j)(i-1,j)) if there is no pawn in the destination square. Additionally, a pawn can move one square diagonally up (from (i,j)(i,j) to either (i−1,j−1)(i-1,j-1) or (i−1,j+1)(i-1,j+1)) if and only if there is an enemy pawn in that square. The enemy pawn is also removed.

Gregor wants to know what is the maximum number of his pawns that can reach row 11?

Note that only Gregor takes turns in this game, and the enemy pawns never move. Also, when Gregor's pawn reaches row 11, it is stuck and cannot make any further moves.

有一个 n×nn \times n 的棋盘。从上往下第 ii 行、从左往右第 jj 列的方格记为 (i,j)(i,j)。

目前,Gregor 的一些兵位于第 nn 行(即最底行)。同时,敌方的兵位于第 11 行(即最顶行)。在每一轮中,Gregor 移动他的一枚兵。一枚兵可以向上移动一格(即从 (i,j)(i,j) 移动到 (i−1,j)(i-1,j)),当且仅当目标方格为空;此外,一枚兵也可以沿对角线向上移动一格(即从 (i,j)(i,j) 移动到 (i−1,j−1)(i-1,j-1) 或 (i−1,j+1)(i-1,j+1)),当且仅当目标方格中存在一枚敌方兵;此时该敌方兵将被移除。

Gregor 想知道:他的兵最多有多少枚能够到达第 11 行?

注意:本游戏中仅有 Gregor 进行回合操作,敌方兵永远不会移动。此外,当 Gregor 的一枚兵抵达第 11 行后,它将被固定在该位置,无法再进行任何移动。

输入格式

The first line of the input contains one integer tt (1≤t≤2⋅1041\le t\le 2\cdot 10^4) — the number of test cases. Then tt test cases follow.

Each test case consists of three lines. The first line contains a single integer nn (2≤n≤2⋅1052\le n\le 2\cdot{10}^{5}) — the size of the chessboard.

The second line consists of a string of binary digits of length nn, where a 11 in the ii-th position corresponds to an enemy pawn in the ii-th cell from the left, and 00 corresponds to an empty cell.

The third line consists of a string of binary digits of length nn, where a 11 in the ii-th position corresponds to a Gregor's pawn in the ii-th cell from the left, and 00 corresponds to an empty cell.

It is guaranteed that the sum of nn across all test cases is less than 2⋅1052\cdot{10}^{5}.

输入的第一行包含一个整数 tt(1≤t≤2⋅1041\le t\le 2\cdot 10^4),表示测试用例的数量。随后是 tt 个测试用例。

每个测试用例由三行组成。第一行包含一个整数 nn(2≤n≤2⋅1052\le n\le 2\cdot{10}^{5}),表示棋盘的大小。

第二行是一个长度为 nn 的二进制数字字符串,其中第 ii 位上的 11 表示从左往右数第 ii 个格子中有一个敌方兵,而 00 表示该格子为空。

第三行是一个长度为 nn 的二进制数字字符串,其中第 ii 位上的 11 表示从左往右数第 ii 个格子中有一个 Gregor 的兵,而 00 表示该格子为空。

保证所有测试用例的 nn 值之和小于 2⋅1052\cdot{10}^{5}。

输出格式

For each test case, print one integer: the maximum number of Gregor's pawns which can reach the 11-st row.

对于每个测试用例,输出一个整数:能够到达第 11 行的 Gregor 的兵的最大数量。

输入输出样例

  • 输入#1

    4
    3
    000
    111
    4
    1111
    1111
    3
    010
    010
    5
    11001
    00000

    输出#1

    3
    4
    0
    0

说明/提示

In the first example, Gregor can simply advance all 33 of his pawns forward. Thus, the answer is 33.

In the second example, Gregor can guarantee that all 44 of his pawns reach the enemy row, by following the colored paths as demonstrated in the diagram below. Remember, only Gregor takes turns in this "game"!

In the third example, Gregor's only pawn is stuck behind the enemy pawn, and cannot reach the end.

In the fourth example, Gregor has no pawns, so the answer is clearly 00.

在第一个例子中,Gregor 可以简单地将他的全部 33 个兵向前推进。因此,答案是 33。

在第二个例子中,Gregor 可以通过遵循下图中所示的彩色路径,确保他的全部 44 个兵均抵达敌方底线。请注意,在这个“游戏”中,只有 Gregor 进行回合!

在第三个例子中,Gregor 唯一的一个兵被敌方兵阻挡在后方,无法抵达底线。

在第四个例子中,Gregor 没有兵,因此答案显然是 00。

输入解题思路,AI测评打分。不知道怎么写?

首页