#include <iostream>
using namespace std;
int main(){
int data[10404],a,b,min=1000001,x,y,all=0,x1,y1;
for(int i=0;i<10404;i++){
data[i]=1000001;
}
cin>>a>>b;
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
cin>>data[i*b+2*i+j+1];
if(data[i*b+2*i+j+1]<min){
min=data[i*b+2*i+j+1];
x=i;
y=j;
}
}
}
all+=data[x*b+2*x+y+1];
data[x*b+2*x+y+1]=1000001;
x1=x;
y1=y;
while(true){
if(data[x*b+2*x+y]<data[x*b+2*x+y+2]&&data[x*b+2*x+y]<data[x*b+2*x+y-1-b]&&data[x*b+2*x+y]<data[x*b+2*x+y+3+b]){
all+=data[x*b+2*x+y];
y1-=1;
data[x*b+2*x+y]=1000001;
}else if(data[x*b+2*x+y+2]<data[x*b+2*x+y-1-b]&&data[x*b+2*x+y+2]<data[x*b+2*x+y+3+b]){
all+=data[x*b+2*x+y+2];
y1+=1;
data[x*b+2*x+y+2]=1000001;
}else if(data[x*b+2*x+y-1-b]<data[x*b+2*x+y+3+b]){
all+=data[x*b+2*x+y-1-b];
x1-=1;
data[x*b+2*x+y-1-b]=1000001;
}else if(data[x*b+2*x+y+3+b]<1000001){
all+=data[x*b+2*x+y+3+b];
x1+=1;
data[x*b+2*x+y+3+b]=1000001;
}else{
break;
}
x=x1;
y=y1;
}
cout << all << endl ;
return 0;
}
#include
using namespace std;
int main(){
int data[10404],a,b,min=1000001,x,y,all=0,x1,y1;
for(int i=0;i<10404;i++){
data[i]=1000001;
}
cin>>a>>b;
for(int i=1;i<=a;i++){
for(int j=1;j<=b;j++){
cin>>data[i*b+2*i+j+1];
if(data[i*b+2*i+j+1]<min){
min=data[i*b+2*i+j+1];
x=i;
y=j;
}
}
}
all+=data[x*b+2*x+y+1];
data[x*b+2*x+y+1]=1000001;
x1=x;
y1=y;
while(true){
if(data[x*b+2*x+y]<data[x*b+2*x+y+2]&&data[x*b+2*x+y]<data[x*b+2*x+y-1-b]&&data[x*b+2*x+y]<data[x*b+2*x+y+3+b]){
all+=data[x*b+2*x+y];
y1-=1;
data[x*b+2*x+y]=1000001;
}else if(data[x*b+2*x+y+2]<data[x*b+2*x+y-1-b]&&data[x*b+2*x+y+2]<data[x*b+2*x+y+3+b]){
all+=data[x*b+2*x+y+2];
y1+=1;
data[x*b+2*x+y+2]=1000001;
}else if(data[x*b+2*x+y-1-b]<data[x*b+2*x+y+3+b]){
all+=data[x*b+2*x+y-1-b];
x1-=1;
data[x*b+2*x+y-1-b]=1000001;
}else if(data[x*b+2*x+y+3+b]<1000001){
all+=data[x*b+2*x+y+3+b];
x1+=1;
data[x*b+2*x+y+3+b]=1000001;
}else{
break;
}
x=x1;
y=y1;
}
cout << all << endl ;
return 0;
}
解題報告不要直接PO程式碼==
在打的時候不是都有警告你嗎??
我發現貼了就改不了
感謝提醒,以後會多注意(我真的沒看到)
順便講解一下
由於本人程式能力不佳,所以想出了這個方法
網路上大多以-1代表走過的格子,但如此一來,-1必定小於其他有效格子的數字,像我只會for,while,if,cin,cout這些基礎的不太友善,要確定數字有效性後再比較,程式應該十分複雜,所以我將走過的格子設為1000001,如此一來比大小時,無效的數字必定大於要比較的有效數字,但是這樣2維陣列會出現溢位的問題,故使用一維陣列。
在a*b的格子中
x-y的格子編號為x*b+2*x+y+1
x-(y-1)的格子編號為x*b+2*x+y
x-(y+1)的格子編號為x*b+2*x+y+2
(x-1)-y的格子編號為x*b+2*x+y-1-b
(x+1)-y的格子編號為x*b+2*x+y+3+b
(有加邊界,上下各一行,左右各一列,我也忘了當初我x,y哪個是行哪個是列)
最後,其實這程式根據我當初的想法有點瑕疵,最後一格邊界的編號應為10404號(忘記陣列要從0開始數),但沒有這個邊界,不過在題目極限100*100的方格中最大號會用到的邊界格子是10403號所以沒事