题目:扫雷 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.