Codeforces 510A Fox And Snake Solution & Explanation

Difficulty : 800

Problem Description

Fox Ciel starts to learn programming. The first task is drawing a fox! However, that turns out to be too hard for a beginner, so she decides to draw a snake instead.

A snake is a pattern on a n by m table. Denote c-th cell of r-th row as (r, c). The tail of the snake is located at (1, 1), then it’s body extends to (1, m), then goes down 2 rows to (3, m), then goes left to (3, 1) and so on.

Your task is to draw this snake for Fox Ciel: the empty cells should be represented as dot characters (‘.’) and the snake cells should be filled with number signs (‘#’).

Consider sample tests in order to understand the snake pattern.

Input

The only line contains two integers: n and m (3 ≤ n, m ≤ 50).

n is an odd number.

Output

Output n lines. Each line should contain a string consisting of m characters. Do not output spaces.

Examples

Input3 3
Output###
. . #
###
Input3 4
Output####
. . . #
####
Input5 3
Output###
. . #
###
# . .
###
Input9 9
Output#########
. . . . . . . . #
#########
# . . . . . . . .
#########
. . . . . . . . #
#########
# . . . . . . . .
#########

Solution

In our solution, we utilize the modulo operator (%) to determine whether the current line count indicates a segment of the snake’s body or a turning point.

  • If the line count is odd, it signifies the snake’s body, so we fill the entire line with ‘#’ characters.
  • If the line count is even, we check whether it’s divisible by 4 to determine if it’s a turning point. If it is, we place a ‘#’ at the beginning of the line and fill the rest with ‘.’ characters. Otherwise, we fill the line with ‘.’ characters and place a ‘#’ at the end.

C# Solution

Solution1

string[] first = Console.ReadLine().Split(' '); //split by space
int n = int.Parse(first[0]); 
int m = int.Parse(first[1]); 


for(int i=1; i<=n; i++){
    var tmp = "";
    if(i%2 >0){
        tmp = new string('#', m);
    }
    else{
        if(i%4==0){
            tmp = "#"+new string('.', m-1);
        }
        else{
             tmp = new string('.', m-1)+"#";
        }
    }
    Console.WriteLine(tmp);
}

Java Solution

Solution1 (Java 11+)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String[] first = scanner.nextLine().split(" "); // split by space
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        
        for(int i=1; i<=n; i++){
            String tmp = "";
            if(i%2 >0){
                tmp = "#".repeat(m);
            }
            else{
                if(i%4==0){
                    tmp = "#"+".".repeat(m-1);
                }
                else{
                     tmp = ".".repeat(m-1)+"#";
                }
            }
             System.out.println(tmp);
        }
    }
}

In Java 11, there is a built-in function – String’s repeat(), which we can use to easily solve this problem.

Solution2 (before Java11)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        String[] first = scanner.nextLine().split(" "); // split by space
        int n = Integer.parseInt(first[0]);
        int m = Integer.parseInt(first[1]);
        
        for(int i=1; i<=n; i++){
            String tmp = "";
            if(i%2 >0){
                tmp = getRepeatString('#',m);
            }
            else{
                if(i%4==0){
                    tmp = "#"+getRepeatString('.',m-1);
                }
                else{
                     tmp = getRepeatString('.',m-1)+"#";
                }
            }
             System.out.println(tmp);
        }
    }
    
    private static String getRepeatString(Character chr, int times){
        StringBuilder tmp = new StringBuilder();
        for (int i = 0; i < times; i++) {
            tmp.append(chr);
        }
        return tmp.toString();
    }
}

And for versions before Java 11 (such as Java 8), we need to repeat the string manually.


Conclusion

Drawing a snake is easier than drawing a fox; I agree with that!

Reference

🧡If my solution helps, that is my honor!

🧡You can support me by sharing my posts, thanks you~~

✅If you got any problem about the explanation or you need other programming language solution, please feel free to let me know !!

The problem link : Problem – 510A – Codeforces

Random Codeforces Posts

Leave a Reply

Your email address will not be published. Required fields are marked *