【模拟】扫雷

题目:扫雷 rqnoj484

题目描述

你玩过扫雷游戏吗?这个有趣的小游戏来自于某个被人们遗忘的操作系统.游戏的目标是找出一个n×m矩阵内的所有地雷.在本题中,你需要为每个单元格统计出他周围的地雷个数.每个单元格最多有8个相邻的单元格.下图的4×4矩阵中有两个地雷,用'*'表示.计算结果为右边的矩阵:

输入格式

输入将包含若干个矩阵,对于每一个矩阵,第一行包含两个数字n和m(0<n,m<=100),分别代表这个剧真的行数和列数.接下来的n行每行包含m个字符,即该矩阵.安全区域用'.'表示,有地雷的区域用'*'表示.当n=m=0时,输入文件结束.

输出格式

对于第x个矩阵,首先在单独的一行里打印序号:'Field #x:',接下来的n行中,读入的'.'应被该位置周围的地雷数所代替.输出的每两个矩阵必须用一个空行隔开

样例输入

4 4 *... .... .*.. .... 3 5 **... ..... .*... 0 0

样例输出

Field #1: *100 2210 1*10 1110 Field #2: **100 33200 1*100

 

Pascal Code

program rqnoj484;

var
  n,m,num:longint;
  map:array[0..100+10,0..100+10] of char;
  ans:array[0..100+10,0..100+10] of longint;

procedure init;
begin
  assign(input,'rqnoj484.in');
  assign(output,'rqnoj484.out');
  reset(input);
  rewrite(output);
end;

procedure outit;
begin
  close(input);
  close(output);
  halt;
end;

procedure work;
var
  i,j:longint;
begin
  for i:=1 to n do
    for j:=1 to m do
    begin
      if map[i,j]='*' then
      begin
        ans[i,j]:=-1;
        continue;
      end;
      if map[i-1,j]='*' then inc(ans[i,j]);//上
      if map[i+1,j]='*' then inc(ans[i,j]);//下
      if map[i,j-1]='*' then inc(ans[i,j]);//左
      if map[i,j+1]='*' then inc(ans[i,j]);//右
      if map[i-1,j-1]='*' then inc(ans[i,j]);//左上
      if map[i-1,j+1]='*' then inc(ans[i,j]);//右上
      if map[i+1,j-1]='*' then inc(ans[i,j]);//左下
      if map[i+1,j+1]='*' then inc(ans[i,j]);//右下
    end;
  if num>=2 then
  begin
    writeln;
    writeln;
  end;
  writeln('Field #',num,':');
  for i:=1 to n do
  begin
    for j:=1 to m do
    begin
      if ans[i,j]=-1 then
      begin
        write('*');
        continue;
      end;
      write(ans[i,j]);
    end;
    if i<>n then writeln;
  end;
end;

procedure readdata;
var
  i,j:longint;
begin
  while true do
  begin
    readln(n,m);
    fillchar(map,sizeof(map),0);
    if (n=0)and(m=0) then outit;
    inc(num);
    for i:=1 to n do
    begin
      for j:=1 to m do
        read(map[i,j]);
      readln;
    end;
    fillchar(ans,sizeof(ans),0);
    work;
  end;
end;

begin
  init;
  readdata;
  //main;
  outit;
end.

 

 

转载于:https://www.cnblogs.com/oijzh/archive/2012/08/17/2643647.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值