POJ 2965 The Pilots Brothers' refrigerator BFS+位运算

题意:冰箱有16个开关,按4*4的矩阵排列。每改变一个开关的状态,与它同行同列的所有开关也要全部改变状态。求出打开冰箱所需要的最少调整次数。
题解:DFS应该也可以。

#include <queue>
#include <iostream>
using namespace std;

queue<int> que;
int step[1<<16];
int point[1<<16];
int cell[] = { 4383, 8751, 17487, 34959, 4593, 8946, 17652, 35064, 7953, 12066, 20292, 36744, 61713, 61986, 62532, 63624 };

int main()
{
	char ch; 
	int i, pos = 0;
	for ( i = 0; i < 16; i++ )
	{
		cin >> ch;
		if ( ch == '+' )
			pos += ( 1 << i );
	}

	memset(step,-1,sizeof(step));
	memset(point,-1,sizeof(point));

	que.push ( pos );
	step[pos] = 0;
	int current, next;
	while ( !que.empty () )
	{
		current = que.front ();
		que.pop();
		for ( i = 0; i < 16; i++ )
		{
			next = current ^ cell[i];
			
			if ( step[next] == -1 )
			{
				point[next] = i;
				step[next] = step[current] + 1;
				que.push ( next );
			}
			if ( next == 0 )
			{
				cout << step[current] + 1 << endl;
				while ( next != pos )
				{
					cout << point[next] / 4 + 1 << ' ' << point[next] % 4 + 1 << endl;
					next = next ^ cell[point[next]];
				}
				return 0;
			}
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值