AtCoder Regular Contest 095 - E Symmetric Grid

65人阅读 评论(0) 收藏 举报
分类:

E - Symmetric Grid


Time limit : 2sec / Memory limit : 256MB

Score : 700 points

Problem Statement

There is an H×W grid (H vertical, W horizontal), where each square contains a lowercase English letter. Specifically, the letter in the square at the i-th row and j-th column is equal to the j-th character in the string Si.

Snuke can apply the following operation to this grid any number of times:

  • Choose two different rows and swap them. Or, choose two different columns and swap them.

Snuke wants this grid to be symmetric. That is, for any 1iH and 1jW, the letter in the square at the i-th row and j-th column and the letter in the square at the (H+1i)-th row and (W+1j)-th column should be equal.

Determine if Snuke can achieve this objective.

Constraints

  • 1H12
  • 1W12
  • |Si|=W
  • Si consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

H W
S1
S2
:
SH

Output

If Snuke can make the grid symmetric, print YES; if he cannot, print NO.


Sample Input 1

Copy
2 3
arc
rac

Sample Output 1

Copy
YES

If the second and third columns from the left are swapped, the grid becomes symmetric, as shown in the image below:


题意:

给你h*w的字符矩阵,你可以交换任意的行或者列。问你能不能使这个矩阵的每一个mp[i][j]=mp[h+1-i][w+1-j].


POINT:

遍历行的情况,然后判断列能不能合法。

swaped[i]=x代表。I行和x行配对。由于配对是两两的。所以情况数最多是11!!,而不是12!。

对于每一种行配对的情况,判断所有列能不能两两配对。

注意奇数的中间多出一列的情况。


英文题解:

E: Symmetric Grid

Notice that an operation about rows and an operation about columns are commutative. Thus, assume that we firstperform operations about rows, and then operations about columns.

Suppose that we finished operations about rows. How can we check if we can achieve a goal by operations aboutcolumns? Notice that, for each j = 1,2,...,W, the j-th column must be the reverse of the W + 1 j-th column. Inparticular, when W is odd, (W + 1)/2 is a palindrome.

Therefore, we can use the following method do check if we can achieve a goal by operations about columns:Initially, all rows are ”unused”.
Choose a particular unused row. Reverse it, and check if it matches with another unused row.

If it matches with another row, mark the two rows as ”used”.

Otherwise, if W is even, the answer is ”NO”. If W is odd and the current row is not a palindrome, theanswer is ”NO”. If W is odd and the current row is a palindrome, we should put it in the (W + 1)/2-throw. If this row is already filled, the answer is ”NO”.

It works in O(HW 2) time (or by binary search you can do it in O(HW log W ))
If we consider all H! possible orders of rows, it will be too slow. Instead, notice that only the set of pairs ((1,H)-th

rows, (2, H 1)-th rows, . . . ) matters. (In case H is odd, one row is left unpaired.)
Thus, we only need to try all pairings of rows. Since there are at most 11!! = 11
×9×7×5×3×1 = 10395 ways

of pairings, it’s fast enough. 



#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int n,m;
string s[20];
int swaped[20];
int used[20];
int ans = 0;
bool check()
{
	memset(used,0,sizeof used);
	for(int i=0;i<m;i++){
		for(int j=0;j<m;j++){
			if(used[i]||used[j]||i==j) continue;
			int cur=1;
			for(int k=0;k<n;k++){
				if(s[swaped[k]][i]!=s[k][j]){
					cur=0;
					break;
				}
			}
			if(cur){
				used[i]=used[j]=1;
			}
		}
	}
	int cnt=0,to=0;
	for(int i=0;i<m;i++){
		if(!used[i]){
			cnt++;to=i;
		}
	}
	if(cnt>=2||(m%2==0&&cnt)){
		return false;
	}
	if(!cnt) return true;
	for(int i=0;i<n;i++){
		if(s[swaped[i]][to]!=s[i][to]){
			return false;
		}
	}
	return true;
}


void dfs(int now,bool ji)
{
	if(now==(n+1)/2){
		if(check()){
			ans=1;
		}
		return;
	}
	for(int i=now;i<n;i++){
		for(int j=i+1;j<n;j++){
			if(swaped[i]==-1&&swaped[j]==-1){
				swaped[i]=j;
				swaped[j]=i;
				dfs(now+1,ji);
				swaped[i]=-1;
				swaped[j]=-1;
			}
		}
		if(ji&&swaped[i]==-1){
			swaped[i]=i;
			dfs(now+1,false);
			swaped[i]=-1;
		}
	}

}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		cin>>s[i];
		swaped[i]=-1;
	}
	if(n&1){
		dfs(0,true);
	}else{
		dfs(0,false);
	}
	if(ans) printf("YES\n");
	else printf("NO\n");
}



查看评论

拨号上网程序

关键词:Visual C++大家知道,在Netants、DownLoad Expert等软件中都带有定时拨号上网下载软件的功能。而一般用户的拨号上网,利用的是Windows的Remote Access...
  • snowguy
  • snowguy
  • 2001-02-13 10:31:00
  • 1012

AtCoder Regular Contest 095

C - Many Medians题目链接题意:给n个数(n为偶数),然后问除第i个数以后的(n-1)个数的第(n-1)/2大是多少,输出所有的i.思路:对原数组copy一份numa[],排序,找出中间...
  • qq_40160605
  • qq_40160605
  • 2018-04-20 17:01:12
  • 33

AtCoder Regular Contest 095 - E Symmetric Grid

E - Symmetric GridTime limit : 2sec / Memory limit : 256MBScore : 700 pointsProblem StatementThere i...
  • Mr_Treeeee
  • Mr_Treeeee
  • 2018-04-15 16:25:14
  • 65

第六场积分赛—E - Optimal Symmetric Paths

题意:给一个带有数字的矩阵,你需要从左上格走到右下格。要求是路径关于右上格与左下格的连线对称且最短。问:这样的路径有多少条。 思路:看到题想当然的以为是让输出从 左上格 到 右下格 的对称路...
  • aaaaheiqiu
  • aaaaheiqiu
  • 2018-04-06 16:41:27
  • 15

UVA 12295 Optimal Symmetric Paths(spfa+记忆化)

题意: 求从左上角到右下角的最短路径数,且要求沿斜线对称 思路: 既然要求对称,所以我们将对称的权值叠加,那么就是求到对角线的最短路径了,通过dp解决方案数// whn6325689 ...
  • u013007900
  • u013007900
  • 2015-06-07 20:57:23
  • 499

C#测量程序运行时间

Stopwatch watch = Stopwatch.StartNew(); doOrther();  watch.Stop(); Console.WriteLine(watch.Elapse...
  • luofeng0710
  • luofeng0710
  • 2012-03-16 15:24:59
  • 447

集群环境下的一些特殊问题

以下摘自:http://apps.hi.baidu.com/share/detail/23532601     一 集群环境下的一些特殊问题 1.1 并发控制 在 集群环...
  • H18010484010
  • H18010484010
  • 2016-09-19 11:03:16
  • 2634

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告[LeetCode]https://leetcode.com/problems/symmetric-tree/Total Accepted: 106639 Tot...
  • fuxuemingzhu
  • fuxuemingzhu
  • 2016-05-08 15:01:08
  • 474

LeetCode---Symmetric Tree

题目大意:给出一个二叉树,判断该树是否关于根节点是中心对称的。 算法思想: 如果一个二叉树是中心对称的,则说明其左右子树对称,由二叉树的性质可知通过前序遍历结果和后序遍历结果可以确定一个二叉树的结...
  • wzg272365960
  • wzg272365960
  • 2015-11-03 16:52:47
  • 214

利用Theano理解深度学习——Multilayer Perceptron

一、多层感知机MLP1、MLP概述对于含有单个隐含层的多层感知机(single-hidden-layer Multi-Layer Perceptron, MLP),可以将其看成是一个特殊的Logist...
  • google19890102
  • google19890102
  • 2015-10-12 11:23:21
  • 5107
    个人资料
    持之以恒
    等级:
    访问量: 5万+
    积分: 4491
    排名: 154万+
    最新评论