跳到正文

题解

题解 P1172 【安全逃离】

姬海棠果约 3 分钟
原文
文章信息与版本

文章信息

发布
2016年5月31日
收录
2026年10月26日
更新
2026年11月14日
篇幅
约 3 分钟
获赞
3
题目
P1172

版本

  1. v1当前

首先判断所有奶牛,如果它右边和上边都有奶牛,那么它不能逃离

如果都能逃离,输出0

再枚举删掉哪只奶牛,并进行与开始相同的操作,如果枚举到奶牛i其他的都可逃离,直接输出,不必继续枚举

如果枚举完毕还没有输出,就输出-1

代码如下:

C++
program Evac;
const
maxr=50;
maxn=100;
var
a:array[1..maxr,1..maxr] of byte;
N:byte;
cows:array[1..maxn,1..4] of byte;
R,C:byte;
procedure init;
var i:byte;
begin
assign(input,'evac.in');
reset(input);
assign(output,'evac.out');
rewrite(output);
readln(R,C);
fillchar(a,sizeof(a),0);
readln(N);
fillchar(cows,sizeof(cows),0);
for i:=1 to N do
begin
readln(cows[i,1],cows[i,2]);
a[cows[i,1],cows[i,2]]:=i;
end;
close(input);
end;
procedure solve;
var id,i,j,t,t1,x:byte;
flag:boolean;
begin
t:=0;
for id:=1 to N do
begin
for i:=1 to cows[id,1]-1 do
if a[i,cows[id,2]]<>0 then inc(cows[id,3]);
for j:=cows[id,2]+1 to C do
if a[cows[id,1],j]<>0 then inc(cows[id,4]);
end;
for id:=1 to N do if cows[id,3]*cows[id,4]<>0 then inc(t);
if t=0 then
begin writeln(0);exit;end;
flag:=true;
for id:=1 to N do
begin
t1:=t;
if cows[id,3]*cows[id,4]<>0 then dec(t1);
for i:=cows[id,1]+1 to R do
if a[i,cows[id,2]]>0 then
begin
x:=a[i,cows[id,2]];
if (cows[x,3]=1)and(cows[x,4]<>0) then dec(t1);
end;
for j:=1 to cows[id,2]-1 do
if a[cows[id,1],j]>0 then
begin
x:=a[cows[id,1],j];
if (cows[x,4]=1)and(cows[x,3]<>0) then dec(t1);
end;
if t1=0 then
begin writeln(id);flag:=false; end;
end;
if flag then writeln(-1);
end;
begin
init;
solve;
close(output);
end.