码迷,mamicode.com
首页 > 其他好文 > 详细

dotcpp1096 Minesweeper fillflood

时间:2018-11-23 20:34:01      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:nod   span   code   set   des   while   sage   空白   process   

问题 1096: Minesweeper

时间限制: 1Sec 内存限制: 64MB 提交: 581 解决: 254

题目描述
Minesweeper Have you ever played Minesweeper? This cute little game comes with a certain operating system whose name we can‘t remember. The goal of the game is to find where all the mines are located within a M x N field. The game shows a number in a square which tells you how many mines there are adjacent to that square. Each square has at most eight adjacent squares. The 4 x 4 field on the left contains two mines, each represented by a ``*‘‘ character. If we represent the same field by the hint numbers described above, we end up with the field on the right: *... .... .*.. .... *100 2210 1*10 1110
输入
The input will consist of an arbitrary number of fields. The first line of each field contains two integers n and m ( 0 < n, m$ \le$100) which stand for the number of lines and columns of the field, respectively. Each of the next n lines contains exactly m characters, representing the field. Safe squares are denoted by ``.‘‘ and mine squares by ``*,‘‘ both without the quotes. The first field line where n = m = 0 represents the end of input and should not be processed.
输出
For each field, print the message Field #x: on a line alone, where x stands for the number of the field starting from 1. The next n lines should contain the field with the ``.‘‘ characters replaced by the number of mines adjacent to that square. There must be an empty line between field outputs.
样例输入
4 4
*...
....
.*..
....
3 5
**...
.....
.*...
0 0
样例输出
Field #1:
*100
2210
1*10
1110

Field #2:
**100
33200
1*100

#include<cstdio>
#include<iostream>
#include<vector>
#include<string.h>
using namespace std;
char pic[1001][1001];
int vis[1001][1001];
int dx[]={0,0,1,-1,1,-1,1,-1};
int dy[]={1,-1,0,0,1,-1,-1,1};
int h,w;
struct node{
    int i;
    int j;
};
vector<node>v;//
vector<node>l;//空白

void print(int i,int j){//扫描雷
    for(int k=0;k<8;k++){
        int tmpi=i+dy[k],tmpj=j+dx[k];
        if(tmpi>=0 && tmpi<h && tmpj>=0 && tmpj<w)
            vis[tmpi][tmpj]++;
    }
    return;
}

void printb(int i,int j){//扫描空白
    int ans=0;
    for(int k=0;k<8;k++){
        int tmpi=i+dy[k],tmpj=j+dx[k];
        if(tmpi>=0 && tmpi<h && tmpj>=0 && tmpj<w && pic[tmpi][tmpj]==*)
            ans++;
    }
    vis[i][j]=ans;
    return;
}

int main(){
    int tot=1;
    while(cin>>h>>w && (h+w)!=0){
        memset(vis,0,sizeof(vis));
        v.clear();
        l.clear();
        int tota=0,totb=0;//a雷b空白初始化
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                cin>>pic[i][j];
                if(pic[i][j]==*) {node cc;cc.i=i;cc.j=j;v.push_back(cc);tota++;}
                if(pic[i][j]==.) {node cc;cc.i=i;cc.j=j;l.push_back(cc);totb++;}
            }
        }
        printf("Field #%d:\n",tot++);
        if(tota>totb){
            for(int i=0;i<l.size();i++){
                printb(l[i].i,l[i].j);
            }
        }else{
            for(int i=0;i<v.size();i++){
                print(v[i].i,v[i].j);
            }
        }
        for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                if(pic[i][j]==*) printf("*");
                else printf("%d",vis[i][j]);
            }
            printf("\n");
        }
        printf("\n");
    }
    return 0;
}

 

dotcpp1096 Minesweeper fillflood

标签:nod   span   code   set   des   while   sage   空白   process   

原文地址:https://www.cnblogs.com/rign/p/10009236.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!