Competitive-Coding | algorithmic question of various online judges | Learning library
kandi X-RAY | Competitive-Coding Summary
kandi X-RAY | Competitive-Coding Summary
A repository for my solutions to hackerrank problems(& CodeChef(Please feel free to contribute.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Competitive-Coding
Competitive-Coding Key Features
Competitive-Coding Examples and Code Snippets
Community Discussions
Trending Discussions on Competitive-Coding
QUESTION
I have been trying to write a program for some question taken for competitive-coding of Cs-academy site.
The question is as following:
Suppose we were going through a bunch of black and white pictures and all of a sudden, a quirky idea struck to your mind – how many clicks does it take to convert all the white pixels into black and clear out a picture using brushes? So, we scanned and converted several pictures into binary data. Each pixel of the binary pictures are now represented by an integer, 0 for a black pixel and 1 for a white pixel.
To modify a picture, we can only use one of the three brushes each time:
+(plus) - When used, the pixels at the left, top, right and bottom of the current pixel are also colored in the same color.
x(cross) - When used, the pixels at the top-left, top-right, bottom-right and bottom-left of the current pixel are also colored in the same color.
*(star) - When used, all 9 pixels around the current pixel are also colored in the same color.
(Here is an illustration:https://publicmedia1.csacademy.com/public/1507909042-2157482298.png)
Each of these brushes, when used, works recursively. So if you color a pixel, its neighbors will get colored, then the neighbors of the neighbors, and so on.
Our task is to compute the minimum clicks you will have to perform with each brush in order to clear out a picture: First we will have to compute the minimum clicks that are needed to clear the picture using only the + brush, then compute the minimum clicks that are needed to clear the picture using only the x brush and finally compute the minimum clicks that are needed to clear the picture using only the * brush.
Input begins with a single number t (1≤t≤100) which denotes the number of pictures to process. Each test case begins with a line, which contains 2 space-separated integers w and h denoting the width and the height of the picture in pixels (1≤w,h≤1001). Following there will be h lines denoting the w pixel values (either 0 or 1) of each row of the picture.
For each test case output, 3 space separated integers which denote the minimum number of clicks needed to clear out the picture for each of the three brushes: +, x and *.
I wrote a program which calculates recursively - using separate functions - the count for each of 3 options:
...ANSWER
Answered 2019-Aug-18 at 05:36You're not reading the input correctly.
When you run cin>>mat1[i][j]
, since mat1[i][j]
is an integer the entire line of input will be read in. So for the first (small) example, mat[0][0]
will have the value 1011001 (one million, eleven thousand, and one). What you should be doing is reading the input matrix a character at a time, then storing an appropriate value in your matrices, taking care to handle the newline correctly (and checking for errors in the input).
Another problem is that you don't output any spaces, so all three numbers run together to form one bigger number.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Competitive-Coding
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page