//148K 157MS C++
#include <cstring>
#include <cstdio>
#define M_PI 3.14159265358979323846
using namespace std;
double Diameter;
double Radius;
double tileLength;
double width;
double height;
double totalSquare;
double OneTile(int height, int width, int tileLength, double Radius) {
if (height * width == 1) {
return 1.0;
}
if (height == 1) {
int middleNum = width - 2;
double square = 0;
square += tileLength*(tileLength - Radius)*2;
square += middleNum*(tileLength - 2*Radius)*tileLength;
// printf("%lf\n", square);
return square/totalSquare;
} else if (width == 1) {
int middleNum = height - 2;
double square = 0;
square += tileLength*(tileLength - Radius)*2;
square += middleNum*(tileLength - 2*Radius)*tileLength;
return square/totalSquare;
}
double square = 0;
int middleNum = (height - 2) * (width - 2);
if (middleNum < 0) {
middleNum = 0;
}
int cornerNum = 4;
int sideNotCornerNum = (height*width - cornerNum - middleNum);
square += middleNum*(tileLength - Diameter)*(tileLength - Diameter);
square += cornerNum*(tileLength - Radius)*(tileLength - Radius);
square += sideNotCornerNum*(tileLength - Diameter)*(tileLength - Radius);
return square/totalSquare;
}
double FourTile(int height, int width, int tileLength, double Radius) {
if (height < 2 || width < 2) {
return 0.0;
}
return ((height - 1)*(width - 1)*M_PI*(Radius)*Radius)/totalSquare;
}
double TwoTile(int height, int width, int tileLength, double Radius) {
if (height * width == 1) {
return 0.0;
}
int adjacent1 = 0;
int adjacent2 = 0;
int adjacent3 = 0;
int adjacent4 = 0;
if (height == 1) {
adjacent1 = 2;
adjacent2 = width - 2;
double square = 0;
square += adjacent1 * Radius * (tileLength);
//sepcial, left and after
square += adjacent2 * Radius * (tileLength) * 2;
return square/totalSquare;
} else if (width == 1) {
adjacent1 = 2;
adjacent2 = height - 2;
double square = 0;
//special, left and after
square += adjacent1 * Radius * (tileLength);
square += adjacent2 * Radius * (tileLength) * 2;
return square/totalSquare;
} else {
adjacent2 = 4;
adjacent4 = (height-2)*(width-2);
adjacent3 = height*width - adjacent2 - adjacent4;
}
double square = 0;
square += adjacent1 * Radius * (tileLength);
square += adjacent2 * Radius * (tileLength - Radius) * 2;
square += adjacent3 * ( Radius * (tileLength - Diameter) + Radius * (tileLength - Radius)*2);
square += adjacent4 * Radius * (tileLength - Diameter) * 4;
return square/totalSquare;
}
int caseNum;
int main() {
scanf("%d", &caseNum);
for (int i = 1; i <= caseNum; i++) {
scanf("%lf %lf %lf %lf", &height, &width, &tileLength, &Diameter);
Radius = (double)Diameter/2;
totalSquare = height * width * tileLength * tileLength;
double P1Tile = OneTile(height, width, tileLength, Radius);
double P2Tile = TwoTile(height, width, tileLength, Radius);
double P4Tile = FourTile(height, width, tileLength, Radius);
double P3Tile = 1 - P1Tile - P2Tile - P4Tile;
if (P3Tile <0) {
P3Tile = 0.0;
}
printf("Case %d:\n", i);
printf("Probability of covering 1 tile = %.4lf%%\n", P1Tile*100);
printf("Probability of covering 2 tiles = %.4lf%%\n", P2Tile*100);
printf("Probability of covering 3 tiles = %.4lf%%\n", P3Tile*100);
printf("Probability of covering 4 tiles = %.4lf%%\n", P4Tile*100);
if (i != caseNum) {
printf("\n");
}
}
}
纯概率题,不过还是有些好的思路的.
poj-3440
最新推荐文章于 2017-04-18 16:01:16 发布