码迷,mamicode.com
首页 > 其他好文 > 详细

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B

时间:2016-10-09 07:15:49      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

Description

You are given a table consisting of n rows and m columns.

Numbers in each row form a permutation of integers from 1 to m.

You are allowed to pick two elements in one row and swap them, but no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from 0 to n + 1 actions in total. Operations can be performed in any order.

You have to check whether it‘s possible to obtain the identity permutation 1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table.

Each of next n lines contains m integers — elements of the table. It‘s guaranteed that numbers in each line form a permutation of integers from 1 to m.

Output

If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO" (without quotes).

Examples
input
2 4
1 3 2 4
1 3 4 2
output
YES
input
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
output
NO
input
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5
output
YES
Note

In the first sample, one can act in the following way:

  1. Swap second and third columns. Now the table is
    1 2 3 4
    1 4 3 2
  2. In the second row, swap the second and the fourth elements. Now the table is
    1 2 3 4
    1 2 3 4

题意:n*m的矩阵,可以做一行中的两个数交换,或者是列交换(每种最多一次),问可不可以还原成1-m的形式

解法:暴力,首先从第一行的1,2位置的数交换,交换后看错位的数量是不是小于等于2,大于2那么就算再交换一次也没办法还原了。直到有两个位置符合要求,则把这一次交换看成是列交换,(比如第一组数据)再根据这两个位置,将下一行也交换(因为我们是列交换嘛),判断是否符合小于等于2的条件,直到结束

#include<bits/stdc++.h>
using namespace std;
int n,m;
int a[200][200];
int b[200][200];
set<int>q,q2;
map<int,int>p;
map<int,int>::iterator it;
int cmd(int x,int y)
{
    int flag=0;
    for(int i=1;i<=n;i++)
    {
        int num=0;
        swap(b[i][x],b[i][y]);
        for(int j=1;j<=m;j++)
        {
            if(b[i][j]!=j)
            {
                num++;
            }
        }
      //  cout<<x<<" "<<y<<" "<<num<<endl;
        if(num>2)
        {
       //     cout<<"A"<<endl;
            return 0;
        }
    }
    return 1;
}
int main()
{
    int flag=0;
    cin>>n>>m;
    for(int i=1; i<=n; i++)
    {
        int num=0;
        for(int j=1; j<=m; j++)
        {
            cin>>a[i][j];
            b[i][j]=a[i][j];
            if(a[i][j]!=j)
            {
                num++;
                p[j]++;
                q.insert(j);
                q2.insert(i);
            }
        }
        if(num>2)
        {
            flag=1;
        }
        // cout<<num<<endl;
    }
    /*  if(q2.size()!=n)
      {
          for(int i=1; i<=n; i++)
          {
              if(p[i]>=3)
              {
                  cout<<"NO"<<endl;
                  return 0;
              }
          }
          cout<<"YES"<<endl;
      }
      else if(q2.size()==n)
      {
          if(q.size()==2)
          {
              cout<<"YES"<<endl;
          }*/

    int flag1=0;
    if(flag==0)
    {
        flag1=1;
    }
    for(int i=1;i<=m;i++)
    {
        for(int j=i+1;j<=m;j++)
        {
            if(cmd(i,j))
            {
                cout<<i<<" "<<j<<endl;
                flag1=1;
            }
            for(int _=1;_<=n;_++)
            {
                for(int q=1;q<=m;q++)
                {
                    b[_][q]=a[_][q];
                }
            }
        }
    }
    if(flag1)
    {
        puts("YES");
    }
    else
    {
        puts("NO");
    }
    return 0;
}

  

 

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) B

标签:

原文地址:http://www.cnblogs.com/yinghualuowu/p/5940692.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!