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 |
Re:有人是用枚举做的吗??给个代码让我研究下~~In Reply To:有人是用枚举做的吗??给个代码让我研究下~~ Posted by:20071121181 at 2009-08-23 11:17:06 #include<iostream> using namespace std; int map[4][4]; int change[16]; int save[16]; int leastchange; int nopen; void turn(int n,int m) { int i; for(i=0;i<4;i++) { if(map[i][m]==1) nopen--; else nopen++; map[i][m]=1-map[i][m]; } for(i=0;i<4;i++) { if(map[n][i]==1) nopen--; else nopen++; map[n][i]=1-map[n][i]; } if(map[n][m]==1) nopen--; else nopen++; map[n][m]=1-map[n][m]; } void backstract(int n,int tc) { if(nopen==16&&tc<leastchange) {leastchange=tc;for(int i=0;i<16;i++) save[i]=change[i];} if(n>15) return; if(tc+1<leastchange) { turn(n/4,n%4); change[n]=1; backstract(n+1,tc+1); turn(n/4,n%4); change[n]=0; } backstract(n+1,tc); } int main() { int i,j; char a; nopen=0; leastchange=16; for(i=0;i<16;i++) change[i]=0; for(i=0;i<16;i++) save[i]=1; for(i=0;i<4;i++) for(j=0;j<4;j++) { cin>>a; if(a=='-') {map[i][j]=1;nopen++;} else map[i][j]=0; } backstract(0,0); cout<<leastchange<<endl; for(i=0;i<16;i++) if(save[i]) cout<<i/4+1<<" "<<i%4+1<<endl; return 1; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator