Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "nonogram.h"
- #include <string.h>
- #include <assert.h>
- int main(void) {
- int width, height;
- scanf("%d%d", &width, &height);
- //Prevents incorrect initial inputs
- if (!(width > 0 && height > 0)) {
- printf("Cannot decode\n");
- return 0;
- }
- char row[50];
- char grid[height][width];
- int limit=0;
- while(scanf("%s",row)!=EOF){
- if(strlen(row)!=width){
- printf("Invalid image data\n");
- return 0;
- }
- else if(limit >height-1){
- printf("Invalid image data\n");
- return 0;
- }
- else if(limit == height-1){
- break;
- }
- else {
- strcpy(grid[limit],row);
- limit++;
- }
- }
- if(limit != height-1){
- printf("Invalid image data\n");
- return 0;
- }
- for(int i=0;i<width;i++) {
- grid[height-1][i]=row[i];
- }
- //Time to print X
- int onecount = 0; //Keeping track of how many ones have been printed
- int prevprint = 0; //Has something been printed before? Deals with whitespace issues
- int prevdetect = 0; //Has a one been seen before?
- printf("X:\n");
- for (int i = 0; i<height; i++) {
- for (int j = width-1; j >= 0; j--) {
- if (grid[i][j]== '0') {
- if (j == width) { //beginning OR 1 not encountered yet
- continue;
- }
- if (prevdetect == 0) { //1 NOT encountered
- continue;
- }
- if (prevdetect == 1) { //1 detected previously
- if (prevprint == 0) { //Nothing printed before
- printf("%d", onecount); //Note the lack of whitespace
- prevprint = 1; //Ensures whitespace between printouts if necessary
- }
- else {
- printf(" %d", onecount);
- }
- prevdetect = 0; //Resets
- onecount = 0;
- }
- }
- else { //When encountering a one
- if (j == 0) { //end of line
- onecount++;
- if (prevprint == 0) {
- printf("%d", onecount);
- }
- else {
- printf(" %d", onecount);
- }
- }
- else {
- onecount++;
- prevdetect = 1;
- }
- }
- }
- if (prevprint == 0 && onecount ==0) {
- printf("0");
- }
- onecount = 0;
- prevdetect = 0;
- prevprint = 0;
- printf("\n");
- }
- //Works fine up to here.
- printf("\nY:\n");
- for (int i=0; i<width; i++) {
- for (int j= height - 1; j>=0; j--) {
- if (grid[j][i] == '0') {
- if (j == height - 1) { //beginning OR 1 not encountered yet
- continue;
- }
- if (prevdetect == 0) { //1 NOT encountered
- continue;
- }
- if (prevdetect == 1) { //1 detected previously
- if (prevprint == 0) { //Nothing printed before
- printf("%d", onecount);
- prevprint = 1;
- }
- else {
- printf(" %d", onecount);
- }
- prevdetect = 0; //Resets one detection
- onecount = 0; //Resets one count
- }
- }
- else { //When encountering a one
- if (j == 0) { //end of line
- onecount++;
- if (prevprint == 0) {
- printf("%d", onecount);
- }
- else {
- printf(" %d", onecount);
- }
- }
- else {
- onecount++;
- prevdetect = 1;
- }
- }
- }
- if (prevprint == 0 && onecount ==0) {
- printf("0");
- }
- onecount = 0;
- prevdetect = 0;
- prevprint = 0;
- printf("\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement