Advertisement
Guest User

Vic's nonogram

a guest
Mar 28th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "nonogram.h"
  4. #include <string.h>
  5. #include <assert.h>
  6.  
  7. int main(void) {
  8.  
  9. int width, height;
  10.  
  11. scanf("%d%d", &width, &height);
  12.  
  13. //Prevents incorrect initial inputs
  14. if (!(width > 0 && height > 0)) {
  15. printf("Cannot decode\n");
  16. return 0;
  17. }
  18.  
  19. char row[50];
  20. char grid[height][width];
  21.  
  22. int limit=0;
  23. while(scanf("%s",row)!=EOF){
  24.  
  25. if(strlen(row)!=width){
  26. printf("Invalid image data\n");
  27. return 0;
  28. }
  29.  
  30. else if(limit >height-1){
  31. printf("Invalid image data\n");
  32. return 0;
  33. }
  34.  
  35. else if(limit == height-1){
  36. break;
  37. }
  38.  
  39. else {
  40. strcpy(grid[limit],row);
  41. limit++;
  42. }
  43. }
  44.  
  45. if(limit != height-1){
  46. printf("Invalid image data\n");
  47. return 0;
  48. }
  49.  
  50. for(int i=0;i<width;i++) {
  51. grid[height-1][i]=row[i];
  52. }
  53.  
  54.  
  55. //Time to print X
  56. int onecount = 0; //Keeping track of how many ones have been printed
  57. int prevprint = 0; //Has something been printed before? Deals with whitespace issues
  58. int prevdetect = 0; //Has a one been seen before?
  59.  
  60. printf("X:\n");
  61.  
  62. for (int i = 0; i<height; i++) {
  63. for (int j = width-1; j >= 0; j--) {
  64. if (grid[i][j]== '0') {
  65.  
  66. if (j == width) { //beginning OR 1 not encountered yet
  67. continue;
  68. }
  69. if (prevdetect == 0) { //1 NOT encountered
  70. continue;
  71. }
  72. if (prevdetect == 1) { //1 detected previously
  73.  
  74. if (prevprint == 0) { //Nothing printed before
  75. printf("%d", onecount); //Note the lack of whitespace
  76. prevprint = 1; //Ensures whitespace between printouts if necessary
  77. }
  78. else {
  79. printf(" %d", onecount);
  80. }
  81.  
  82. prevdetect = 0; //Resets
  83. onecount = 0;
  84.  
  85. }
  86.  
  87.  
  88. }
  89.  
  90. else { //When encountering a one
  91. if (j == 0) { //end of line
  92. onecount++;
  93. if (prevprint == 0) {
  94. printf("%d", onecount);
  95. }
  96.  
  97. else {
  98. printf(" %d", onecount);
  99. }
  100.  
  101. }
  102.  
  103. else {
  104. onecount++;
  105. prevdetect = 1;
  106.  
  107. }
  108.  
  109.  
  110. }
  111. }
  112.  
  113. if (prevprint == 0 && onecount ==0) {
  114. printf("0");
  115. }
  116.  
  117. onecount = 0;
  118. prevdetect = 0;
  119. prevprint = 0;
  120. printf("\n");
  121.  
  122. }
  123.  
  124. //Works fine up to here.
  125. printf("\nY:\n");
  126. for (int i=0; i<width; i++) {
  127. for (int j= height - 1; j>=0; j--) {
  128. if (grid[j][i] == '0') {
  129. if (j == height - 1) { //beginning OR 1 not encountered yet
  130. continue;
  131. }
  132.  
  133. if (prevdetect == 0) { //1 NOT encountered
  134. continue;
  135. }
  136.  
  137. if (prevdetect == 1) { //1 detected previously
  138.  
  139. if (prevprint == 0) { //Nothing printed before
  140. printf("%d", onecount);
  141. prevprint = 1;
  142. }
  143.  
  144. else {
  145. printf(" %d", onecount);
  146. }
  147.  
  148. prevdetect = 0; //Resets one detection
  149. onecount = 0; //Resets one count
  150.  
  151. }
  152.  
  153. }
  154. else { //When encountering a one
  155. if (j == 0) { //end of line
  156. onecount++;
  157. if (prevprint == 0) {
  158. printf("%d", onecount);
  159. }
  160.  
  161. else {
  162. printf(" %d", onecount);
  163. }
  164.  
  165. }
  166.  
  167. else {
  168. onecount++;
  169. prevdetect = 1;
  170.  
  171. }
  172. }
  173. }
  174. if (prevprint == 0 && onecount ==0) {
  175. printf("0");
  176. }
  177.  
  178. onecount = 0;
  179. prevdetect = 0;
  180. prevprint = 0;
  181. printf("\n");
  182. }
  183.  
  184.  
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement