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

贴一个简单易懂的代码

Posted by 1032505101 at 2017-08-20 19:25:31 on Problem 1753
//#include<bits/stdc++.h>
#include<cstring>
#include<iostream>
#include<cstdio>
using namespace std;
#define inf 0x3f3f3f3f
int e[10][10],e1[10][10];
int fx[4][2]={1,0,-1,0,0,1,0,-1};
void flip(int x,int y)
{
 e1[x][y]^=1;
 for(int i=0;i<4;++i)
    e1[x+fx[i][0]][y+fx[i][1]]^=1;
}
int solve(int color)
{
    int i,j,k,high=(1<<4),ret=inf;
    for(i=0;i<high;++i)
    {
        int s=0;
        memcpy(e1,e,sizeof(e));
        for(j=0;j<4;++j)
          if(i&(1<<j)) {s++;flip(1,j+1);}
        for(int x1=2;x1<=4;++x1)
        {
            for(int y1=1;y1<=4;++y1)
            {
                if(e1[x1-1][y1]!=color){
                    s++;
                    flip(x1,y1);
                }
            }
        }
        for(j=1;j<=4;++j)
            if(e1[4][j]!=color) break;
        if(j==5) ret=min(ret,s);
    }
    return ret;
}
int main()
{
    char ch;
    for(int i=1;i<=4;++i)
        for(int j=1;j<=4;++j){
        cin>>ch;
        e[i][j]=ch=='w'?0:1;
    }
    int ans=min(solve(0),solve(1));
    if(ans==inf) puts("Impossible");
    else cout<<ans<<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