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 |
请路过的大虾帮忙看看为什么输入4*3的正确,输入6*6的就死循环了呢?#include<iostream> using namespace std; int flag[27][27]; int vr[900],vc[900]; int count=0; bool ac=0; int p,q; int ii; int add[8][2]={{-1,-2},{-1,2},{1,-2},{1,2},{2,1},{2,-1},{-2,1},{2,-1}};//访问规则 void dfs(int r,int c) { if(ac==0&&count==p*q) { ac=1;//问题已解决 printf("Scenario #%d:\n",ii); for(int i=0;i<count;i++) { printf("%c%d",vr[i]+'A',vc[i]+1); } printf("\n\n");return; } else if(ac==0) { for(int j=0;j<8;j++) { int tr=r+add[j][0]; int tc=c+add[j][1]; if(tr<p&&tr>=0&&tc<q&&tc>=0&&flag[tr][tc]==0) { vr[count]=tr;vc[count]=tc; count++; flag[tr][tc]=1; dfs(tr,tc); flag[tr][tc]=0; count--; } } } } int main() { int n; cin>>n; for(ii=1;ii<=n;ii++) { ac=0; scanf("%d%d",&q,&p); memset(flag,0,sizeof(flag)); for(int j=0;j<p;j++) { for(int k=0;k<q;k++) { count=1; vr[0]=j;vc[0]=k;//设置起点 flag[j][k]=1; dfs(j,k); if(ac==1) break; } if(ac==1)break; } if(ac==0) printf("Scenario #%d:\nimpossible\n\n",ii); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator