Algorithm


Problem Name: beecrowd | 2552

Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2552

CheeseBreadSweeper

 

By Ricardo Oliveira, UFPR BR Brazil

Timelimit: 1

The Nlogonian Aquatic Surf Championship, to be hosted in Bonita Horeleninha (BH) city, is about to start! The game CheeseBreadSweeper is very popular in this town!

The game board consists on a matrix with N rows and M columns. Each cell contains a Cheese Bread or the number of Cheese Breads present in its adjacent cells. A cell is adjacent to another if it is immediately on the left, the right, above or bellow the cell. Please notice that, if a cell does not contain a Bread Cheese, then it must have a number from 0 to 4, inclusive.

Given the positions of the Cheese Breads, determine the game board!

 

Input

 

The input contains several test cases. The first line of each test case contains integers N and M (1 ≤ N, M ≤ 100). Next N lines contains M integers each, separated by spaces, describing the Cheese Breads on the board. The j-th integer in the i-th line is 1 if there is a Cheese Bread at row i and column j of the board, or 0 otherwise.

The input ends with end-of-file (EOF).

 

Output

 

For each test case, print N lines with M integers each, not separated by spaces, describing the board’s configuration. If a cell contains a Cheese Bread, print 9 for it; otherwise, print the number the cell must contain.

 

 

 

Input Sample Output Sample

4 4
0 0 1 1
0 1 0 1
0 0 1 0
1 1 0 1
1 2
0 1

0299
1949
1393
9939
19

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <stdio.h>
#include <stdlib.h>

int main(void)

{
    int i, j, n, x, y, count=0;

    while (scanf("%i %i", &x, &y) != EOF)
    {
        int arr[x][y];

        for (i = 0; i  <  x; ++i)
        {
            for (j = 0; j  <  y; ++j)
                scanf("%i", &arr[i][j]);
        }

        for (i = 0; i  <  x; ++i)
        {
            for (j = 0; j  <  y; ++j)
            {
                if (arr[i][j] == 1)
                    arr[i][j] = 9;
            }
        }

        for (i = 0; i  <  x; ++i)
        {
            for (j = 0; j  <  y; ++j)
            {
                if (arr[i][j] == 0)
                {
                    if (i == 0 || i == x-1 || j == 0 || j == y-1)
                    {
                        if (i == 0 && j == 0)
                        {
                            if (arr[i][j+1] == 9)
                            count++;

                            if (arr[i+1][j] == 9)
                            count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (i == 0 && j == y-1)
                        {
                            if (arr[i][j-1] == 9)
                            count++;

                            if (arr[i+1][j] == 9)
                            count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (i == x-1 && j == 0)
                        {
                            if (arr[i][j+1] == 9)
                            count++;

                            if (arr[i-1][j] == 9)
                            count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (i == x-1 && j == y-1)
                        {
                            if (arr[i][j-1] == 9)
                            count++;

                            if (arr[i-1][j] == 9)
                            count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (i == x-1)
                        {
                            if (arr[i][j+1] == 9)
                                count++;

                            if (arr[i][j-1] == 9)
                                count++;

                            if (arr[i-1][j] == 9)
                                count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (i == 0)
                        {
                            if (arr[i][j+1] == 9)
                                count++;

                            if (arr[i][j-1] == 9)
                                count++;

                            if (arr[i+1][j] == 9)
                                count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (j == y-1)
                        {
                            if (arr[i][j-1] == 9)
                                count++;

                            if (arr[i+1][j] == 9)
                                count++;

                            if (arr[i-1][j] == 9)
                                count++;

                            arr[i][j] = count;
                            count = 0;
                        }

                        else if (j == 0)
                        {
                            if (arr[i][j+1] == 9)
                                count++;

                            if (arr[i+1][j] == 9)
                                count++;

                            if (arr[i-1][j] == 9)
                                count++;

                            arr[i][j] = count;
                            count = 0;
                        }
                    }

                    else
                    {
                        if (arr[i][j+1] == 9)
                            count++;

                        if (arr[i][j-1] == 9)
                            count++;

                        if (arr[i+1][j] == 9)
                            count++;

                        if (arr[i-1][j] == 9)
                            count++;

                        arr[i][j] = count;
                        count = 0;
                    }
                }
            }
        }

        for (i = 0; i  <  x; ++i)
        {
            for (j = 0; j  <  y; ++j)
            {
                if (j == y-1)
                    printf("%i\n", arr[i][j]);

                else
                    printf("%i", arr[i][j]);
            }
        }
    }

    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 1 2 0 1

Output

x
+
cmd
0299 1949 1393 9939 19

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

#define t 102

using namespace std;

int main(void) {
    int l, c, m[t][t];
    int i, j, s;

    while (cin >> l >> c) {

        for (i = 0; i  <  t; ++i)
            for (j = 0; j  <  t; ++j)
                m[i][j] = 7;

        for (i = 1; i  <  l + 1; ++i)
            for (j = 1; j  <  c + 1; ++j)
                cin >> m[i][j];

        for (i = 1; i  <  l + 1; ++i) {
            for (j = 1; j  <  c + 1; ++j) {
                if (m[i][j] == 1)
                    cout << 9;
                else {
                    s = 0;
                    if (m[i-1][j] == 1) s += 1;
                    if (m[i+1][j] == 1) s += 1;
                    if (m[i][j-1] == 1) s += 1;
                    if (m[i][j+1] == 1) s += 1;
                    cout << s;
                }
            }
            cout << endl;
        }
    }
    return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 1 2 0 1

Output

x
+
cmd
0299 1949 1393 9939 19

#3 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;
import java.io.IOException;

public class Main {

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		
		while(sc.hasNext()){
			int n = sc.nextInt();
			int m = sc.nextInt();
			int[][] tabuleiro = new int[n][m];
			for(int i = 0 ; i  <  n ; i++)
				for(int j = 0 ; j  <  m ; j++)
					tabuleiro[i][j] = sc.nextInt();

			for(int i = 0 ; i  <  n ; i++)
				for(int j = 0 ; j  <  m ; j++)
					if(tabuleiro[i][j] == 1)
						tabuleiro[i][j] = 9;

			int somapao;
			for(int i = 0 ; i < n ; i++)
				for(int j = 0 ; j  <  m ; j++)
					if(tabuleiro[i][j]==0>{
						somapao=0;
						if(j-1 >= 0) if(tabuleiro[i][j-1]==9) somapao++;
						if(j+1 < m) if(tabuleiro[i][j+1]==9> somapao++;
						if(i-1 >= 0) if(tabuleiro[i-1][j]==9) somapao++;
						if(i+1 < n) if(tabuleiro[i+1][j]==9) somapao++;
						tabuleiro[i][j]=somapao;
					}
			
			for(int i = 0 ; i  <  n ; i++){
				for(int j = 0 ; j  <  m ; j++)
					System.out.printf("%d",tabuleiro[i][j]);
				System.out.println();
			}
		}
		sc.close(>;
	}
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 1 2 0 1

Output

x
+
cmd
0299 1949 1393 9939 19

#4 Code Example with Javascript Programming

Code - Javascript Programming


const { createReadStream } = require("node:fs")
const { createInterface } = require("node:readline")

const PATH = "/dev/stdin"
const ENCODING = "utf8"

/**
 * @param {string} path
 * @param {BufferEncoding} encoding
 */
function createReadLineInterface(path, encoding) {
	return createInterface({
		input: createReadStream(path, encoding),
		crlfDelay: Infinity,
		terminal: false
	})
}


/** @param {import("readline").Interface} readLineInterface */
const processLineByLine = function (readLineInterface) {
	let EOF = false

	const nextLineGenerator = (async function* () {
		for await (const line of readLineInterface) yield line
		EOF = true
	})()

	return {
		hasNextLine: () => !EOF,
		nextLine: async (fn) => {
			const { value } = (await nextLineGenerator.next())
			return (typeof fn === "function") ? fn(value) : value
		}
	}
}


async function main() {
	const output = []

	const RLI = createReadLineInterface(PATH, ENCODING)
	const readLineInstance = processLineByLine(RLI)
	const nextLine = readLineInstance.nextLine.bind(undefined, (n = "") => n.split(" ").map(value => Number.parseInt(value, 10)))

	while (readLineInstance.hasNextLine()) {
		const [N, M] = await nextLine()

		const tableA = Array.from({ length: N })
		const tableB = Array.from({ length: N }, () => new Array(M))

		for (let index = 0; index  <  N; index++)
			tableA[index] = (await nextLine()).slice(0, M)

		for (let i = 0; i  <  N; i++) {
			for (let j = 0; j  <  M; j++) {
				if (tableA[i][j] === 1)
					tableB[i][j] = 9
				else if (tableA[i][j] === 0)
					tableB[i][j] = Number(tableA[i][Math.max(j - 1, 0)] === 1) // HORIZONTAL RIGHT
						+ Number(tableA[i][Math.min(M - 1, j + 1)] === 1) // HORIZONTAL LEFT
						+ Number(tableA[Math.max(i - 1, 0)][j] === 1) // VERTICAL UP
						+ Number(tableA[Math.min(N - 1, i + 1)][j] === 1) // VERTICAL DOWN
				else
					throw new Error(`Invalid value in input: ${tableA[i][j]}`)
			}

			output.push(tableB[i].join(""))
		}
	}

	console.log(output.join("\n"))
}

main()
Copy The Code & Try With Live Editor

Input

x
+
cmd
4 4 0 0 1 1 0 1 0 1 0 0 1 0 1 1 0 1 1 2 0 1

Output

x
+
cmd
0299 1949 1393 9939 19
Advertisements

Demonstration


Previous
#2551 Beecrowd Online Judge Solution 2551 New Record Solution in C, C++, Java, Js and Python
Next
#2554 Beecrowd Online Judge Solution 2554 Pizza Before BH Solution in C, C++, Java, Js and Python