搜索的入门题
#include "stdio.h"
char fi[105][105];
int move[8][2]={0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
int qu[10005][2]; int head,tail;
void begin() { head=0; tail=-1; }
void inq(int l,int c) { ++tail;
qu[tail][0]=l; qu[tail][1]=c; }
void outq() { head++; }
int empty() { if(head>tail)return 1;
return 0; }
void search(int l,int c,int n,int m) { int tl,tc,i;
begin();
inq(l,c); fi[l][c]='.';
while(!empty()) { tl=qu[head][0]; tc=qu[head][1];
outq();
for(i=0;i<8;i++) { tl+=move[i][0]; tc+=move[i][1];
if(tl>=0&&tl<n&&tc>=0&&tc<m) { if(fi[tl][tc]=='W') { inq(tl,tc); fi[tl][tc]='.'; } }
tl-=move[i][0]; tc-=move[i][1]; } } }
int main() { int n,m; int i,j; int count;
while(scanf("%d%d",&n,&m)!=EOF) { for(i=0;i<n;i++) { scanf("%s",&fi[i][0]); }
count=0; for(i=0;i<n;i++) { for(j=0;j<m;j++) { if(fi[i][j]=='W') { search(i,j,n,m); count++; } } }
printf("%d\n",count); }
return 0; }
目,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"
__int64 num[10005];
void swap(int a,int b) { __int64 t; t=num[a]; num[a]=num[b]; num[b]=t;
}
int partion(int low,int high,int p) { while(low<=high) { if(low<p) { if(num[low]>num[p]){swap(low,p);p=low;} low++; } else { if(high>p) { if(num[high]<num[p]){swap(high,p);p=high;} } high--; } } return p; }
void qsort(int left,int right) { int middle; if(left<right) { middle=(left+right)/2; middle=partion(left,right,middle); qsort(left,middle-1); qsort(middle+1,right); } }
int main() { int n,i; __int64 value,sum;
while(scanf("%d",&n)!=EOF) { for(i=0;i<n;i++) { scanf("%I64d",&num[i]); }
qsort(0,n-1);
value=0; for(i=0;i<n;i++) { value+=num[i]-num[0]; }
sum=value; for(i=1;i<n;i++) { value=value+(num[i]-num[i-1])*i-(num[i]-num[i-1])*(n-i); sum+=value; }
printf("%I64d\n",sum); } return 0; }
一个词:迭代。地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=2253
#include <stdio.h> #include <math.h>
const double MAX = 20000.0;
typedef struct { double x; double y; }point; point stone[201];
double map[201][201];
double dis ( point *a, point *b ) {
return sqrt ( (a->x-b->x)*(a->x-b->x) + (a->y-b->y)*(a->y-b->y) ); }
double maxest ( double a, double b ) {
return a > b ? a : b; }
double cost[201];
double ford ( int s, int t, int n ) {
int u, v;
for ( u=0; u<n; u++ ) { cost[u] = map[s][u]; }
int flag = 1; while ( flag ) { flag = 0; for ( v=0; v<n; v++ ) { for ( u=0; u<n; u++ ) { if ( cost[u] < MAX ) { double max = maxest ( cost[u], map[u][v] ); if ( cost[v] > max ) { cost[v] = max; flag = 1; } } } } } return cost[t]; }
int main () {
int n; int i, j; int count = 0;
while ( scanf ( "%d", &n ) != EOF && n ) { for ( i=0; i<n; i++ ) { scanf ( "%lf%lf", &stone[i].x, &stone[i].y ); } for ( i=0; i<n; i++ ) { for ( j=0; j<=i; j++ ) { 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; }
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
25 | 26 | 27 | 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 | 1 | 2 | 3 | 4 | 5 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
|
|