挑战程序设计初级篇例题

POJ2386   Lake Counting

题意大概是求有多少个水洼,水洼的八个方向是联通的,这道题的思路就是直接DFS搜索,将水洼联通的水块都换成土地,这样就可以保证下次再搜索到水洼的时候不会重复搜索

#include<cstdio>
#include<cstring>
int n,m;
char map1[200][200];
void dfs(int r,int c)
{
    map1[r][c] = '.';
    for(int dr=-1;dr<=1;dr++)
    {
        for(int dc=-1;dc<=1;dc++)
        {
            int nr = r+dr;
            int nc = c+dc;
            if(map1[nr][nc]=='W')
                dfs(nr,nc);
        }
    }
    return;
}
int main()
{
    //freopen("debug\\in.txt","r",stdin);
    int ans = 0;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%s",map1[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(map1[i][j]=='W')
                {
                    dfs(i,j);
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;

}

POJ3253

//Fence Repair 特殊的贪心算法,加上优先队列时间复杂度减低很多

这道题的思路就是每次找最短的两块木板合并
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct cmp1
{
    bool operator()(long long &a,long long &b)
    {
        return a>b;
    }
};
long long n;
int main()
{
    priority_queue<long long,vector<long long>,cmp1>q;
    while(cin>>n)
    {
        for(long long i=0;i<n;i++)
        {
            long long val;
            cin>>val;
            q.push(val);
        }
        long long ans = 0;
        while(!q.empty())
        {
            long long tmp1 = q.top();
            //cout<<tmp1<<endl;
            q.pop();
            long long tmp2 = q.top();
            q.pop();
            ans+=tmp1+tmp2;
            if(q.empty())
                break;
            else
                q.push(tmp1+tmp2);
        }
        cout<<ans<<endl;
    }
    return 0;
}

POJ3617 

//Best Cow line  

这道题大致的题意就是说给你一个字符串,每次从头或者尾取出一个,组成一个字典序最小的字符串,这道题的处理方法可以是从两边往中间靠,左边小就取左边,右边小就取右边,当遇到两个字符字典序相同,就继续往下找
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int n;
char s[2010];
int main()
{
    //freopen("debug\\in.txt","r",stdin);
    while(cin>>n)
    {
        for(int i=0;i<n;i++)
          cin>>s[i];
        int a=0;
        int b=n-1;
        int count = 0;
        while(a<=b)
        {
            bool left = false;
            for(int i=0;a+i<=b-i;i++)
            {
                if(s[a+i]<s[b-i])
                {
                    left = true;
                    break;
                }
                else if(s[a+i]>s[b-i])
                {
                    left = false;
                    break;
                }
            }
            if(left)cout<<s[a++];
            else cout<<s[b--];
            count++;
            if(count==80)
            {
                count=0;
                cout<<endl;
            }
        }
        cout<<endl;
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值