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 |
DFS+记忆化搜都TLE,醉了,只能看大牛dp的解题报告了#include <iostream> #include <cstdio> using namespace std; const int _MAXN=1005; const int dirx[4]={0,0,1,-1}; const int diry[4]={1,-1,0,0}; int r,s; char map[_MAXN][_MAXN]; bool used[_MAXN][_MAXN]; int dp[_MAXN][_MAXN]; int ans; bool ok(int x,int y) { return x>=0&&x<r&&y>=0&&y<s; } int dfs(int x,int y) { if(map[x][y]=='#') return dp[x][y]=0; if(x==r-1&&y==s-1) { ans++; return dp[x][y]=1; } if(!dp[x][y]) return 0; int X=0; for(int i=0;i<4;i++) { int tx=x+dirx[i]; int ty=y+diry[i]; if(ok(tx,ty)&&false==used[tx][ty]) { used[tx][ty]=true; if(dfs(tx,ty)) X++; used[tx][ty]=false; } } if(!x) dp[x][y]=0; return dp[x][y]; } int main() { int Z; for(cin>>Z;Z-->0;) { cin>>r>>s; getchar(); for(int i=0;i<r;i++) gets(map[i]); for(int i=0;i<_MAXN;i++) for(int j=0;j<_MAXN;j++) used[i][j]=false,dp[i][j]=1; ans=0; used[0][0]=true; dfs(r-1,0); cout<<"Existuje "<<ans<<" ruznych cest."<<endl; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator