UVa Problem Solution: 10189 - Minesweeper

来源:互联网 发布:淘宝店铺登录名 编辑:程序博客网 时间:2024/04/25 18:04

Straight forward method. I did not work out a funnier solution.

Code:
  1. /*************************************************************************
  2.  * Copyright (C) 2008 by liukaipeng                                      *
  3.  * liukaipeng at gmail dot com                                           *
  4.  *************************************************************************/
  5. /* @JUDGE_ID 00000 10189 C "Minesweeper" */
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <strings.h>
  13.                
  14. #define MAX 102
  15. int around(int mat[][MAX], int n, int m)
  16. {
  17.   return mat[n-1][m-1] + mat[n-1][m] + mat[n-1][m+1]
  18.     + mat[n][m-1] + mat[n][m+1]
  19.     + mat[n+1][m-1] + mat[n+1][m] + mat[n+1][m+1];
  20. }
  21. int main(int argc, char *argv[])
  22. {
  23. #ifndef ONLINE_JUDGE
  24.   char in[256];
  25.   char out[256];
  26.   strcpy(in, argv[0]);
  27.   strcat(in, ".in");
  28.   strcpy(out, argv[0]);
  29.   strcat(out, ".out");
  30.   close(0); 
  31.   open(in, O_RDONLY);
  32.   close(1);
  33.   open(out, O_WRONLY | O_CREAT, 0600);
  34. #endif
  35.   
  36.   int mat[MAX][MAX];
  37.   int n, m, i, j, f = 1;
  38.   char c;
  39.   for (scanf("%d %d", &n, &m); n != 0 && m != 0; scanf("%d %d", &n, &m)) {
  40.     if (f != 1)
  41.       putchar('/n');
  42.     bzero(mat, MAX*MAX*sizeof(int));
  43.     c = getchar();
  44.     for (i = 1; i <= n; ++i) {
  45.       for (j = 1; j <= m; ++j) {
  46.         c = getchar();
  47.         if (c == '*') mat[i][j] = 1;
  48.         else if (c == '.') mat[i][j] = 0;
  49.       }
  50.       c = getchar();
  51.     }
  52.     printf("Field #%d:/n", f++);
  53.     for (i = 1; i <= n; ++i) {
  54.       for (j= 1; j <= m; ++j) {
  55.         if (mat[i][j] == 1) putchar('*');
  56.         else printf("%d", around(mat, i, j));
  57.       }
  58.       putchar('/n');
  59.     }
  60.   }
  61.   
  62.   return 0;
  63. }

原创粉丝点击