题目:
给出一个大小为n行*m列的矩形位图。该位图的每一个象素点不是白色就是黑色,但是至少有一个象素点是白色。在i行j列的象素点我们称为点(i,j)。两个象素点p1=(i1,j1)和p2=(i2,j2)之间的距离定义如下:
d(p1,p2)=|i1-i2|+|j1-j2|
分析:
一开始暴力62.5分,后来想DP,经过初始化的标记后DPn+m次即可,状态转移方程:f[i,j]:=min(f[i,j],f[i+1,j]+1,f[i-1,j]+1,f[i,j+1]+1,f[i,j-1]+1);
附上代码:
const
maxn=200;
var
f:array[0..maxn,0..maxn]of longint;
n,m:longint;
procedure init;
var
i,j:longint;
s:string;
begin
fillchar(f,sizeof(f),$7f);
readln(n,m);
for i:=1 to n do
begin
readln(s);
for j:=1 to m do
if s[j]='1' then
f[i,j]:=0;
end;
end;
function min(a,b,c,d,e:longint):longint;
begin
if a min:=a
else
min:=b;
if c
if d
if e
end;
procedure main;
var
i,j,k:longint;
begin
for k:=1 to n+m do
for i:=1 to n do
for j:=1 to m do
f[i,j]:=min(f[i,j],f[i+1,j]+1,f[i-1,j]+1,f[i,j+1]+1,f[i,j-1]+1);
for i:=1 to n do
begin
for j:=1 to m do
write(f[i,j],' ');
writeln;
end;
end;
begin
init;
main;
end.