爬山问题
题目大意:
若两个格子有公共顶点,那么他们就是相邻的格子。(所以与(i,j)相邻的格子有(i−1, j−1),(i−1,j),(i−1,j+1),(i,j−1),(i,j+1),(i+1,j−1),(i+1,j),(i+1,j+1))。
我们定义一个格子的集合S为山峰(山谷)当且仅当:
1.S的所有格子都有相同的高度。
2.S的所有格子都联通
3.对于s属于S,与s相邻的s’不属于S。都有ws>ws’(山峰),或者ws <ws’(山谷)
给定一个图,求所有的山峰和山谷。
注意,有的格子可能既不是山峰也不是山谷,当所有格子都等高的时候,它既是山峰也是山谷。
直接裸搜,枚举一个未被访问的结点搜索所有它能到达的点。深搜要写手写栈。但是要注意广搜的队列不要每次fillchar。。。pascal会常数很大导致跑不出。血淋淋的教训啊
1 program Neayo; 2 const 3 inf='grz.in'; 4 ouf='grz.out'; 5 f:array[1..8,1..2] of longint=((-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)); 6 var 7 i,j,k,n,sf,sg,tmp:longint; 8 a:array[0..1001,0..1001]of longint; 9 v:array[0..1001,0..1001]of boolean; 10 qx,qy:array[0..1000000]of longint; 11 procedure init; 12 begin 13 assign(input,inf);assign(output,ouf); 14 reset(input);rewrite(output); 15 readln(n); 16 fillchar(v,sizeof(v),false); 17 for i:=1 to n do 18 for j:=1 to n do 19 read(a[i,j]); 20 close(input); 21 end; 22 function bfs(sx,sy:longint):longint; 23 var i,closed,top,x,y:longint; 24 big,small:boolean; 25 begin 26 qx[1]:=sx;qy[1]:=sy; 27 small:=true; 28 big:=true; 29 closed:=1;top:=0; 30 repeat 31 inc(top); 32 for i:=1 to 8 do 33 begin 34 x:=qx[top]+f[i,1]; 35 y:=qy[top]+f[i,2]; 36 if(x>0)and(y>0)and(x<=n)and(y<=n)then 37 begin 38 if a[qx[top],qy[top]]<a[x,y] then big:=false; 39 if a[qx[top],qy[top]]>a[x,y] then small:=false; 40 if (not v[x,y])and(a[qx[top],qy[top]]=a[x,y]) then 41 begin 42 inc(closed); 43 qx[closed]:=x; 44 qy[closed]:=y; 45 v[x,y]:=true; 46 end; 47 end; 48 end; 49 until(top>=closed); 50 if (big and small)or((not big) and (not small)) then exit(0); 51 if big and (not small) then exit(1); 52 if small and (not big) then exit(2); 53 end; 54 begin 55 init; 56 for i:=1 to n do 57 for j:=1 to n do 58 if not v[i,j] then 59 begin 60 v[i,j]:=true; 61 tmp:=bfs(i,j); 62 if tmp=1 then inc(sf); 63 if tmp=2 then inc(sg); 64 end; 65 if (sf=0)and(sg=0)then writeln(1,' ',1) 66 else writeln(sf,' ',sg); 67 close(output); 68 end.