跳到正文

题解

题解:CF1974D Ingenuity-2

xzh7约 4 分钟
原文
文章信息与版本

文章信息

发布
2024年7月30日
收录
2025年12月12日
更新
2026年11月15日
篇幅
约 4 分钟
题目
CF1974D

版本

  1. v1当前

思路:

用 x1x_1 和 y1y_1 分别记录 Rower 的横坐标和纵坐标,然后再用 x2x_2 和 y2y_2 分别记录 Helicopter 的横坐标和纵坐标。然后对于每组数据,从前往后扫,如果扫到了 NN,那么让 Rower 和 Helicopter 中横坐标更小的那一方的横坐标加一,扫到了 SS 或 EE 或 WW 同理。这样,就可以得到 Rower 和 Helicopter 操作后的坐标,对比两个坐标,如果相等那么符合要求,否则输出 NONO 。

注意:

这道题有一个坑点,就是不能让所有操作都只让 Rower 或只让 Helicopter 进行,这里需要加一些特判。

代码:

C++
#include<bits/stdc++.h>
using namespace std;
int t,n;
string s;
int main(){
	cin>>t;
	while (t--){
		cin>>n;
		cin>>s;
		if (s=="SN" or s=="NS" or s=="WE" or s=="EW"){
			cout<<"NO"<<endl;
			continue;
		}
		int x1,x2;
		int y1,y2;
		x1=0;
		x2=0;
		y1=0;
		y2=0;
		int jj1=0,jj2=0,jj3=0,jj4=0;
		for (int i=0;i<n;i++){
			if (s[i]=='N'){
				if (x1<=x2) x1++;
				else x2++;
				jj1++;
			}
			if (s[i]=='S'){
				if (x1>x2){
					x1--;
				}
				else x2--;
				jj2++;
			}
			if (s[i]=='W'){
				if (y1>=y2){
					y1--;
				}
				else y2--;
				jj3++;
			}
			if (s[i]=='E'){
				if (y1<y2) y1++;
				else y2++;
				jj4++;
			}
		}
		if (x1!=x2 or y1!=y2){
			cout<<"NO"<<endl;
			continue;
		}
		x1=0;
		x2=0;
		y1=0;
		y2=0;
		if (jj3==jj4 and jj1==jj2){
			bool bt1=0,bt2=0;
			bool bbt1=0,bbt2=0;
			for (int i=0;i<n;i++){
				if (bt1==0 and s[i]=='N'){
					cout<<"R";
					bt1=1;
				}
				else if (s[i]=='N'){
					cout<<"H";
				}
				else if (bt2==0 and s[i]=='S'){
					cout<<"R";
					bt2=1;
				}
				else if (s[i]=='S'){
					cout<<"H";
				}
				if (bbt1==0 and s[i]=='W'){
					cout<<"H";
					bbt1=1;
				}
				else if (s[i]=='W'){
					cout<<"R";
				}
				else if (bbt2==0 and s[i]=='E'){
					cout<<"H";
					bbt2=1;
				}
				else if (s[i]=='E'){
					cout<<"R";
				}
			}
			cout<<endl;
			continue;
		}
		for (int i=0;i<n;i++){
			if (s[i]=='N'){
				if (x1<=x2) x1++,cout<<"R";
				else x2++,cout<<"H";
			}
			if (s[i]=='S'){
				if (x1>x2){
					x1--;
					cout<<"R";
				}
				else x2--,cout<<"H";
			}
			if (s[i]=='W'){
				if (y1>=y2){
					y1--;
					cout<<"R";
				}
				else y2--,cout<<"H";
			}
			if (s[i]=='E'){
				if (y1<y2) y1++,cout<<"R";
				else y2++,cout<<"H";
			}
		}
		cout<<endl;
	}
	return 0;
}
原文