Algorithm
Problem Name: beecrowd | 2168
Problem Link: https://www.beecrowd.com.br/judge/en/problems/view/2168
Twilight at Portland
By M.C. Pinto, UNILA Brazil
Timelimit: 1
At Twilight the Portland city is full of vampires and werewolves. However, none of them wants to be seen while walking downtown.
At each corner of Portland downtown, one surveillance camera will be installed. A monthly updated map with all working cameras is made available at City Hall website.
A block is considered safe if there is at least two working cameras in its four corners. At Portland downtown all the blocks are squares with the same size.
Your task is, given the working cameras map, to indicate the status of all downtown blocks.
Input
The first line of input has a positive integer N (1 ≤ N ≤ 100). For the next N+1 lines there are N+1 numbers in each, indicating, for each corner, the presence or absence of a working surveillance camera. The number 1 represents a working camera at that corner, and the number zero represents there is no working camera in that corner.
Output
The output is given in N lines. Each line has N characters, indicating whether the corresponding block is safe or unsafe. If a block is safe, you must print the character S; if it is not safe, print the character U.
Input Samples | Output Samples |
1 |
U |
2 |
SU |
3 |
SSS |
Code Examples
#1 Code Example with C Programming
Code -
C Programming
#include <stdio.h>
int main(void)
{
int n, i, j, x=0;
scanf("%i", &n);
int arr[n+1][n+1];
char ar[n][n];
for (i = 0; i < = n; ++i)
{
for (j=0; j < =n; ++j)
scanf("%i", &arr[i][j]);
}
for (i = 1; i < = n; ++i)
{
for (j = 1; j < = n; ++j)
{
if (arr[i][j]==1 && arr[i][j-1]==1 || arr[i][j]==1 && arr[i-1][j]==1 || arr[i][j]==1 && arr[i-1][j-1]==1 || arr[i-1][j]==1 && arr[i][j-1]==1 || arr[i-1][j]==1 && arr[i-1][j-1]==1 || arr[i-1][j-1]==1 && arr[i][j-1]==1)
ar[i-1][j-1]='S';
else
ar[i-1][j-1]='U';
}
}
for (i = 0; i < n; ++i)
{
for (j = 0; j < n; ++j)
printf("%c", ar[i][j]);
printf("\n");
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
Output
#2 Code Example with C++ Programming
Code -
C++ Programming
#include <iostream>
int main(){
int n, i, j,sum = 0;
std::cin >> n;
int corners[n+1][n+1];
for (i = 0;i < n+1;i++) {
for (j = 0;j < n+1;j++) {
std::cin >> corners[i][j];
}
}
for (i = 0;i < n;i++) {
for (j = 0;j < n;j++) {
if ((corners[i][j]+corners[i][j+1]+corners[i+1][j+1]+corners[i+1][j]) > 1) {
std::cout << "S";
}else {
std::cout << "U";
}
}
std::cout << std::endl;
}
return 0;
}
Copy The Code &
Try With Live Editor
Input
#3 Code Example with Javascript Programming
Code -
Javascript Programming
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');
var prompt = function(texto) { return lines.shift();};
const size = parseInt(prompt()) + 1;
var blocks = [];
for (let i = 0; i < size; i++) {
let aux = prompt().split(" ").map(Number);
blocks.push(aux);
}
for (let r = 0; r < size - 1; r++) {
let aux = [];
for (let c = 1; c < size; c++) {
if (
blocks[r][c] +
blocks[r][c - 1] +
blocks[r + 1][c - 1] +
blocks[r + 1][c] >
1
) {
aux.push("S");
} else {
aux.push("U");
}
}
console.log(aux.join(""));
}
Copy The Code &
Try With Live Editor
Input
#4 Code Example with Python Programming
Code -
Python Programming
quadras = int(input())
camera = [input().split() for x in range(quadras+1)]
for x in range(quadras):
for y in range(quadras):
if(int(camera[x][y]) + int(camera[x][y+1]) + int(camera[x+1][y]) + int(camera[x+1][y+1]) < 2):
print("U", end="")
else:
print("S", end="")
print(>
Copy The Code &
Try With Live Editor
Input