其他分享
首页 > 其他分享> > 题解 CF1549B 【Gregor and the Pawn Game】

题解 CF1549B 【Gregor and the Pawn Game】

作者:互联网

CF1549B Gregor and the Pawn Game

题目大意:

给定一个 \(N \times N\) 的棋盘,第一行有若干敌方棋子,第 \(N\) 行有若干己方棋子,有两种走方式,问有多少己方棋子能到第一行。

行走方式:

  1. 若前方无棋子可直线走。
  2. 斜着走并吃敌方棋子。

solution :

贪心的想,能直线走就直线走;若不能,以左侧优先。

细节处理:

代码
#include<cstdio>
#include<cstring>
using namespace std;
const int N=2e5+5;
char a[N],b[N];
bool vis[N];
int main(){
	int T;scanf("%d",&T);
	while(T--){
		memset(vis,0,sizeof(vis));
		int n; scanf("%d",&n);
		scanf("%s%s",a+1,b+1);
		int ans=0;
		for(int i=1;i<=n;i++){
			if(b[i]=='1'&&a[i]=='0') ans++;//能直走就直走
			else if(b[i]=='1'&&a[i]=='1'){//不能
				if(i>1&&a[i-1]=='1'&&!vis[i-1]){//左侧
					ans++;
					vis[i-1]=1;
				}
				else if(i<n&&a[i+1]=='1'&&!vis[i+1]){//右侧
					ans++;
					vis[i+1]=1;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

End

标签:int,题解,Gregor,vis,Game,棋子,ans,scanf
来源: https://www.cnblogs.com/TSZ-think/p/15112716.html