搜索的入门题
#include "stdio.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
char fi[105][105];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) int move[8][2]= {0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int qu[10005][2];
int head,tail;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void begin()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
head=0;
tail=-1;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void inq(int l,int c)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
++tail;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qu[tail][0]=l;
qu[tail][1]=c;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void outq()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
head++;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int empty()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
if(head>tail)return 1;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void search(int l,int c,int n,int m)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int tl,tc,i;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
begin();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
inq(l,c);
fi[l][c]='.';
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(!empty())
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tl=qu[head][0];
tc=qu[head][1];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
outq();
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for(i=0;i<8;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
tl+=move[i][0];
tc+=move[i][1];
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
if(tl>=0&&tl<n&&tc>=0&&tc<m)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(fi[tl][tc]=='W')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
inq(tl,tc);
fi[tl][tc]='.';
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
tl-=move[i][0];
tc-=move[i][1];
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int n,m;
int i,j;
int count;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(scanf("%d%d",&n,&m)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%s",&fi[i][0]);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
count=0;
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(j=0;j<m;j++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(fi[i][j]=='W')
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
search(i,j,n,m);
count++;
}
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
printf("%d\n",count);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
目,BFS和DFS都可以的,地址: http://acm.pku.edu.cn/JudgeOnline/problem?id=2386
摘要: 大数的运算,地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=2325
#include<stdio.h>#include<stdlib.h>#include<string.h>const int OneNode = 1000000; //?????????????... 阅读全文
不知不觉工作已经一月有余,也开始做东西了,BOSS好像也挺信任我的,一下子给了好多东西,自己也就慢慢做,呵呵,加油,加油!
摘要: 二叉堆,算法是固定的。地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=2201
#include <stdio.h>typedef struct{ int k; int a; &nbs... 阅读全文
排序然后贪心吧,自己也有点忘记了。地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=2231
#include "stdio.h"
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
__int64 num[10005];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void swap(int a,int b)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
__int64 t;
t=num[a];
num[a]=num[b];
num[b]=t;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int partion(int low,int high,int p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
while(low<=high)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(low<p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(num[low]>num[p]) {swap(low,p);p=low;}
low++;
}
else
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if(high>p)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) if(num[high]<num[p]) {swap(high,p);p=high;}
}
high--;
}
}
return p;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
void qsort(int left,int right)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int middle;
if(left<right)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
middle=(left+right)/2;
middle=partion(left,right,middle);
qsort(left,middle-1);
qsort(middle+1,right);
}
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
int n,i;
__int64 value,sum;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while(scanf("%d",&n)!=EOF)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf("%I64d",&num[i]);
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
qsort(0,n-1);
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
value=0;
for(i=0;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
value+=num[i]-num[0];
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
sum=value;
for(i=1;i<n;i++)
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
value=value+(num[i]-num[i-1])*i-(num[i]-num[i-1])*(n-i);
sum+=value;
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
printf("%I64d\n",sum);
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
一个词:迭代。地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=2253
#include <stdio.h>
#include <math.h>
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
const double MAX = 20000.0;
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
typedef struct
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
double x;
double y;
}point;
point stone[201];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
double map[201][201];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
double dis ( point *a, point *b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return sqrt ( (a->x-b->x)*(a->x-b->x) + (a->y-b->y)*(a->y-b->y) );
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
double maxest ( double a, double b )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
return a > b ? a : b;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
double cost[201];
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
double ford ( int s, int t, int n )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int u, v;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
for ( u=0; u<n; u++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cost[u] = map[s][u];
}
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int flag = 1;
while ( flag )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
flag = 0;
for ( v=0; v<n; v++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( u=0; u<n; u++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
if ( cost[u] < MAX )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
double max = maxest ( cost[u], map[u][v] );
if ( cost[v] > max )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
cost[v] = max;
flag = 1;
}
}
}
}
}
return cost[t];
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
int main ()
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif) ![](http://www.cppblog.com/Images/OutliningIndicators/ContractedBlock.gif) {
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
int n;
int i, j;
int count = 0;
![](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
while ( scanf ( "%d", &n ) != EOF && n )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
scanf ( "%lf%lf", &stone[i].x, &stone[i].y );
}
for ( i=0; i<n; i++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
for ( j=0; j<=i; j++ )
![](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif) {
map[i][j] = map[j][i] = dis ( &stone[i], &stone[j] );
}
}
printf ( "Scenario #%d\nFrog Distance = %.3lf\n\n", ++count, ford ( 0, 1, n ) );
}
return 0;
}
![](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
![](/images/xml.gif)
阅读排行榜
评论排行榜
|
|