UVA 1641 - ASCII Area(模拟)

题目链接 https://cn.vjudge.net/problem/UVA-1641

【题意】
在一个n*w的字符矩阵里(2<=h,w<=100)用 “.” “/” “\” 画出一个多边形并计算面积

【思路】
从上到下从左到右模拟,字符”/”和”\”对应的格子时半白半黑,其它格子时全黑或全白,用一个变量in记录当前格子是否在多边形内部即可

#include<bits/stdc++.h>
using namespace std;

const int maxn=120;

int n,m;
char g[maxn][maxn];

void solve(){
    int ans=0;
    for(int i=0;i<n;++i){
        bool in=false;
        for(int j=0;j<m;++j){
            if(g[i][j]=='/' || g[i][j]=='\\'){
                ++ans;
                in=!in;
            }
            else if(in) ans+=2;
        }
    }
    printf("%d\n",ans/2);
}

int main(){
    while(scanf("%d%d",&n,&m)==2){
        for(int i=0;i<n;++i) scanf("%s",g[i]);
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xiao_k666/article/details/82152561