这里给出四种方法,我每个写完进行过测试,速度从慢到快
1. 10209ms
2. 8133ms
3. 1352ms
4. 389ms
1.数组写法,最慢的写法
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 5;
char bro[N][N] , backup[N][N];
int get(int x,int y ){
return x*4+y;
}
void turn_one(int x,int y)
{
if(bro[x][y] == '+') bro[x][y] = '-';
else bro[x][y] = '+';
}
void turn_all(int x,int y){
for(int i = 0;i<4;i++)
{
turn_one(x,i);
turn_one(i,y);
}
turn_one(x,y);
}
int main()
{
for(int i = 0;i<4;i++)cin>>bro[i];
vector<PII> res;
for(int op = 0;op<1<<16;op++)
{
vector<PII> tmp;
memcpy(backup,bro,sizeof bro);
for(int i = 0;i<4;i++)
for(int j = 0;j<4;j++)
if(op>>get(i,j)&1)
{
tmp.push_back({i,j});
turn_all(i,j);
}
bool has_close = false;
for(int i =0;i<4;i++)
for(int j = 0;j<4;j++)
if(bro[i][j]=='+'){
has_close = true;
break;
}
if(has_close == false)
{
if(res.empty()||res.size()>tmp.size())res = tmp;
}
memcpy(bro,backup,sizeof bro);
}
cout<<res.size()<<endl;
for(auto c:res)cout<<c.x+1<<' '<<c.y+1<<endl;
return 0;
}
2.位优化,比数组快很多
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 4;
int change[N][N];
int get(int x,int y ){
return x*4+y;
}
int main()
{
for(int i = 0;i<N;i++)
for(int j = 0;j<N;j++)
{
for(int k = 0;k<N;k++) change[i][j] +=(1<<get(i,k)) + (1<<get(k,j));
change[i][j] -= 1<<get(i,j);
}
int state = 0;
for(int i = 0;i<4;i++){
string s;
cin>>s;
for(int j = 0;j<4;j++)
if(s[j] == '+') state += 1<<get(i,j);
}
vector<PII> res;
for(int k = 0;k<1<<16;k++)
{
int now = state;
vector<PII> tmp;
for(int i = 0;i<16;i++)
{
if(k>>i&1)
{
int a = i/4,b = i%4;
now ^= change[a][b];
tmp.push_back({a,b});
}
}
if(!now && (res.empty()||res.size()>tmp.size())) res = tmp;
}
cout<<res.size()<<endl;
for(auto c:res)cout<<c.x+1<<' '<<c.y+1<<endl;
return 0;
}
3.dfs,比枚举能优化十倍左右
题中如果存在多种打开冰箱的方式,则按照优先级整体从上到下,同行从左到右打开。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 5;
char g[N][N];
vector<PII> res,tmp;
void turn_one(int x,int y)
{
if(g[x][y]=='+')g[x][y] = '-';
else g[x][y]='+';
}
void turn_all(int x,int y)
{
for(int i = 0;i<4;i++)
{
turn_one(x,i);
turn_one(i,y);
}
turn_one(x,y);
}
void dfs(int x,int y)
{
if(x == 3 && y == 4)
{
bool success = true;
for(int i = 0;i<4;i++)
for(int j = 0; j <4;j++)
if(g[i][j] == '+'){
success = false;
break;
}
if(success)
if(res.empty()||tmp.size()<res.size())
res = tmp;
return;
}
if(y == 4)x++,y= 0;
turn_all(x,y);
tmp.push_back({x,y});
dfs(x,y+1);
tmp.pop_back();
turn_all(x,y);
dfs(x,y+1);
}
int main()
{
for(int i = 0;i<4;i++)scanf("%s",g[i]);
//从0,0开始DFS
dfs(0,0);
cout<<res.size()<<endl;
for(auto c:res)cout<<c.x+1<<' '<<c.y+1<<endl;
return 0;
}
4.dfs+位优化 ,实测比dfs还能快很多
在上面的基础上加上位优化
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cstdio>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 4;
int change[N][N];
vector<PII> res,tmp;
int state = 0;
int now;
int get(int x,int y ){
return x*4+y;
}
void turn_all(int x,int y)
{
now ^= change[x][y];
}
void dfs(int x,int y)
{
if(x == 3 && y == 4)
{
bool success = true;
if(now)
{
success = false;
}
if(success)
if(res.empty()||tmp.size()<res.size())
res = tmp;
return;
}
if(y == 4)x++,y= 0;
turn_all(x,y);
tmp.push_back({x,y});
dfs(x,y+1);
tmp.pop_back();
turn_all(x,y);
dfs(x,y+1);
}
int main()
{
for(int i = 0;i<N;i++)
for(int j = 0;j<N;j++)
{
for(int k = 0;k<N;k++) change[i][j] +=(1<<get(i,k)) + (1<<get(k,j));
change[i][j] -= 1<<get(i,j);
}
for(int i = 0;i<4;i++){
string s;
cin>>s;
for(int j = 0;j<4;j++)
if(s[j] == '+') state += 1<<get(i,j);
}
now = state;
//从0,0开始DFS
dfs(0,0);
cout<<res.size()<<endl;
for(auto c:res)cout<<c.x+1<<' '<<c.y+1<<endl;
return 0;
}
TQL