Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:为什么dfs错了,还时间很长?

Posted by csuyuanxing at 2010-07-23 14:36:28 on Problem 1992
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator