Volleyball Positions

Description:

You are watching a volleyball tournament, but you missed the beginning of the very first game of your favorite team. Now you’re curious about how the coach arranged the players on the field at the start of the game.

The team you favor plays in the following formation:

0 3 0
4 0 2
0 6 0
5 0 1

where positive numbers represent positions occupied by players. After the team gains the serve, its members rotate one position in a clockwise direction, so the player in position 2 moves to position 1, the player in position 3 moves to position 2, and so on, with the player in position 1 moving to position 6.

Here’s how the players change their positions:

Given the current formation of the team and the number of times k it gained the serve, find the initial position of each player in it.

Example

  • For
    formation = [["empty",   "Player5", "empty"],
                 ["Player4", "empty",   "Player2"],
                 ["empty",   "Player3", "empty"],
                 ["Player6", "empty",   "Player1"]]
    

    and k = 2, the output should be

    volleyballPositions(formation, k) = [
        ["empty",   "Player1", "empty"],
        ["Player2", "empty",   "Player3"],
        ["empty",   "Player4", "empty"],
        ["Player5", "empty",   "Player6"]
    ]
    
  • For
    formation = [["empty", "Alice", "empty"],
                 ["Bob",   "empty", "Charlie"],
                 ["empty", "Dave",  "empty"],
                 ["Eve",   "empty", "Frank"]]
    

    and k = 6, the output should be

    volleyballPositions(formation, k) = [
        ["empty", "Alice", "empty"],
        ["Bob",   "empty", "Charlie"],
        ["empty", "Dave",  "empty"],
        ["Eve",   "empty", "Frank"]
    ]
    

Input/Output

  • [time limit] 3000ms (cs)
  • [input] array.array.string formation

    A 4 × 3 array of strings representing names of the players in the positions corresponding to those in the schema above.
    It is guaranteed that for each empty position the corresponding element of formation is "empty".
    It is also guaranteed that there is no player called "empty" in the team.

  • [input] integer k

    The number of times the team gained the serve.

    Constraints:
    0 ≤ k ≤ 109.

  • [output] array.array.string

    Team arrangement at the start of the game.

Tests:
Solution(C#):

string[][] volleyballPositions(string[][] formation, int k) {
    List<Point> rotates = new List<Point>{
        new Point(3, 2, 1, 2), 
        new Point(1, 2, 0, 1), 
        new Point(0, 1, 1, 0), 
        new Point(1, 0, 3, 0), 
        new Point(3, 0, 2, 1), 
        new Point(2, 1, 3, 2)};
    string[][] pre = formation.Select(a => a.ToArray()).ToArray();
    string[][] result = formation.Select(a => a.ToArray()).ToArray();
    k = k % 6;
    if (k < 1) {
        return formation;
    }
    for (int i = 1;i<=k;i++) {
        foreach(Point rotate in rotates) {
            int x1 = rotate.x1, y1 = rotate.y1, x2 = rotate.x2, y2 = rotate.y2;
            result[x2][y2] = pre[x1][y1];
        }
        pre = result.Select(a => a.ToArray()).ToArray();
    }
    return result;
}
public class Point
{
    public Point(int x1,int y1,int x2, int y2) {
        this.x1=x1;
        this.x2=x2;
        this.y1=y1;
        this.y2=y2;
    }
    public int x1{get;set;}
    public int x2{get;set;}
    public int y1{get;set;}
    public int y2{get;set;}
}

Advertisements

One thought on “Volleyball Positions

  1. String[][] volleyballPositions(String[][] formation, int k) {
    int[][] koord = {{1,0},{0,1},{1,2},{3,2},{2,1},{3,0}};
    String[] tmp = new String[6];
    if (k%6==0) return formation;
    for (int i=0;i<6;i++) tmp[i]=formation[koord[i][0]][koord[i][1]];
    for(int i=0;i<6;i++) formation[koord[i][0]][koord[i][1]]=tmp[(i+k)%6];
    return formation;
    }

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s