|
常用鏈接
留言簿(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; } } } }
|
|