@mihailos

Как решить данную задачку?

Есть задача
There is a binary string consisting of characters '0' and '1'. Initially, Bob is facing East when he starts reading the binary string. When he sees a '0', he turns 90∘ to his right. When he sees a '1', he turns 90∘ to his left.

The four directions are as follows: East is denoted by 'E', West by 'W', North by 'N', and South by 'S'.

After reading the entire string, which direction will Bob face?

Input
The first line contains an integer t (1≤t≤102) − the number of test cases in the input.

Then t test cases follow.

The first line of the test case contains one integer n (1≤n≤105).

The second line of the test case contains a string s of n characters, consisting only '0' and '1'.

It is guaranteed that the sum of the values of n for all test cases in the input does not exceed 2⋅105.

Output
For each test case, Print a single character — the direction Bob is facing after reading the string.
Пример ввода
3
4
1010
5
00000
3
111
Пример вывода
E
S
S
Есть решение, но оно не работает:
#include <iostream>

using namespace std;

int main(){
	char a[4] = {'E', 'S', 'W', 'N'};
	int t, n;
	char s;
	cin >> t;
	int pos=0;
	for(int i = 0; i < t; i++){
		cin >> n;
		for(int i = 0; i < n; i++){
			cin >> s;
			if(s == '0') pos-=1;
			else if(s == '1') pos+=1;
		}
		cout << a[pos];
		pos = 0;
	}
}

Оно не работает, потому что я использую отрицательный индекс.
Все мои мысли приводят только к этому решению.
Помогите, кто сможет.
  • Вопрос задан
  • 164 просмотра
Пригласить эксперта
Ответы на вопрос 1
wataru
@wataru Куратор тега C++
Разработчик на С++, экс-олимпиадник.
Если он указывает на "E" и встретил 0, куда он будет указывать после поворота? На "N".
Аналогично, в обратную сторону, при указывании на " N" после 1, он будет указывать на "E".

Чему это соответствует в вашем массиве a? Что происходит с индексом?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы