fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define prt(x) {for (int i = 0; i < w; ++i)putchar(x);putchar('\n');}
  4. void pp(char *a, int h, int w)
  5. {
  6. prt('-')
  7. for (int i = 0; i < h; ++i)prt(*a++);
  8. prt('-')
  9. }
  10. int main()
  11. {
  12. int h, w;
  13. printf("H W = ");
  14. scanf("%d %d", &h, &w);
  15. char *a = (char *)malloc(h * w * (sizeof(char)));
  16. for (int i = 0; i < h * w; ++i)
  17. a[i] = '0';
  18. for (int i = 0; i < h * w / 5; ++i)
  19. {
  20. int x = rand() % h;
  21. int y = rand() % w;
  22. a[x * w + y] = '*';
  23. }
  24. pp(a, h, w);
  25. for (int i = 0; i < h; ++i)
  26. {
  27. for (int j = 0; j < w; ++j)
  28. {
  29. int ij = i * w + j;
  30. if (a[ij] == '*')continue;
  31. for (int di = -1; di < 2; ++di)
  32. {
  33. int xx = i + di;
  34. if ((xx < 0) || (xx >= h))continue;
  35. for (int dj = -1; dj < 2; ++dj)
  36. {
  37. if ((di + dj) % 2 == 0)continue;
  38. int yy = j + dj;
  39. if ((yy < 0) || (yy >= w))continue;
  40. a[ij] += (a[xx * w + yy] == '*');
  41. }
  42. }
  43. }
  44. }
  45. pp(a, h, w);
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0s 2428KB
stdin
10 20
stdout
H W = --------------------
000000*000000000000*
*0*00000*0000000000*
000*0*0*00*00000*0*0
000000*000*0000***00
0000*00*0000000*0*00
00000*0000*000000000
0*000*000*00**000000
000000000*00000*0000
0000*000000000000000
0**0000*00000*000000
--------------------
--------------------
101001*110000000001*
*2*20112*1100000102*
102*2*3*21*10002*3*2
000112*301*1001***20
0001*22*1020001*3*10
01002*1102*111010100
1*101*101*21**110000
010011001*10111*1000
0111*101010001010000
1**1101*10001*100000
--------------------