|
常用鏈接
留言簿(4)
隨筆分類(lèi)
隨筆檔案
搜索
最新評(píng)論

閱讀排行榜
評(píng)論排行榜
Powered by: 博客園
模板提供:滬江博客
|
|
|
|
|
發(fā)新文章 |
|
|
類(lèi)似走迷宮。。。簡(jiǎn)單的深度搜索,遞歸就行了
#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);
}
}


摘要: 簡(jiǎn)單的五子棋判定游戲注意邊界問(wèn)題即可,5個(gè)以上的連續(xù)不算贏
#include <iostream>#include <string>using namespace std;int stone[20][20];bool match(int type,int j,int i){ ... 閱讀全文
未完成。。。 想用窮舉法做,但不知道怎么排除重復(fù)的情況,如3*1+1*3+0和1*3+3*1+0是一樣的。。。 牛人請(qǐng)幫忙
#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打表輸出水題。。。不過(guò)用遞歸卻會(huì)超時(shí),ORZ 對(duì)于這樣一個(gè)0,1序列,a1,a2,a3,...a(i-2),a(i-1),a(i)... 設(shè)f(i)為輸入的數(shù)對(duì)應(yīng)的結(jié)果 用DP做的話,假設(shè)f(i-2),f(i-1)已經(jīng)知道了,那么求f(i)應(yīng)該如下: 當(dāng)取a(i)=0,那么結(jié)果肯定和f(i-1)是一樣的,因?yàn)樵诤竺孀芳拥氖?,肯定不會(huì)導(dǎo)致存在相鄰; 當(dāng)取a(i)=1,那么此時(shí)只要知道a(i-2)就可以了,因?yàn)槲覀兛梢允筧(i-1)為0,這樣就不會(huì)導(dǎo)致相鄰的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--;
}
}


摘要: 幫同學(xué)妹妹弄的作業(yè)。。。以后可以參考
#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) //遍歷左子樹(shù) { visite(p->data); push(s,p); p=p->lchild; } if (!StackEmpty(s)) //通過(guò)下一次循環(huán)中的內(nèi)嵌while實(shí)現(xiàn)右子樹(shù)遍歷 { 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) //遍歷左子樹(shù) { push(s,p); p=p->lchild; } if (!StackEmpty(s)) { p=pop(s); visite(p->data); //訪問(wèn)根結(jié)點(diǎn) p=p->rchild; //通過(guò)下一次循環(huán)實(shí)現(xiàn)右子樹(shù)遍歷 }//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) //遍歷左子樹(shù) { x.ptr = p; x.tag = L; //標(biāo)記為左子樹(shù) 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,表示右子樹(shù)訪問(wèn)完畢,故訪問(wèn)根結(jié)點(diǎn) } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍歷右子樹(shù) p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s)); }//PostOrderUnrec 二。前序最簡(jiǎn)潔算法 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; } } } }
|
|