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 |
一次AC,纪念一下#include<iostream> #include<vector> #include<algorithm> using namespace std; char chess[4][4]; int global=0; //全局 vector<int>ANS; void readdata() { for(int i=0;i<4;i++)for(int j=0;j<4;j++)cin>>chess[i][j]; } void change(int i, int j) { if(chess[i][j]=='b'){chess[i][j]='w';return;} if(chess[i][j]=='w'){chess[i][j]='b';return;} } void alter(int i,int j) { change(i,j); if(i>0)change(i-1,j); if(i<3)change(i+1,j); if(j>0)change(i,j-1); if(j<3)change(i,j+1); } bool ok() { char color=chess[0][0]; for(int i=0;i<4;i++)for(int j=0;j<4;j++)if(chess[i][j]!=color)return false; return true; } void search(int m,int n) { if(ok()){ANS.push_back(global);return;} if(m>=4)return; alter(m,n); global++; if(n<3)search(m,n+1);else search(m+1,0); alter(m,n); global--; if(n<3)search(m,n+1);else search(m+1,0); } void show() { if(!ANS.empty())cout<<*min_element(ANS.begin(),ANS.end()); else cout<<"Impossible"; } int main() { ANS.reserve(100); readdata(); search(0,0); show(); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator