Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
125686 mairuisheng 翻转棋盘 C++ 通过 100 2 MS 252 KB 1068 2024-01-23 21:00:54

Tests(25/25):


#include<stdio.h> #include<iostream> using namespace std; int chess[4][4]; int c=33; void build() { char c; int i,j; for(i=0;i<4;i++) for(j=0;j<4;j++) { cin>>c; if(c=='w') chess[i][j]=0; else chess[i][j]=1; } } void turn(int x,int y) { if(x>=0&&x<=3&&y>=0&&y<=3) chess[x][y]=!chess[x][y]; } void flip(int s) { int i=s/4; int j=s%4; turn(i,j); turn(i+1,j); turn(i,j+1); turn(i-1,j); turn(i,j-1); } int complete() { int i,j,s1=0; for(i=0;i<4;i++) for(j=0;j<4;j++) s1+=chess[i][j]; if(s1%16) return 0; else return 1; } void dfs(int s,int b) { if(complete()) { if(c>b) c=b; return; } if(s>=16) return; dfs(s+1,b); flip(s); dfs(s+1,b+1); flip(s); } int main() { build(); dfs(0,0); if(c==33) printf("Impossible\n"); else printf("%d\n",c); return 0; }


测评信息: