做这个题是根据矩形的最大面积链接过来的,本来以为是用动态规划来做,进来以后感觉这题也不是动态规划,只好参考评论区大神进行模拟,恕我自己太菜。
正向从空画板到现在的画板不好做,进行逆向,从现有画板返回到空画板进行操作,如果当前这个点为B,则从当前点向右下角的对角线去找是否还有B,有的话进行更改,如果发现有G的话,减去一次B,改为Y,按道理不应该有Y,如果有Y的话直接退出。
同理,如果当前点是Y的话,向左下角去寻找,更新方法,同上述一样。
循环遍历所有数组,每一次进入一次更改,结果值+1,
#include<iostream>
#include<vector>
using namespace std;
void helper(vector<vector<char>>& map,const int& n,const int& m,int x,int y){
if(map[x][y] == 'Y'){
while(x < n && y < m){
if(map[x][y] == 'Y') map[x][y] = 'X';
else if(map[x][y] == 'G') map[x][y] = 'B';
else break;
x++;
y++;
}
}else{
while(x < n && y >= 0){
if(map[x][y] == 'B') map[x][y] = 'X';
else if(map[x][y] == 'G') map[x][y] = 'Y';
else break;
x++;
y--;
}
}
return;
}
int main(){
int n=0,m = 0;
cin >> n >> m;
if(n == 0 || m == 0){
cout << 0 << endl;
return 0;
}
vector<vector<char>> map(n,vector<char>(m));
for(int i = 0;i<n;i++){
for(int j=0;j<m;j++){
cin >> map[i][j];
}
}
if(map.size() != n || map[0].size() != m){
cout << 0 << endl;
return 0;
}
int res = 0;
for(int i=0;i<n;i++){
for(int j = 0;j<m;j++){
if(map[i][j] == 'X') continue;
if(map[i][j] == 'Y' || map[i][j] == 'B'){
res ++;
helper(map,n,m,i,j);
}else if(map[i][j] == 'G'){
res ++;
map[i][j] = 'Y';
helper(map,n,m,i,j);
res++;
map[i][j] = 'B';
helper(map,n,m,i,j);
}
}
}
cout << res << endl;
return 0;
}
京公网安备 11010502036488号