标签:== i+1 mat 改变 输出 stream main https 开始
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
map<int,int>Map;
queue<int>q;
inline int get_map(int x,int i){//对于状态x,我按下第i位,会变成什么状态?
x=x^(1<<i);
if((i%5)<4)x=x^(1<<(i+1));
if(i%5)x=x^(1<<(i-1));
if(i>=5)x=x^(1<<(i-5));
if(i<20)x=x^(1<<(i+5));
return x;
}
inline void bfs(){
q.push((1<<25)-1);Map[(1<<25)-1]=0;
while(!q.empty()){
int now=q.front();q.pop();
if(Map[now]==7)return;
for(int i=0;i<25;++i){
int res=get_map(now,i);
if(!Map[res]){
Map[res]=Map[now]+1;
q.push(res);
}
}
}
}
int main(){
bfs();
int T=read();
while(T--){
int cnt=0;
for(int i=0;i<25;++i){
char ch;cin>>ch;
cnt+=((ch-'0')<<i);
}
if(Map[cnt])printf("%d\n",Map[cnt]);
else puts("-1");
}
return 0;
}
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
inline int read(){
int x=0,o=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')o=-1,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*o;
}
int ans,a[10][10],b[10][10];
inline int check(int now){
int sum=now;
for(int i=1;i<=5;i++)
for(int j=1;j<=5;j++)
b[i][j]=a[i][j];
for(int i=1;i<=4;i++)
for(int j=1;j<=5;j++)
if(!b[i][j]){
++sum;
b[i][j]^=1;
b[i+1][j]^=1;
b[i+1][j-1]^=1;
b[i+1][j+1]^=1;
b[i+2][j]^=1;
}
for(int i=1;i<=5;i++)if(!b[5][i])return 1e9;
return sum;
}
inline void dfs(int lie,int now){
if(lie>5){
ans=min(ans,check(now));
return;
}//第一行点击方式枚举完毕
a[1][lie]^=1;a[1][lie-1]^=1;a[1][lie+1]^=1;a[2][lie]^=1;
dfs(lie+1,now+1);//点击第一行第lie列,步数now+1
a[1][lie]^=1;a[1][lie-1]^=1;a[1][lie+1]^=1;a[2][lie]^=1;
dfs(lie+1,now); //回溯,也就是不点击第一行第lie列
return;
}
int main(){
int T=read();
while(T--){
for(int i=1;i<=5;++i)
for(int j=1;j<=5;++j){
char ch;cin>>ch;
a[i][j]=ch-'0';
}
ans=1e9;dfs(1,0);
if(ans<=6)printf("%d\n",ans);
else puts("-1");
}
return 0;
}
标签:== i+1 mat 改变 输出 stream main https 开始
原文地址:https://www.cnblogs.com/PPXppx/p/11253790.html