刺杀大使{二分答案+dfs+人工栈}
2013-11-04 21:45
253 查看
{转自 仕林} {O(n*m*logmax)}
program murder;var n,m,max:longint;p:array[1..1010,1..1010]of longint;v:array[1..1010,1..1010]of boolean;sx,sy:array[0..1000010]of longint;function dfs(x,y,c:longint):boolean;var u,w:longint;beginsx[0]:=1;sy[0]:=1;sx[sx[0]]:=x;sy[sy[0]]:=y;while sx[0]<>0 dobeginu:=sx[sx[0]];w:=sy[sy[0]];v[u][w]:=true;if u=n then exit(true);if (not v[u+1][w])and(p[u+1][w]<=c) thenbegininc(sx[0]);inc(sy[0]);sx[sx[0]]:=u+1;sy[sy[0]]:=w;continue;end;if (w<m)and(not v[u][w+1])and(p[u][w+1]<=c) thenbegininc(sx[0]);inc(sy[0]);sx[sx[0]]:=u;sy[sy[0]]:=w+1;continue;end;if (w>1)and(not v[u][w-1])and(p[u][w-1]<=c) thenbegininc(sx[0]);inc(sy[0]);sx[sx[0]]:=u;sy[sy[0]]:=w-1;continue;end;if (u>1)and(not v[u-1][w])and(p[u-1][w]<=c) thenbegininc(sx[0]);inc(sy[0]);sx[sx[0]]:=u-1;sy[sy[0]]:=w;continue;end;dec(sx[0]);dec(sy[0]);end;exit(false);end;procedure init;var i,j:longint;beginassign(input,'murder10.in');reset(input);assign(output,'murder.out');rewrite(output);read(n);read(m);max:=-maxlongint;for i:=1 to n dofor j:=1 to m dobeginread(p[i][j]);if max<p[i][j] then max:=p[i][j];end;end;procedure main;var l,r,mid,i:longint;f:boolean;beginl:=0;r:=max;while l<r dobeginmid:=(l+r)shr 1;fillchar(v,sizeof(v),false);f:=false;for i:=1 to m do v[1][i]:=true;for i:=1 to m doif dfs(1,i,mid) then begin f:=true;break;end;if f then r:=midelse l:=mid+1;end;write(l);close(input);close(output);end;begininit;main;end.
下面附上我的仿真代码{WA}{能否指教?}
type s=recordx,y:longint;end;varn,m,i,j,l,r,mid,max:longint;dx:array[1..4] of integer=(-1,1,0,0);dy:array[1..4] of integer=(0,0,-1,1);flag:boolean;p:array[1..1000,1..1000] of longint;used:array[1..1000,1..1000] of boolean;stack:array[0..1000000] of s;function dfs(x,y,w:longint):boolean;vartx,ty,top,j:longint;beginfillchar(used,sizeof(used),false);for j:=1 to m do used[1,j]:=true;stack[1].x:=x;stack[1].y:=y;top:=1;while top>0 dobeginused[stack[top].x,stack[top].y]:=true;if stack[top].x=n then exit(true);tx:=stack[top].x+dx[1];ty:=stack[top].y+dy[1];if (tx>=1)and(tx<=n)and(ty>=1)and(ty<=m)and(not used[tx,ty])and(p[tx,ty]<=w) thenbegininc(top);stack[top].x:=tx;stack[top].y:=ty;continue;end;tx:=stack[top].x+dx[2];ty:=stack[top].y+dy[2];if (tx>=1)and(tx<=n)and(ty>=1)and(ty<=m)and(not used[tx,ty])and(p[tx,ty]<=w) thenbegininc(top);stack[top].x:=tx;stack[top].y:=ty;continue;end;tx:=stack[top].x+dx[3];ty:=stack[top].y+dy[3];if (tx>=1)and(tx<=n)and(ty>=1)and(ty<=m)and(not used[tx,ty])and(p[tx,ty]<=w) thenbegininc(top);stack[top].x:=tx;stack[top].y:=ty;continue;end;tx:=stack[top].x+dx[4];ty:=stack[top].y+dy[4];if (tx>=1)and(tx<=n)and(ty>=1)and(ty<=m)and(not used[tx,ty])and(p[tx,ty]<=w) thenbegininc(top);stack[top].x:=tx;stack[top].y:=ty;continue;end;dec(top);end;exit(false);end;beginassign(input,'work.in');reset(input);assign(output,'work.out');rewrite(output);read(n,m);max:=0;for i:=1 to n dofor j:=1 to m dobeginread(p[i,j]);if max<p[i,j] then max:=p[i,j];end;l:=1;r:=max;while l<>r dobeginmid:=(l+r) shr 1;flag:=false;for j:=1 to m doif dfs(1,j,mid) thenbeginflag:=true;breakend;if flag=true then r:=midelse l:=mid+1;end;writeln(l);close(input);close(output);end.
相关文章推荐
- 洛谷P1902 刺杀大使(二分答案+bfs验证)
- P1902 刺杀大使(BFS+二分答案)
- 【NOIP 模拟题】刺杀大使(二分答案+并查集)
- 洛谷P1902 刺杀大使(二分)
- 【P1902】T12 刺杀大佬【雾】(二分答案+bfs)
- 二分+Dfs【p1902】刺杀大使
- poj 3208 Apocalypse Someday (数位dp+二分答案)
- bzoj 3035 二分答案+二分图最大匹配
- 2017-07-08:考试(二分答案+斜率+搜索)
- POJ 3261 Milk Patterns(后缀数组+二分答案)
- BZOJ 1196 [HNOI2006]公路修建问题(二分答案+并查集)
- BZOJ 2525 Poi2011 Dynamite 二分答案+树形贪心
- ZSOI2012 最大立方体空间 二分答案
- hdu 4190 二分答案
- POJ 1064 Cable master 二分答案
- 有一种感觉叫二分(二分答案)
- [BZOJ1044]HAOI2008木棍分割|DP|二分答案
- 洛谷Luogu-267 跳石头(NOIP提高组2015 day2 t1)(二分答案) HQG_AC的博客
- 函数求解与二分答案
- HDU 3586 Information Disturbing(二分答案+树形DP)