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 Alanqky at 2014-08-01 12:11:00 on Problem 1753
#include<stdio.h>
#include<string.h>
#include<iostream>
char str[4];
int bit[16];//原来的东西 
int result[16];//存储的是结果 
int hm;
int ok;
using namespace std;
int check()
{
   for(int i=1;i<16;i++)
     if(bit[i]!=bit[i-1])
     {
        return 0;//表示的是此时已经不匹配 
     }
   return 1;//表示此时已经匹配了    
   
}
void changecolor(int i)
{
  bit[i]=!bit[i];
  int x=i/4;
  int y=i%4;
  if(y<3) bit[i+1]=!bit[i+1];
  if(y>0) bit[i-1]=!bit[i-1];
  if(x>0) bit[i-4]=!bit[i-4];
  if(x<3) bit[i+4]=!bit[i+4]; //changecolor
}
void combine(int start,int hm,int len)
{
   if(ok==1) return;
   for(int i=start;i>=hm;i--)
   {
      result[hm-1]=i-1;//表示的是放置结果 
      if(hm>1) combine(i-1,hm-1,len);
      else
      {
           for(int j=0;j<len;j++)
           changecolor(result[j]);
           if(check())
           {
              ok=1;
              printf("%d\n",len);
              break;
           }
             else
           {
               for(int j=0;j<len;j++)
                changecolor(result[j]);//把他变回去 
           }
      } 
    
   }
   return;
}
int main(void)
{
    int line=4;
    int k=0;
    hm=0; 
    ok=0;
    while(line--)
    {
       scanf("%s",str);
       for(int i=0;i<4;i++)
       {
          if(str[i]=='b')
            bit[k++]=1;
          else if(str[i]=='w')
            bit[k++]=0;
       }
    }
    if(check())
    {
       printf("0\n");//此时已经匹配了
    }
    else
    {
       for(int i=1;i<=16;i++)//这里的i表示的是选择多少个数字来进行查找 
         combine(16,i,i);
       if(ok==0) printf("Impossible\n"); 
    }
    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