2017 计蒜之道 初赛 第四场

A题
贪心+dfs
dfs求出每个分割后小块中小孔的个数,除2是能安装芯片的个数,累加即可

#include <bits/stdc++.h>
#define nmax 105
using namespace std;
int mp[nmax][nmax];
int sx[] = {0,1,0,-1};
int sy[] = {1,0,-1,0};
int n,m,k;
bool check(int x, int y)
{
    if(x<0 || x>=n || y<0|| y>=m || mp[x][y] == 1){
        return false;
    }else{
        return true;
    }
}
int dfs(int x, int y,int depth)
{
    mp[x][y] = 1;
    for(int i = 0; i<4;++i){
        int spx = x+sx[i];
        int spy = y+sy[i];
        if(check(spx,spy)){
            return dfs(spx,spy,depth+1);
        }
    }
    return depth;

}
void out()
{
    for(int i = 0; i<n;++i){
        for(int j = 0; j<m;++j){
            printf("%d ",mp[i][j]);
        }
        printf("\n");
    }
}
int main()
{
    while(scanf("%d %d %d",&n,&m,&k) != EOF){
        memset(mp,0,sizeof(mp));
        int d, c;
        for(int i = 0; i<k;++i){
            scanf("%d %d",&d,&c);
            if(d == 0){
                for(int j = 0; j<m;++j){
                    mp[c-1][j] = 1;
                }
            }else{
                for(int j = 0; j<n;++j){
                    mp[j][c-1] = 1;
                }
            }
        }
        int temp = 0,ans = 0;
        for(int i = 0; i<n;++i){
            for(int j = 0; j<m;++j){
                if(check(i,j)){
                    temp = dfs(i,j,1);
                    ans +=temp /2;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

B题
map存pair和个数,然后o(n)扫描map即可

#include <bits/stdc++.h>
using namespace std;
map<pair<int,int>, int> mp;
pair<int,int> p;
int main()
{
    int n;
    while(scanf("%d",&n) != EOF){
        mp.clear();
        for(int i = 0; i<n;++i){
            int x1,x2,y1,y2;
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            int dx = x2-x1;
            int dy = y2-y1;
            p = make_pair(dx,dy);
            mp[p]++;
        }
        map<pair<int,int>,int>::iterator it;
        int nowbiggest = -1e9;
        for(it = mp.begin();it!=mp.end();it++){
            if(it->second>nowbiggest){
                nowbiggest = it->second;
                p = it->first;
            }
        }
        printf("%d %d\n",p.first,p. second);
    }
    return 0;
}
posted @ 2017-05-30 14:06  pengwill  阅读(159)  评论(0编辑  收藏  举报