您的位置:首页 > 其它

刺杀大使{二分答案+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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: