| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:为什么dfs错了,还时间很长?In Reply To:Re:为什么dfs错了,还时间很长? Posted by:csuyuanxing at 2010-07-23 14:35:54 #include <cstdio>
#include <cstring>
//#include <conio.h>
const int MAXN=1000+5;
char maze[MAXN][MAXN];
int cnt;
int i;
int num;
int steps;
int R,S;
void dfs(int i,int j)
{
if(++steps==R+S-2)
{
if(i==R-1&&j==S-1)num++;
return;
}
if( maze[i+1][j]=='.' && i+1<R){dfs(i+1,j);steps--;}
if( maze[i][j+1]=='.' && j+1<S){dfs(i,j+1);steps--;}
}
int main()
{
//freopen("input.txt","r",stdin);
scanf("%d",&cnt);
while(cnt--)
{
scanf("%d%d\n",&R,&S);
for(i=0;i<R;i++)
gets( *(maze+i) );
num=0;
steps=-1;
dfs(0,0);
printf("Existuje %d ruznych cest.\n",num);
}
//getch();
return 0;
}
我改进成这样照样超时,难道只能dp?
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator