跳到正文

题解

题解 P2670 【扫雷游戏】

Sooke约 1 分钟
原文
文章信息与版本

文章信息

发布
2016年12月10日
收录
2025年12月12日
更新
2026年11月17日
篇幅
约 1 分钟
获赞
25
题目
P2670

版本

  1. v1当前

这题用暴力的确很可以,我用的是如果输入了地雷就将周围的数增加一的方法。

pascal
 var
 n,m,i,j,x,y:longint;
 a:array[0..101,0..101] of integer; //这里的数组原本是1..100的,为了防止越界这样设
 c:char;
begin
 readln(n,m);
 for x:= 1 to n do for y:= 1 to m do begin
  read(c); if (y = m) and (x < n) then readln; //输入字符,进行处理
  if c = '*' then begin
   a[x,y]:=-10;
   //如果是地雷,就把自己在数组里的整数设为-10,突显自己是地雷
   for i:= -1 to 1 do for j:= -1 to 1 do inc(a[x+i,y+j]); 
   //不管四周是什么,都增加1(无须担心数组越界)
  end;
 end;
 for x:= 1 to n do begin
  for y:= 1 to m do
   if a[x,y] < 0 then write('*') else write(a[x,y]);
   writeln;
  //合理输出,如果仍有负数,就当作是地雷输出,否则直接输出自然数,另外注意换行
 end;
end.