本題思路:
1 先掃描行,如果可以吃,就數吃了多少格,然後做好標志
2 掃描列,同樣處理
掃描完就可以出答案了。
時間效率是O(n*m)了。算是暴力法
題目:
#include#include #include #include #include #include using namespace std; void Cakeminator() { unsigned row, col; cin>>row>>col; vector cake(row); for (unsigned i = 0; i < row; i++) { cin>>cake[i]; } int cakeCells = 0; for (unsigned i = 0; i < row; i++) { bool eatable = true; for (unsigned j = 0; j < col; j++) { if ('S' == cake[i][j]) eatable = false; } if (eatable) { for (unsigned j = 0; j < col; j++) { if ('.' == cake[i][j]) { cake[i][j] = 'E'; cakeCells++; } } } } for (unsigned j = 0; j < col; j++) { bool eatable = true; for (unsigned i = 0; i < row; i++) { if ('S' == cake[i][j]) eatable = false; } if (eatable) { for (unsigned i = 0; i < row; i++) { if ('.' == cake[i][j]) { cake[i][j] = 'E'; cakeCells++; } } } } cout<