您好,我写了一个简短的程序来尝试回答Google Kickstart 2020 B轮问题漫游机器人。我实质上是尝试实施Google提供的分析,而且我的解决方案似乎可以在我尝试过的所有示例案例中都可以使用,但从未通过Google的第一个测试集。
这是我的代码,下面是问题描述和我的方法的简短说明。我真正想知道的是我做错了什么,为什么我没有通过第一个测试集,比什么都重要。
#include <iostream>
#include<math.h>
using namespace std;
#include <iomanip>
int W, H, L, U, R, D;
const int len = 100000;
double facts [len];
void factorial(){
facts[0] = log2(1);
for(int i=2; i<= len; i++){
facts[i-1] = log2(i) + facts[i-2];
}
}
bool impossible(){
if((L==1 && R==W) || (U==1 && D==H)){
return true;
}
return false;
}
double pass(int x, int y, int s, int t, int i){
double num = log2(x+y);
num = facts[x+y-1];
double prob = x+y;
double tot = 0;
double comb = 0;
//cout << x << " " << s << " " << y << " " << t << endl;
while(x!=s && y!=t){
double denom = facts[x-1] + facts[y-1];
comb = num - denom - prob;
// cout << num << endl;
tot += pow(2, comb);
x+=i;
y-=i;
}
//lower diagonal, hit the bottom
//x+1 = number of moves it takes
//to hit last square
if(y==H){
double p = pow(2,comb);
tot -= p;
x-=i;
for(int j=1; j<=x; j++){
p -= pow(0.5, prob);
p += pow(0.5, prob-j-1);
}
tot += p;
}
//upper diagonal, hit the right side
else if(x==W){
double p = pow(2,comb);
tot -= pow(2, comb);
y+=i;
for(int j=1; j<=y; j++){
p -= pow(0.5, prob);
p += pow(0.5, prob-j-1);
}
tot += p;
}
return tot;
}
int main() {
int t;
cin >> t; // read t. cin knows that t is an int, so it reads it as such.
for (int i = 1; i <= t; ++i) {
cin >> W >> H >> L >> U >> R >> D;
double tot = 0;
if(impossible()){
cout << "Case #" << i << ": 0.0" << endl;
}
//upper diagonal
else if(L==1 || D==H){
factorial();
tot = pass(R, U-2, W, -1, 1);
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
//lower diagonal
else if(R==W || U==1){
factorial();
tot = pass(L-2, D, -1, H, -1);
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
//symmetric
else if(D==R && L==U){
factorial();
tot = pass(R, U-2, W, -1, 1);
tot *=2;
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
//call count both
else{
factorial();
tot = pass(R, U-2, W, -1, 1);
tot += pass(L-2, D,-1, H, -1);
cout << "Case #" << i << ": " << fixed << setprecision(9) << tot << endl;
}
}
return 0;
}
我试图解决的方法是找到穿过绿色正方形和从孔的右上角到球馆右边缘的倒数正方形的概率。