Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:谁的代码比我长?

Posted by guangmingge at 2013-05-14 22:41:58 on Problem 1753
In Reply To:谁的代码比我长? Posted by:liuweibiao at 2012-02-19 14:35:48
#include<iostream>
#include<cstring>

using namespace std;
int matrix[4][4];
string str[16]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};

int Flip(string str,int color){
                    int total(0);
                    int a[4][4];
                    bool flag(true);
                    for(int i=0;i<4;i++){
                        for(int j=0;j<4;j++){
                                a[i][j] = matrix[i][j];
                        }
                    }

                    for(int i=0;i<4;i++){

                            int tmp = str[i]-'0';
                            if(tmp==1){
                                    total++;
                                    a[0][i] = 1-tmp;
                                    if(i==0){
                                        a[0][1] = 1 - a[0][1];
                                        a[1][0] = 1 - a[1][0];
                                    }else if(i==3){
                                        a[0][2] = 1 - a[0][2];
                                        a[1][3] = 1 - a[1][3];
                                    }else{
                                        a[0][i-1] = 1 - a[0][i-1];
                                        a[0][i+1] = 1 - a[0][i+1];
                                        a[1][i] = 1 - a[1][i];
                                    }
                            }
                    }

                    for(int i=1;i<3;i++){
                            for(int j=0;j<4;j++){
                                    if(a[i-1][j]==color){
                                            total++;
                                            a[i][j] = 1 - a[i][j];
                                            a[i+1][j] = 1 - a[i+1][j];
                                            a[i-1][j] = 1 - a[i-1][j];
                                            if(j>0)
                                                a[i][j-1] = 1 - a[i][j-1];
                                            if(j<3)
                                                a[i][j+1] = 1 - a[i][j+1];
                                    }
                            }
                    }

                    for(int i=0;i<4;i++){
                                    if(i==0){
                                            if(a[2][0]==color){
                                                total++;
                                                a[3][0] = 1 - a[3][0];
                                                a[3][1] = 1 - a[3][1];
                                            }
                                    }

                                    if(i==2||i==1){
                                            if(a[3][i-1]!=a[2][i]){
                                                    flag = false;
                                                    total = 0;
                                                    break;
                                            }else{
                                                    if(a[3][i-1]==color){
                                                            total++;
                                                            a[3][i] = 1 - a[3][i];
                                                            a[3][i-1] = 1 - a[3][i-1];
                                                            a[3][i+1] = 1 - a[3][i+1];
                                                            a[2][i] = 1 - a[2][i];
                                                    }
                                            }
                                    }
                                    if(i==3){
                                                if(a[3][2]!=a[3][3]||a[3][2]!=a[2][3]||a[2][3]!=a[3][3]){
                                                            flag = false;
                                                            total=0;
                                                            break;
                                                }else{
                                                    if(a[3][2]==color){
                                                            total++;
                                                    }
                                                }

                                    }
                    }

                    if(!flag){
                        total==0;
                    }
                    return total;
}

int main(){

    int test(0);
    for(int i=0;i<4;i++){
            string st;
            cin>>st;
            for(int j=0;j<4;j++){
                if(st[j]=='b'){
                    matrix[i][j]=1;
                    test++;
                }else{
                    matrix[i][j]=0;
                }
            }
    }

                if(test==0||test==16){
                        cout<<0<<endl;
                        return 0;
                }

                int min_num(16);
                string tmp;

                for(int i=0;i<16;i++){
                    tmp = str[i];
                    int num1 = Flip(tmp,0);
                    int num2 = Flip(tmp,1);
                    if(num1!=0){
                            if(num1<min_num){
                                min_num = num1;
                            }
                    }
                    if(num2!=0){
                            if(num2<min_num){
                                min_num = num2;
                            }
                    }

                }
                if(min_num==0||min_num==16)
                    cout<<"Impossible"<<endl;
                else
                    cout<<min_num<<endl;

    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator