Straight forward method. I did not work out a funnier solution.
Code:
- /*************************************************************************
- * Copyright (C) 2008 by liukaipeng *
- * liukaipeng at gmail dot com *
- *************************************************************************/
- /* @JUDGE_ID 00000 10189 C "Minesweeper" */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <strings.h>
- #define MAX 102
- int around(int mat[][MAX], int n, int m)
- {
- return mat[n-1][m-1] + mat[n-1][m] + mat[n-1][m+1]
- + mat[n][m-1] + mat[n][m+1]
- + mat[n+1][m-1] + mat[n+1][m] + mat[n+1][m+1];
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- char in[256];
- char out[256];
- strcpy(in, argv[0]);
- strcat(in, ".in");
- strcpy(out, argv[0]);
- strcat(out, ".out");
- close(0);
- open(in, O_RDONLY);
- close(1);
- open(out, O_WRONLY | O_CREAT, 0600);
- #endif
- int mat[MAX][MAX];
- int n, m, i, j, f = 1;
- char c;
- for (scanf("%d %d", &n, &m); n != 0 && m != 0; scanf("%d %d", &n, &m)) {
- if (f != 1)
- putchar('/n');
- bzero(mat, MAX*MAX*sizeof(int));
- c = getchar();
- for (i = 1; i <= n; ++i) {
- for (j = 1; j <= m; ++j) {
- c = getchar();
- if (c == '*') mat[i][j] = 1;
- else if (c == '.') mat[i][j] = 0;
- }
- c = getchar();
- }
- printf("Field #%d:/n", f++);
- for (i = 1; i <= n; ++i) {
- for (j= 1; j <= m; ++j) {
- if (mat[i][j] == 1) putchar('*');
- else printf("%d", around(mat, i, j));
- }
- putchar('/n');
- }
- }
- return 0;
- }
本文介绍了一种用于扫雷游戏的简单直接算法实现。通过计算矩阵中每个元素周围八个位置的雷数来标记雷区,适用于任何大小的游戏地图。文章包含完整的C语言代码示例,适合初学者学习。
356

被折叠的 条评论
为什么被折叠?



