2018湖南省第14屆大學生計算機程式設計競賽---賣萌表情
阿新 • • 發佈:2019-01-09
用貪心來求。我的理解:用貪心來解決問題時要優先選擇貢獻大的。
對於後2種表情,直接列舉就行。
對於前面2種表情,先選擇倒著的笑臉,因為出現‘^ ’時,倒著的笑臉能儘可能多的使用‘v’.
#include<bits/stdc++.h> #define maxn 1005 using namespace std; char mp[maxn][maxn]; int n,m; int main() { ios::sync_with_stdio(false); while(cin>>n>>m) { memset(mp,'0',sizeof(mp)); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>mp[i][j]; int cnt=0; for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(mp[i][j]=='^') { if(mp[i+1][j-1]=='v'&&mp[i+1][j+1]=='v') { cnt++; mp[i+1][j-1]='0'; mp[i+1][j+1]='0'; mp[i][j]='0'; } else { if(mp[i][j+2]=='^'&&mp[i+1][j+1]=='v') { cnt++; mp[i][j+2]='0'; mp[i+1][j+1]='0'; mp[i][j]='0'; } } } else if(mp[i][j]=='<') { if(mp[i+1][j+1]=='>'&&mp[i+2][j]=='<') { cnt++; mp[i+1][j+1]='0'; mp[i+2][j]='0'; mp[i][j]='0'; } } else if(mp[i][j]=='>') { if(mp[i+1][j-1]=='<'&&mp[i+2][j]=='>') { cnt++; mp[i+1][j-1]='0'; mp[i+2][j]='0'; mp[i][j]='0'; } } } } cout<<cnt<<endl; } return 0; }