题解 | #不要二#
不要二
https://www.nowcoder.com/practice/1183548cd48446b38da501e58d5944eb
蛋糕距离不能为2
/* 2022年09月18日 18:28:32 ( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) == 4 1 + 3 = 4 3 + 1 = 4 2 + 2 = 4 0 + 4 = 4 4 + 0 = 4 前三种情况都不存在 只能是x1=x2 (y1-y2) = 2 或者 y1=y2 x1-x2 = 2 假设数组全是1 1 1 1 1 1 1 --> 1 1 1 1 0 0 也就是v[i][j]和v[i+2][j]、v[i][j]和v[i][j+2] 只能有一个有蛋糕 */ #include <iostream> #include <vector> using namespace std; int main() { int row, col; cin >> row >> col; vector<vector<int>> vv(row); // row行 int count = 0; for(int i = 0; i < vv.size(); ++i) vv[i].resize(col, 1); // col列,全初始化为1 for (int i = 0; i < vv.size(); ++i) { for (int j = 0; j < vv[i].size(); ++j) { if(vv[i][j] == 1){ ++count; if(i+2 < row) vv[i+2][j] = 0; if(j+2 < col) vv[i][j+2] = 0; } } } cout << count << endl; return 0; }
/* 2022年09月18日 18:28:32 ( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) == 4 1 + 3 = 4 3 + 1 = 4 2 + 2 = 4 0 + 4 = 4 4 + 0 = 4 前三种情况都不存在 只能是x1=x2 (y1-y2) = 2 或者 y1=y2 x1-x2 = 2 另一种写法,往前判断,注意i-2 j-2不能越界 假设数组全是0 0 0 0 0 0 0 --》 1 1 1 1 0 0 */ #include <iostream> #include <vector> using namespace std; int main() { int row, col; cin >> row >> col; vector<vector<int>> vv(row); // row行 int count = 0; for (int i = 0; i < vv.size(); ++i) { vv[i].resize(col); // col列,默认都是0 for (int j = 0; j < vv[i].size(); ++j) { if (i - 2 >= 0 && vv[i - 2][j] == 1) continue; if (j - 2 >= 0 && vv[i][j - 2] == 1) continue; vv[i][j] = 1; ++count; } } cout << count << endl; return 0; } // 不能用size_t