标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 965 Accepted Submission(s): 556
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int n,m; int ans=0; int edge[10][10]; int dfs(int x,int y,int cou) { int i,j,t,sign,ok; /* for(i=0; i<n; i++) { for(j=0; j<m; j++) cout<<edge[i][j]<<" "; cout<<endl; } cout<<endl; */ if(cou>ans) ans=cou; edge[x][y]=2; /**当前行可以增加*/ for(j=y+1; j<n; j++) { if(edge[x][j]==1) continue; sign=0; ok=1; for(t=j-1; t>=0; t--) { if(edge[x][t]!=0) sign++; if(sign==2&&edge[x][t]==2) { ok=0; break; } } if(ok==1) { sign=0; for(t=x-1; t>=0; t--) { if(edge[t][j]!=0) sign++; if(sign==2&&edge[t][j]==2) { ok=0; break; } } if(ok==1) { edge[x][j]=2; dfs(x,j,cou+1); edge[x][j]=0; } } } /**当前行不能增加,加入后面的行*/ for(i=x+1; i<n; i++) { for(j=0; j<m; j++) { if(edge[i][j]==1) continue; sign=0; ok=1; for(t=i-1; t>=0; t--) { if(edge[t][j]!=0) sign++; if(sign==2&&edge[t][j]==2) { ok=0; break; } } if(ok==1) { edge[i][j]=2; dfs(i,j,cou+1); edge[i][j]=0; } } } } int main() { int i,j,q; int x,y; while(scanf("%d%d%d",&n,&m,&q)!=EOF) { memset(edge,0,sizeof(edge)); while(q--) { scanf("%d%d",&x,&y); edge[x][y]=1; } ans=0; for(i=0; i<n; i++) for(j=0; j<m; j++) if(edge[i][j]==0) { edge[i][j]=2; dfs(i,j,1); edge[i][j]=0; } cout<<ans<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/GeekZRF/p/5671730.html