Poj2965

11 篇文章 0 订阅

和Poj1753一样,但是Poj2965要求输出路径,暴力就完事儿了

http://poj.org/problem?id=1753

http://poj.org/problem?id=2965

//Poj2965
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
const int maxnum = 17;
int a[4][4];
int ans = maxnum;
vector<int> m, n,p,q;
int judge() {
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 4; j++) {
			if (a[i][j] != 0) {
				return 0;
			}
		}
	}
	return 1;
}

void flip(int row, int col)
{
	a[row][col] = !a[row][col];
	for (int i = 0; i < 4; i++)
	{
		if (i != col)
		a[row][i] = !a[row][i];
		if (i != row)
		a[i][col] = !a[i][col];
		//a[col][i] = !a[row][i];
	}
	
}

int dfs(int x, int y, int t) {
	
		if (judge()) {
			if (t < ans)
			{
				ans = t;
				p = m;
				q = n;
			}
			return 0;
		}
		if (x >= 4 || y >= 4)return 0;
		int nx = (x + 1) % 4;
		int ny = y + (x + 1) / 4;
		//int ny = (y + 1) % 4;
		//int nx = x + (y + 1) / 4;
		dfs(nx, ny, t);
		flip(x, y);
		m.push_back(x);
		n.push_back(y);
		dfs(nx, ny, t + 1);
		flip(x, y);
		m.pop_back();
		n.pop_back();
	
	return 0;
}

int main() {
	char temp[5];
	for (int i = 0; i < 4; i++) {
		gets_s(temp);
		for (int j = 0; j < 4; j++) {
			a[i][j] = (temp[j] == '+' ? 1 : 0);
		}
	}
	dfs(0, 0, 0);
	if (ans < maxnum) {
		cout << ans << endl;
		for (int i = 0; i < p.size(); i++)
		{
			cout << p[i]+1 << " " << q[i]+1 << endl;
		}
	}
	else printf("Impossible\n");
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值