請你幫忙設計乙個從城市m到城市z的輸油管道,現在已經把整個區域劃分為r行c列,每個單元格可能是空的也可能是以下7種基本管道之一:
油從城市m流向z,『+』型管道比較特殊,因為石油必須在兩個方向(垂直和水平)上傳輸,如下圖所示:
現在****弄到了輸油管道的設計圖,並把其中乙個單元格中的管道偷走了,請你幫忙找到偷走的管道的位置以及形狀。
題解:列舉每乙個沒有管道的點四周有沒有管道通向這個店,判斷放什麼管道,輸出。
**:
var
i,j,r,c,x,y,l,q,shang,xia,zuo,you:longint;
ch:char;
a:array[0..25,0..25] of longint;
procedure
a7;begin
inc(x);
if a[x,y]=0
then
begin l:=x;q:=y;shang:=1;end;
x:=x-2;
if a[x,y]=0
then
begin l:=x;q:=y;xia:=1;end;
inc(x);inc(y);
if a[x,y]=0
then
begin l:=x;q:=y;zuo:=1;end;
y:=y-2;
if a[x,y]=0
then
begin l:=x;q:=y;you:=1;end;
inc(y);
end;
procedure
a6;begin
inc(y);
if a[x,y]=0
then
begin l:=x;q:=y;zuo:=1;end;
y:=y-2;
if a[x,y]=0
then
begin l:=x;q:=y;you:=1;end;
inc(y);
end;
procedure
a5;begin
inc(x);
if a[x,y]=0
then
begin l:=x;q:=y;shang:=1;end;
x:=x-2;
if a[x,y]=0
then
begin l:=x;q:=y;xia:=1;end;
inc(x);
end;
procedure
a4;begin
inc(x);
if a[x,y]=0
then
begin l:=x;q:=y;shang:=1;end;
dec(x);dec(y);
if a[x,y]=0
then
begin l:=x;q:=y;you:=1;end;
end;
procedure
a3;begin
dec(x);
if a[x,y]=0
then
begin l:=x;q:=y;xia:=1;end;
inc(x);dec(y);
if a[x,y]=0
then
begin l:=x;q:=y;you:=1;end;
end;
procedure
a2;begin
dec(x);
if a[x,y]=0
then
begin l:=x;q:=y;xia:=1;end;
inc(x);inc(y);
if a[x,y]=0
then
begin l:=x;q:=y;zuo:=1;end;
end;
procedure
a1;begin
inc(x);
if a[x,y]=0
then
begin l:=x;q:=y;shang:=1;end;
dec(x);inc(y);
if a[x,y]=0
then
begin l:=x;q:=y;zuo:=1;end;
end;
procedure
init;
begin
readln(r,c);
for i:=1
to r do
begin
for j:=1
to c do
begin
read(ch);
case ch of
'1','2','3','4':a[i,j]:=ord(ch)-48;
'|':a[i,j]:=5;
'-':a[i,j]:=6;
'+':a[i,j]:=7;
'm','z':a[i,j]:=8;
end;
end;
readln;
end;
end;
begin
init;
for i:=1
to r do
for j:=1
to c do
begin
x:=i;y:=j;
if a[x,y]=1
then a1;
if a[x,y]=2
then a2;
if a[x,y]=3
then a3;
if a[x,y]=4
then a4;
if a[x,y]=5
then a5;
if a[x,y]=6
then a6;
if a[x,y]=7
then a7
end;
if zuo=0
then
if a[l,q-1]=8
then zuo:=zuo+1;
if shang=0
then
if a[l-1,q]=8
then shang:=shang+1;
if you=0
then
if a[l,q+1]=8
then you:=you+1;
if xia=0
then
if a[l+1,q]=8
then xia:=xia+1;
if zuo+you+xia+shang=4
then writeln(l,' ',q,' ','+')
else
if zuo=1
then
begin
if shang=1
then writeln(l,' ',q,' ',3);
if you=1
then writeln(l,' ',q,' ','-');
if xia=1
then writeln(l,' ',q,' ',4);
endelse
if shang=1
then
begin
if you=1
then writeln(l,' ',q,' ',2);
if xia=1
then writeln(l,' ',q,' ','|');
endelse writeln(l,' ',q,' ',1);
end.
輸油管道問題
某石油公司計畫建造一條由東向西的主輸油管道。該管道要穿過乙個有n 口油井的油田。從每口油井都要有一條輸油管道沿最短路經 或南或北 與主管道相連。如果給定n 口油井的位置,即它們的x 座標 東西向 和y 座標 南北向 應如何確定主管道的最優位置,即使各油井到主管道之間的輸油管道長度總和最小的位置?1 ...
輸油管道問題
某石油公司計畫建造一條由東向西的主輸油管道。該管道要穿過乙個有n口油井的油田。從每口油井都要有一條輸油管道沿最短路經 或南或北 與主管道相連。如果給定n口油井的位置,即它們的x座標 東西向 和y座標 南北向 應如何確定主管道的最優位置,即使各油井到主管道之間的輸油管道長度總和最小的位置?給定n口油井...
輸油管道問題
根據中位數定理,要使總和距離最小,應該選擇中位數作為目標點。因此解決此問題的方法就是將各個油井投影到y軸,找到y座標的中位數即可。解題思路 要找出乙個數的中位數,最簡單的方法就是對陣列進行排序,但是快速排序的時間複雜度也需要o nlogn 對與有序陣列a n 如果陣列是奇數個,中位數就是a n 2 ...