2019 google code jam qualification round ques no 2 solution
YOU CAN GO YOUR OWN WAY
Problem
You have just entered the world's easiest maze. You start in the northwest cell of an N by N grid of unit cells, and you must reach the southeast cell. You have only two types of moves available: a unit move to the east, and a unit move to the south. You can move into any cell, but you may not make a move that would cause you to leave the grid.
You are excited to be the first in the world to solve the maze, but then you see footprints. Your rival, Labyrinth Lydia, has already solved the maze before you, using the same rules described above!
As an original thinker, you do not want to reuse any of Lydia's moves. Specifically, if her path includes a unit move from some cell A to some adjacent cell B, your path cannot also include a move from A to B. (However, in that case, it is OK for your path to visit A or visit B, as long as you do not go from A to B.) Please find such a path.
In the following picture, Lydia's path is indicated in blue, and one possible valid path for you is indicated in orange:
Input
The first line of the input gives the number of test cases, T. T test cases follow; each case consists of two lines. The first line contains one integer N, giving the dimensions of the maze, as described above. The second line contains a string P of 2N - 2 characters, each of which is either uppercase
E
(for east) or uppercase S
(for south), representing Lydia's valid path through the maze.Output
For each test case, output one line containing
Case #x: y
, where x
is the test case number (starting from 1) and y
is a string of 2N - 2 characters each of which is either uppercase E
(for east) or uppercase S
(for south), representing your valid path through the maze that does not conflict with Lydia's path, as described above. It is guaranteed that at least one answer exists.Limits
1 ≤ T ≤ 100.
Time limit: 15 seconds per test set.
Memory limit: 1GB.
P contains exactly N - 1
Time limit: 15 seconds per test set.
Memory limit: 1GB.
P contains exactly N - 1
E
characters and exactly N - 1 S
characters.Test set 1 (Visible)
2 ≤ N ≤ 10.
Test set 2 (Visible)
2 ≤ N ≤ 1000.
Test set 3 (Hidden)
For at most 10 cases, 2 ≤ N ≤ 50000.
For all other cases, 2 ≤ N ≤ 10000.
For all other cases, 2 ≤ N ≤ 10000.
Sample
Input | Output |
2 2 SE 5 EESSSESE | Case #1: ES Case #2: SEEESSES |
In Sample Case #1, the maze is so small that there is only one valid solution left for us.
Sample Case #2 corresponds to the picture above. Notice that it is acceptable for the paths to cross.
SOLUTION:(C++)
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
int T; cin >> T;
for (int case_num = 1; case_num <= T; case_num ++) {
int N; cin >> N;
string S; cin >> S;
string V;
for (char c : S) {
V += (c ^ 'S' ^ 'E');
}
cout << "Case #" << case_num << ": " << V << '\n';
}
return 0;
}
PYTHON:
import sys
if __name__ == "__main__":
f = sys.stdin
if len(sys.argv) >= 2:
fn = sys.argv[1]
if fn != '-':
f = open(fn)
T = int(f.readline())
for _T in xrange(T):
N = int(f.readline())
S = f.readline().strip()
ans = []
for c in S:
if c == 'S':
ans.append('E')
else:
ans.append('S')
ans = ''.join(ans)
print "Case #%d: %s" % (_T + 1, ans)
Comments
Follow for more...