2017.07.08【NOIP提高組】模擬賽B組 山峰(summits) 題解

原題:

http://172.16.0.132/senior/#contest/show/2041/2

題目描述:

作為地質(zhì)學(xué)家的JIH,為了繪制地圖進(jìn)行了野外考察。考察結(jié)束,他得到了一張n*m的地面高度地圖。為了科學(xué)研究,JIH定義了一種山峰叫做d-山峰。一個(gè)高度為h地點(diǎn)被稱作d-山峰,只有滿足從這里出發(fā),在不經(jīng)過(guò)小于等于h-d的地點(diǎn)的前提下無(wú)法達(dá)到比它更高的地方。JIH正糾結(jié)于怎么分禮物,標(biāo)出d-山峰的任務(wù)就交給你了。

輸入:

第一行n,m,d
第二行開始,有一個(gè)n*m的矩陣表示地圖,用空格隔開。

輸出:

輸出d-山峰的個(gè)數(shù)。

樣例輸入:

6 10 2
0 0 0 0 0 0 0 0 0 0
0 1 2 1 1 1 1 0 1 0
0 2 1 2 1 3 1 0 0 0
0 1 2 1 3 3 1 1 0 0
0 2 1 2 1 1 1 0 2 0
0 0 0 0 0 0 0 0 0 0

樣例輸出:

4

數(shù)據(jù)范圍限制:

30% n,m<=10
100% n,m<=500

提示:

Paste_Image.png

分析:

正難則反,用總共的點(diǎn)-不合法的點(diǎn),即為合法點(diǎn)。
枚舉每一個(gè)點(diǎn),暴力跑答案判斷,用記憶化優(yōu)化,如果發(fā)現(xiàn)當(dāng)前點(diǎn)的高度>h-d,并是沒(méi)有遍歷過(guò)的就exit(true) else exit(false);
每次找到不合法的點(diǎn),就dec(ans);
如果跑的點(diǎn)數(shù)>n*m的話就直接exit掉了。
暴力可以寬搜,也可以深搜。

實(shí)現(xiàn):

uses math;
const
        u:array[1..4,1..2]of longint=((0,1),(1,0),(0,-1),(-1,0));
var
        ans,n,m,d,i,j,maxn:longint;
        a,f:array[0..501,0..501]of longint;
        bz:array[0..501,0..501]of boolean;
function dg(x,y,h,sum,tot:longint):boolean;
var
        i,xx,yy:longint;
begin
        f[x,y]:=sum;
        if a[x,y]>h then
        begin
                dec(ans);
                exit(true);
        end;
        if (a[x,y]=h)and(bz[x,y]) then
        begin
                dec(ans);
                exit(true);
        end;
        if tot>n*m then exit(false);
        for i:=1 to 4 do
        begin
                xx:=x+u[i,1];
                yy:=y+u[i,2];
                if (xx>0)and(xx<=n)and(yy>0)and(yy<=m) then
                        if (f[xx,yy]<>sum)and(a[xx,yy]>h-d)and(dg(xx,yy,h,sum,tot+1)) then exit(true);
        end;
        exit(false);
end;
begin
        readln(n,m,d);
        for i:=1 to n do
                for j:=1 to m do
                begin
                        read(a[i,j]);
                        maxn:=max(a[i,j],maxn);
                end;
        ans:=n*m;
        for i:=1 to n do
                for j:=1 to m do
                        if a[i,j]<>maxn then bz[i,j]:=dg(i,j,a[i,j],i*m+j,0);
        writeln(ans);
end.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容