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;
}