|
常用链接
留言簿(4)
随笔分类
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
Powered by: 博客园
模板提供:沪江博客
|
|
|
|
|
发新文章 |
|
|
类似走迷宫。。。简单的深度搜索,递归就行了
#include <iostream> #include <string> using namespace std;
char tiles[101][101]; int used[101][101]; int count,w,h;
void countstep(int i,int j) { count++; if (i>1&&used[i-1][j]==0) //向上 { used[i-1][j]=1;countstep(i-1,j); } if (i<h&&used[i+1][j]==0) //向下 { used[i+1][j]=1;countstep(i+1,j); } if (j>0&&used[i][j-1]==0) //向左 { used[i][j-1]=1;countstep(i,j-1); } if (j<w-1&&used[i][j+1]==0) //向右 { used[i][j+1]=1;countstep(i,j+1); } } int main() { int starti,startj; while (scanf("%d %d",&w,&h)!=EOF) { if (w==0||h==0) break; count=0; memset(used,0,sizeof(used)); for (int i=1;i<=h;i++) { scanf("%s",&tiles[i]); } for(int i=1;i<=h;i++) { for (int j=0;j<w;j++) { if (tiles[i][j]=='#') used[i][j]=1; else if (tiles[i][j]=='@') { used[i][j]=1; startj=j; starti=i; } } } countstep(starti,startj); printf("%d\n",count); } }
摘要: 简单的五子棋判定游戏注意边界问题即可,5个以上的连续不算赢
#include <iostream>#include <string>using namespace std;int stone[20][20];bool match(int type,int j,int i){ ... 阅读全文
未完成。。。 想用穷举法做,但不知道怎么排除重复的情况,如3*1+1*3+0和1*3+3*1+0是一样的。。。 牛人请帮忙
#include <iostream> #include <string> using namespace std;
int darts[61]; int sum=0,everysum=1;
int dartscount(int current,int times,int start) { for (int i=start;i>0;i--) { if (darts[i]>0) { current=current-i; if (current==0&×>0) { sum+=everysum*darts[i]; } else if (current>0&×>1) { everysum*=darts[i]; sum=dartscount(current,times-1,i); everysum/=darts[i]; } current+=i; } } return sum; }
int main() { memset(darts,0,sizeof(darts)); darts[25]=1;darts[50]=1; for (int i=1;i<=60;i++) { if (i<=20) darts[i]++; if (i%2==0&&i/2<=20) darts[i]++; if (i%3==0&&i/3<=20) darts[i]++; } int n,num,k=1; scanf("%d",&n); while (n>0) { printf("Scenario #%d:\n",k++); scanf("%d",&num); sum=0;everysum=1; printf("%d\n\n",dartscount(num,3,num)); n--; } }
典型的DP打表输出水题。。。不过用递归却会超时,ORZ 对于这样一个0,1序列,a1,a2,a3,...a(i-2),a(i-1),a(i)... 设f(i)为输入的数对应的结果 用DP做的话,假设f(i-2),f(i-1)已经知道了,那么求f(i)应该如下: 当取a(i)=0,那么结果肯定和f(i-1)是一样的,因为在后面追加的是0,肯定不会导致存在相邻; 当取a(i)=1,那么此时只要知道a(i-2)就可以了,因为我们可以使a(i-1)为0,这样就不会导致相邻的1了; 所以a[i]=a[i-1]+a[i-2];
#include <iostream> #include <string> using namespace std;
int fabi[50]; void init() { fabi[1]=2; fabi[2]=3; for (int j=3;j<=45;j++) { fabi[j]=fabi[j-1]+fabi[j-2]; } }
int main() { int n,i=1,num; init(); cin>>n; while (n>0) { cin>>num; printf("Scenario #%d:\n",i++); printf("%d\n\n",fabi[num]); n--; } }
摘要: 帮同学妹妹弄的作业。。。以后可以参考
#include <iostream>#include <vector>#include <string>#include <math.h>#include <iomanip>#include <stdlib.h>#includ... 阅读全文
1.先序遍历非递归算法 void PreOrderUnrec(Bitree *t) { Stack s; StackInit(s); Bitree *p=t; while (p!=NULL || !StackEmpty(s)) { while (p!=NULL) //遍历左子树 { visite(p->data); push(s,p); p=p->lchild; } if (!StackEmpty(s)) //通过下一次循环中的内嵌while实现右子树遍历 { p=pop(s); p=p->rchild; }//endif }//endwhile }
2.中序遍历非递归算法 void InOrderUnrec(Bitree *t) { Stack s; StackInit(s); Bitree *p=t;
while (p!=NULL || !StackEmpty(s)) { while (p!=NULL) //遍历左子树 { push(s,p); p=p->lchild; } if (!StackEmpty(s)) { p=pop(s); visite(p->data); //访问根结点 p=p->rchild; //通过下一次循环实现右子树遍历 }//endif }//endwhile }
3.后序遍历非递归算法 typedef enum{L,R} tagtype; typedef struct { Bitree ptr; tagtype tag; }stacknode;
typedef struct { stacknode Elem[maxsize]; int top; }SqStack;
void PostOrderUnrec(Bitree t) { SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) && s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s)); }//PostOrderUnrec 二。前序最简洁算法 void PreOrderUnrec(Bitree *t) { Bitree *p; Stack s; s.push(t);
while (!s.IsEmpty()) { s.pop(p); visit(p->data); if (p->rchild != NULL) s.push(p->rchild); if (p->lchild != NULL) s.push(p->lchild); } }
三。后序算法之二 void BT_PostOrderNoRec(pTreeT root) { stack<treeT *> s; pTreeT pre=NULL;
while ((NULL != root) || !s.empty()) { if (NULL != root) { s.push(root); root = root->left; } else { root = s.top(); if (root->right!=NULL && pre!=root->right){ root=root->right; } else{ root=pre=s.top(); visit(root); s.pop(); root=NULL; } } } }
|
|