标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 849 Accepted Submission(s): 211
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> using namespace std; double sum; int t,n,m,flag; int vis[100][100],dir[4][2]={-2,0,2,0,0,2,0,-2}; double num[100][100]; char s[100][100]; int dfs(int x,int y,double ans) { vis[x][y]=1; if(fabs(ans-sum)<=0.000000001)flag=1; for(int i=0;i<4;i++) { int fx=x+dir[i][0],fy=y+dir[i][1]; int px=x+dir[i][0]/2,py=y+dir[i][1]/2; if(fx>=1&&fx<=n&&fy<=m&&fy>=1&&vis[fx][fy]==0&&s[fx][fy]!=‘#‘) { if(s[px][py]==‘+‘) dfs(fx,fy,ans+num[fx][fy]); else if(s[px][py]==‘*‘) dfs(fx,fy,ans*num[fx][fy]); else if(s[px][py]==‘-‘) dfs(fx,fy,ans-num[fx][fy]); else if(s[px][py]==‘/‘&&num[fx][fy]!=0) dfs(fx,fy,ans/num[fx][fy]); } } vis[x][y]=0; return 0; } int main() { scanf("%d",&t); while(t--) { flag=0; memset(vis,0,sizeof(vis)); memset(num,-1,sizeof(num)); scanf("%d%d%lf",&n,&m,&sum); for(int i=1;i<=n;i++) { scanf("%s",s[i]+1); } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(s[i][j]>=‘0‘&&s[i][j]<=‘9‘) { num[i][j]=s[i][j]-‘0‘; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=m;j++) { if(s[i][j]>=‘0‘&&s[i][j]<=‘9‘) { dfs(i,j,num[i][j]); } } } if(flag)printf("Possible\n"); else printf("Impossible\n"); } return 0; }
hdu5612 Baby Ming and Matrix games (dfs加暴力)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5161861.html