题意:卒子过河,有个马,问安全到达终点的路径有多少条。起点在0,0。每一步可以往右或者往下。
思路:处理出马的看守点,然后暴力。。看了一下暴力会TLE。400^2. 直接dp转移即可。
总结:不知道这个还要开long long, 哎。!
void solve(){
pair<int, int> destination;
vector<pair<int, int>> hourse(1);
cin >>destination.first >> destination.second;
cin >> hourse[0].first >> hourse[0].second;
hourse.emplace_back(hourse[0].first - 1, hourse[0].second - 2);
hourse.emplace_back(hourse[0].first - 1, hourse[0].second + 2);
hourse.emplace_back(hourse[0].first + 1, hourse[0].second - 2);
hourse.emplace_back(hourse[0].first + 1, hourse[0].second + 2);
hourse.emplace_back(hourse[0].first + 2, hourse[0].second - 1);
hourse.emplace_back(hourse[0].first - 2, hourse[0].second - 1);
hourse.emplace_back(hourse[0].first + 2, hourse[0].second + 1);
hourse.emplace_back(hourse[0].first - 2, hourse[0].second + 1);
sort(hourse.begin(), hourse.end());
for (const auto& x : hourse){
if (x == destination){
cout << 0 << '\n';
return;
}
}
vector<vector<long long>>dp(22, vector<long long>(22, 0));
auto inHourse = [&](int x, int y){
for (const auto& cur : hourse){
if (cur == pair<int, int>{x, y}){
return true;
}
}
return false;
};
dp[0][0] = 1;
for (int i = 0; i <= 20; ++i){
for (int j = 0; j <= 20; ++j){
if (inHourse(i, j) == false ){
if (inHourse(i + 1, j) == false) dp[i + 1][j] += dp[i][j];
if (inHourse(i, j + 1) == false) dp[i][j + 1] += dp[i][j];
}
}
}
cout << dp[destination.first][destination.second] << '\n';
}
标签:过河,NOIP2002,destination,P1002,back,second,emplace,hourse,first
From: https://www.cnblogs.com/yxcblogs/p/18120582