ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

POJ2965(找规律)题解

2021-08-06 13:34:36  阅读:201  来源: 互联网

标签:规律 int 题解 行和列 handles POJ2965 open refrigerator 翻转


POJ2965(找规律)题解

题目

The Pilots Brothers’ refrigerator
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 35766 Accepted: 13828

Description
The game “The Pilots Brothers: following the stripy elephant” has a quest where a player needs to open a refrigerator.

There are 16 handles on the refrigerator door. Every handle can be in one of two states: open or closed. The refrigerator is open only when all handles are open. The handles are represented as a matrix 4х4. You can change the state of a handle in any location [i, j] (1 ≤ i, j ≤ 4). However, this also changes states of all handles in row i and all handles in column j.

The task is to determine the minimum number of handle switching necessary to open the refrigerator.
Input
The input contains four lines. Each of the four lines contains four characters describing the initial state of appropriate handles. A symbol “+” means that the handle is in closed state, whereas the symbol “−” means “open”. At least one of the handles is initially closed.
Output
The first line of the input contains N – the minimum number of switching. The rest N lines describe switching sequence. Each of the lines contains a row number and a column number of the matrix separated by one or more spaces. If there are several solutions, you may give any one of them.

题意

这题和POJ1753差不多,只是每一次改变影响的范围变成了行和列的所有点,最终目的是全部变为“-”。

分析

一开始,我直接套用的我之前写的 POJ1753(枚举)的代码,更改了翻转函数,加了个string用于存储翻转过程中的i和j。结果超时了,但最多只有65535次运算,不知道为毛会超时。
后来转换思路,发现了规律,想要将一个点s的“+”变成“-”,只要将s所在的行和列的所有点都翻转一遍就能实现。因为整个过程中只有s点翻转了奇数次,s所在的行和列的其他点都翻转了偶数次,等于没变化。所以我们只需要遍历矩阵,每次遇到“+”就翻转改点所在的行和列的所有点一次。统计所有点翻转的次数,如果是奇数则要翻转,偶数则不用。输出所有要翻转的点。

代码

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int g[7][7] = {0};
	for (int i = 1; i <= 4; i ++ )
	{
		for (int j = 1; j <= 4; j ++ )
		{
			char c;
			cin >> c;
			if (c == '+')
			{
				for (int k = 1; k <= 4; k ++ )
				{
					g[i][k] ++;
					g[k][j] ++;
				}
				g[i][j] --;
			}
		}
	}
	int res = 0;
	for (int i = 1; i <= 4; i ++ )
	{
		for (int j = 1; j <= 4; j ++ )
		{
			if (g[i][j] & 1) res ++ ;
		}
	}
	cout << res << endl;
	for (int i = 1; i <= 4; i ++ )
	{
		for (int j = 1; j <= 4; j ++ )
		{
			if (g[i][j] & 1) cout << i << " " << j << endl;
		}
	}
	return 0;
}

标签:规律,int,题解,行和列,handles,POJ2965,open,refrigerator,翻转
来源: https://blog.csdn.net/weixin_45609500/article/details/119452604

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有