枚举中间可能出现的天气。
#include#include #include const int maxn = 1005; double mat[ 5 ][ 5 ]; void solve( int L,int R,int n ){ double ans[ 5 ],tp[ 5 ]; double res = 0; for( int i=1;i<=n;i++ ){ if( i==1 ){ ans[ 1 ] = mat[ L ][ 1 ]; ans[ 2 ] = mat[ L ][ 2 ]; ans[ 3 ] = mat[ L ][ 3 ]; //printf("i = 1: %lf %lf %lf\n",ans[1],ans[2],ans[3]); continue; } else if( i==n ){ res = ans[ 1 ]*mat[ 1 ][ R ]; res += ans[ 2 ]*mat[ 2 ][ R ]; res += ans[ 3 ]*mat[ 3 ][ R ]; continue; } else { tp[1] = ans[1],tp[2] = ans[2],tp[3] = ans[3]; ans[ 1 ] = tp[ 1 ]*mat[ 1 ][ 1 ]+tp[ 2 ]*mat[ 2 ][ 1 ]+tp[ 3 ]*mat[ 3 ][ 1 ]; ans[ 2 ] = tp[ 1 ]*mat[ 1 ][ 2 ]+tp[ 2 ]*mat[ 2 ][ 2 ]+tp[ 3 ]*mat[ 3 ][ 2 ]; ans[ 3 ] = tp[ 1 ]*mat[ 1 ][ 3 ]+tp[ 2 ]*mat[ 2 ][ 3 ]+tp[ 3 ]*mat[ 3 ][ 3 ]; // ans[1] = tp[1],ans[2] = tp[2],ans[3] = tp[3]; //printf("i = %d: %lf %lf %lf\n",i,ans[1],ans[2],ans[3]); continue; } } printf("%.3lf\n",res); } int main(){ //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while( T-- ){ for( int i=1;i<=3;i++ ){ for( int j=1;j<=3;j++ ){ scanf("%lf",&mat[ i ][ j ]); } } int K; scanf("%d",&K); while( K-- ){ int L,R,N; scanf("%d%d%d",&L,&R,&N); if( N==1 ){ printf("%.3lf\n",mat[ L ][ R ]); continue; } solve( L,R,N ); } } return 0; }