以下是我得程式碼
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int t,t2;
int r=1;
while(cin>>t>>t2)
{
int n[t][t2],m[t][t2];
for(int i=0;i<t;i++)
{
for(int j=0;j<t2;j++) cin>>n[i][j],m[i][j]=n[i][j];
}
queue<int> x;
queue<int>y;
x.push(0),y.push(0);
//int x,y;
while(!x.empty())
{
if(x.front()<t2-1)
{
int x2=x.front()+1;
int y2=y.front();
x.push(x.front()+1),y.push(y.front());
if(n[y2][x2]>n[y.front()][x.front()]+m[y2][x2]||n[y2][x2]==m[y2][x2]) n[y2][x2]=n[y.front()][x.front()]+m[y2][x2];
}
if(y.front()<t-1)
{
int y2=y.front()+1;
int x2=x.front();
y.push(y.front()+1),x.push(x.front());
if(n[y2][x2]>n[y.front()][x.front()]+m[y2][x2]||n[y2][x2]==m[y2][x2]) n[y2][x2]=n[y.front()][x.front()]+m[y2][x2];
}
x.pop();
y.pop();
}
cout<<"Case #"<<r<<" :\n";
cout<<n[t-1][t2-1]<<endl;
r++;
}
}
以下是我得程式碼
#include
#include
using namespace std;
int main()
{
int t,t2;
int r=1;
while(cin>>t>>t2)
{
int n[t][t2],m[t][t2];
for(int i=0;i<t;i++)
{
for(int j=0;j<t2;j++) cin>>n[i][j],m[i][j]=n[i][j];
}
queue x;
queuey;
x.push(0),y.push(0);
//int x,y;
while(!x.empty())
{
if(x.front()<t2-1)
{
int x2=x.front()+1;
int y2=y.front();
x.push(x.front()+1),y.push(y.front());
if(n[y2][x2]>n[y.front()][x.front()]+m[y2][x2]||n[y2][x2]==m[y2][x2]) n[y2][x2]=n[y.front()][x.front()]+m[y2][x2];
}
if(y.front()<t-1)
{
int y2=y.front()+1;
int x2=x.front();
y.push(y.front()+1),x.push(x.front());
if(n[y2][x2]>n[y.front()][x.front()]+m[y2][x2]||n[y2][x2]==m[y2][x2]) n[y2][x2]=n[y.front()][x.front()]+m[y2][x2];
}
x.pop();
y.pop();
}
cout<<"Case #"<<r<<" :\n";
cout<<n[t-1][t2-1]<<endl;
r++;
}
}
沒事了